I am attempting to create a Project for a Business Account programmatically.
public class ExtendProjects_BusinessAccountMaint_Extension : PXGraphExtension<BusinessAccountMaint>
{
public PXAction<BAccount> CreateServicesProjectAction;
[PXMassAction]
[PXButton(CommitChanges = true, Category = "Record Creation")]
[PXUIField(DisplayName = "Create 'Services' Project")]
protected virtual void createServicesProjectAction()
{
BAccount bAccount = Base.BAccount.Current;
if (bAccount is null) return;
PXLongOperation.StartOperation(Base, delegate ()
{
CreateServicesProject(bAccount.BAccountID);
});
}
public static void CreateServicesProject(int? bAccountID)
{
if (bAccountID is null) return;
var bAccountGraph = PXGraph.CreateInstance<BusinessAccountMaint>();
BAccount bAccount = bAccountGraph.BAccount.Current = bAccountGraph.BAccount.Search<BAccount.bAccountID>(bAccountID);
if (bAccount is null) return;
var templateGraph = PXGraph.CreateInstance<PX.Objects.PM.TemplateMaint>();
PMProject template = templateGraph.Project.Search<PMProject.contractCD>("SERVICES");
if (template is null)
throw new PXException("A 'Services' template does not exist.");
PMProject servicesProj = SelectFrom<PMProject>.Where<PMProject.customerID.IsEqual<@P.AsInt>.And<PMProject.templateID.IsEqual<@P.AsInt>>>.View.ReadOnly.SelectWindowed(new PXGraph(), 0, 1, bAccount.BAccountID, template.ContractID);
if (servicesProj is null)
{
var projectGraph = PXGraph.CreateInstance<ProjectEntry>();
PMProject project = projectGraph.Project.Current = projectGraph.Project.Insert(new PMProject());
projectGraph.Project.SetValueExt<PMProject.customerID>(project, bAccount.BAccountID);
projectGraph.Project.SetValueExt<PMProject.templateID>(project, template.ContractID);
projectGraph.Project.SetValueExt<PMProject.description>(project, "Services");
projectGraph.Project.Update(project);
projectGraph.Save.Press();
}
}
}
Sometimes it succeeds, but for some accounts, it fails repeatedly with 'Another process has added the 'ContractBillingSchedule' record. Your changes will be lost.'
Is this code-related or a bug? I did see a few other posts of similar errors in some versions of 21 R1, but we’re in 21 R2.