Related to this post
I am working on setting the attributes on a service order when creating a service order from an opportunity by pulling what is set on the opportunity. I have gotten it to set the value and work as long as you click “Create and Review”, and then save. The problem is when you click create, the cache is lost and the value does not save. I then tried adding the Persist action to my code, but was getting the error that the attribute value cannot be empty. There was very clearly a value being set because I didn’t modify any other code.
I came to find out that if the attribute is required, it can’t save, but if the attribute is not required, it can save and the code works fine. It seems like, it is pulling the default value set for the attribute under the service order type, then saving, then getting my value and saving on top of that because if I set it required with a default value specified, it also works. When it is required and no default value set, it tries to save a null value into a required field and breaks before it gets to my value.
Is there a way to stop it from saving the default value first or make it not required in this instance?
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 attributes = srvOrdGraph.Answers.Select()?.FirstTableItems;
CSAnswers attribute = attributes.FirstOrDefault(a => a.AttributeID == "SUBSERVICE");
var subservice = (PXStringState)Base.OpportunityCurrent.Cache.GetValueExt(Base.OpportunityCurrent.Current, "SUBSERVICE_Attributes");
if (attribute is object)
{
attribute.Value = subservice;
srvOrdGraph.Answers.Update(attribute);
srvOrdGraph.Persist();
}
}