Skip to main content
Solved

Copying Attributes from Opportunity to Service Order

  • 8 August 2024
  • 9 replies
  • 80 views

We would like to have our attribute that is listed on the opportunity to transfer over to the service order when it is converted. It is the exact same attribute defined for both the service order and the opportunity and it should transfer over. 

I have done some digging on here and have found this to override the CreateSrvOrdDocument action and then found this solution by @Vignesh Ponnusamy to update attributes if they are User-Defined fields. I can get that to work great, it’s just that our attribute is listed under the attributes tab instead. I then modified the code to see if I could access those attribute values. I have used the below code

using PX.Data;
using PX.Objects.CS;
using PX.Objects.FS;
using System.Collections.Generic;

namespace PX.Objects.CR
{
public class DialogBoxSOApptCreation_Ext : PXGraphExtension<SM_OpportunityMaint_DBox, SM_OpportunityMaint, OpportunityMaint>
{
public static bool IsActive() => PXAccess.FeatureInstalled<FeaturesSet.serviceManagementModule>();

#region Event Handlers
public delegate void CreateDocumentDelegate(ServiceOrderEntry srvOrdGraph, AppointmentEntry apptGraph, DBoxHeader header, List<DBoxDetails> details);
PXOverride]
public void CreateDocument(ServiceOrderEntry srvOrdGraph, AppointmentEntry apptGraph, DBoxHeader header, List<DBoxDetails> details, CreateDocumentDelegate baseMethod)
{
baseMethod(srvOrdGraph, apptGraph, header, details);

var subservice = (PXStringState)Base.OpportunityCurrent.Cache.GetValueExt(Base.OpportunityCurrent.Current, "SUBSERVICE_Attributes");
srvOrdGraph.CurrentServiceOrder.Cache.SetValueExt(srvOrdGraph.CurrentServiceOrder.Current, "SUBSERVICE_Attributes", subservice.Value);
}
#endregion
}
}

I can see that I am able to access the opportunity attribute when I debug, but it cannot find the attribute field on the service order to set the value. If I change the field to be “AttributeSUBSERVICE” and test it as a UDF, it works great. 

Is there any way to make it work on the attribute tab or am I limited to only using the attribute as a UDF?

Thanks, 

Drew

I think you need something like this:

var attributes = srvOrdGraph.Answers.Select()?.FirstTableItems;

CSAnswers attribute = attributes.FirstOrDefault(a => a.AttributeID == "attributeID");
if (attribute is object)
{
attribute.Value = newValue;
srvOrdGraph.Answers.Update(attribute);
}

 


@darylbowman, you are amazing, thank you very much!


@darylbowman 

Would you be able to provide a way to save those changes to the database? When they click “Create and Review”, it gets saved to the cache and then they press save and it works fine. If they press “Create”, they don’t get redirected, so the cache gets lost and the attribute doesn’t get updated. 

I have tried using the following 

if (attribute is object)
{
attribute.Value = subservice;
srvOrdGraph.Answers.Update(attribute);
srvOrdGraph.Answers.Cache.Persist(PXDBOperation.Update);
}
if (attribute is object)
{
attribute.Value = subservice;
srvOrdGraph.Answers.Update(attribute);
srvOrdGraph.Actions.PressSave();
}

The first gave me no error, but did not keep the value, the second gave me the following error:

Inserting 'Answers' record raised at least one error. Please review the errors. 'Value' cannot be empty

I’m still somewhat new to this and could use a hand.


I believe the second option would be the way to go. Have you verified the value being set isn’t empty? Maybe check that in the if and only set if not empty?


if (attribute is object)
{
attribute.Value = subservice;
srvOrdGraph.Answers.Update(attribute);
if (attribute.Value != null)
{
srvOrdGraph.Actions.PressSave();
}
}

I’ve tried this, debugged and it still is giving me that error. The value isn’t null and it still says it is for whatever reason


What about srvOrdGraph.Persist()?


Same error unfortunately


Not sure what to say then. Seems like something is happening that’s not being accounted for.


So after some testing, I figured out that if the attribute is required for the service order type, I get the error saying that the value cannot be empty, but if the exact same attribute with the exact same code is not required, it saves perfectly and functions just fine. I’ve tried to force it not be required by using “attribute.IsRequired = false”, but it still doesn’t like it for some reason. It also seems to work when there is a default value set under the service order type.

It’s almost like when it’s saving, it pulls the default value, saves, then saves my value on top of that. When the default value is null, it stops before it gets to save my value. However when there is a default value set or it isn’t required, it is able to initially save and then save my value next.


Reply