Search code examples
acumatica

How to create a custom Order Type using customization plug-in (Acumatica)


How to create a custom Order Type using customization plug-in? I used a manual from here. I tried to initialize required filds from exesting order type, but without success. I tried to use Update method on graph as well. Every time I get a common error: Inserting 'Order Type' record raised at least one error. Please review the errors.' and can't see what field the issue is related to. My UpdateDatabase() method:

public override void UpdateDatabase()
        {
            SOOrderTypeMaint orderTypeGraph = PXGraph.CreateInstance<SOOrderTypeMaint>();
            string newOrderTypeName = "SR";
            var existingOrderType = orderTypeGraph.soordertype.Search<SOOrderType.orderType>(newOrderTypeName);
            if (existingOrderType.Count != 0)
            {
                WriteLog(string.Format("{0} type already exist", newOrderTypeName));
            }
            else
            {
                SOOrderType exempleOrderType = SOOrderType.PK.Find(orderTypeGraph, "SO");
                SOOrderTypeOperation exgrid = SOOrderTypeOperation.PK.Find(orderTypeGraph, "SO", "I");
                var ourGrid = new SOOrderTypeOperation();
                var sROrderType = new SOOrderType();
                
                ourGrid.Operation = exgrid.Operation;
                ourGrid.INDocType = exgrid.INDocType;
                ourGrid.OrderPlanType = exgrid.OrderPlanType;
                ourGrid.ShipmentPlanType = exgrid.ShipmentPlanType;
                ourGrid.RequireReasonCode = exgrid.RequireReasonCode;
                sROrderType.OrderType = newOrderTypeName;
                sROrderType.Descr = "description";
                sROrderType.Active = exempleOrderType.Active;
                sROrderType.OrderNumberingID = exempleOrderType.OrderNumberingID;
                sROrderType.FreightSubID = exempleOrderType.FreightSubID;
                sROrderType.DiscountAcctID = exempleOrderType.DiscountAcctID;
                sROrderType.DiscountSubID = exempleOrderType.DiscountSubID;
                sROrderType.Behavior = exempleOrderType.Behavior;
                sROrderType.DefaultOperation = exempleOrderType.DefaultOperation;
                sROrderType.ARDocType = exempleOrderType.ARDocType;
                sROrderType.AllowQuickProcess = exempleOrderType.AllowQuickProcess;
                sROrderType.DiscSubMask = exempleOrderType.DiscSubMask;
                sROrderType.CalculateFreight = exempleOrderType.CalculateFreight;
                sROrderType.SalesSubMask = exempleOrderType.SalesSubMask;
                sROrderType.FreightSubMask = exempleOrderType.FreightSubMask;
                sROrderType.FreightAcctID = exempleOrderType.FreightAcctID;
                sROrderType.InvoiceNumberingID = exempleOrderType.InvoiceNumberingID;
                sROrderType.RequireShipping = exempleOrderType.RequireShipping;
                                
                orderTypeGraph.soordertype.Insert(sROrderType);
                orderTypeGraph.operations.Insert(ourGrid);
                orderTypeGraph.soordertype.Update(sROrderType);
                orderTypeGraph.operations.Update(ourGrid);

                orderTypeGraph.Save.Press();
                WriteLog(string.Format("{0} type has been added", newOrderTypeName));
            }
        }

Acumatica trace:

8/6/2021 6:27:14 PM Error:
Publishing of a customization project failed with an error CustomizationProjects:TestAssignment2
PX.Data.PXOuterException: Error: Inserting  'Order Type' record raised at least one error. Please review the errors.

   at PX.Data.PXUIFieldAttribute.CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
   at PX.Data.PXCache.OnCommandPreparing(String name, Object row, Object value, PXDBOperation operation, Type table, FieldDescription& description)
   at PX.Data.PXCache`1.PersistInserted(Object row, Boolean bypassInterceptor)
   at PX.Data.PXCache`1.Persist(PXDBOperation operation)
   at PX.Data.PXGraph.Persist(Type cacheType, PXDBOperation operation)
   at PX.Data.PXGraph.Persist()
   at PX.Data.PXSave`1.d__2.MoveNext()
   at PX.Data.PXAction`1.d__33.MoveNext()
   at PX.Data.PXAction`1.d__33.MoveNext()
   at PX.Data.PXAction`1.PressImpl(Boolean internalCall, Boolean externalCall)
   at PX.Data.PXAction`1.Press()
   at TestAssignment2.ServiceRepairCreator.UpdateDatabase() in D:\AcumaticaFolder\TestAssignment\App_Data\Projects\TestAssignment2\TestAssignment2\ServiceRepairCreator.cs:line 58
   at Customization.CstWebsiteStorage.UpdateDatabaseFromPlugin(CustomizationPlugin plugin, PXPublishOptions options)
   at Customization.CstWebsiteStorage.RunPluginsDbUpdate()
   at PX.Web.Customization.Controls.Publish.RunPlugins(HttpResponse response)

Solution

  • The problem was in mandatory fields. This method works when u need to create a new order type. Thanks, everyone, for your help.

    public override void UpdateDatabase()
            {
                SOOrderTypeMaint graph = PXGraph.CreateInstance<SOOrderTypeMaint>();
                string newOrderTypeName = Constants.serviceRepairOrderType;
                var existingOrderType = graph.soordertype.Search<SOOrderType.orderType>(newOrderTypeName);
                if (existingOrderType.Count != 0)
                {
                    WriteLog(string.Format("{0} type already exist", newOrderTypeName));
                }
                else
                {
                    SOOrderType exempleOrderType = SOOrderType.PK.Find(graph, SOOrderTypeConstants.SalesOrder);
                    
                    var soSRTypeGeneral = graph.soordertype.Insert();
                    soSRTypeGeneral.OrderType = newOrderTypeName;
                    soSRTypeGeneral.Descr = "description";
                    soSRTypeGeneral.Template = exempleOrderType.Template;
                    soSRTypeGeneral.Behavior = exempleOrderType.Behavior;
                    soSRTypeGeneral.ARDocType = exempleOrderType.ARDocType;
                    soSRTypeGeneral.INDocType = exempleOrderType.INDocType;
                    soSRTypeGeneral.FreightSubID = exempleOrderType.FreightSubID;
                    soSRTypeGeneral.OrderPlanType = exempleOrderType.OrderPlanType;
                    soSRTypeGeneral.DiscountSubID = exempleOrderType.DiscountSubID;
                    soSRTypeGeneral.FreightAcctID = exempleOrderType.FreightAcctID;
                    soSRTypeGeneral.DiscountAcctID = exempleOrderType.DiscountAcctID;
                    soSRTypeGeneral.ShipmentPlanType = exempleOrderType.ShipmentPlanType;
                    soSRTypeGeneral.OrderNumberingID = exempleOrderType.OrderNumberingID;
                    soSRTypeGeneral.DefaultOperation = exempleOrderType.DefaultOperation;
                    soSRTypeGeneral.DaysToKeep = exempleOrderType.DaysToKeep;
                    soSRTypeGeneral.COGSSubMask = exempleOrderType.COGSSubMask;
                    soSRTypeGeneral.DiscSubMask = exempleOrderType.DiscSubMask;
                    soSRTypeGeneral.OrderPriority = exempleOrderType.OrderPriority;
                    soSRTypeGeneral.FreightSubMask = exempleOrderType.FreightSubMask;
                    soSRTypeGeneral.RequireShipping = exempleOrderType.RequireShipping;
                    soSRTypeGeneral.DiscAcctDefault = exempleOrderType.DiscAcctDefault;
                    soSRTypeGeneral.COGSAcctDefault = exempleOrderType.COGSAcctDefault;
                    soSRTypeGeneral.SupportsApproval = exempleOrderType.SupportsApproval;
                    soSRTypeGeneral.AllowQuickProcess = exempleOrderType.AllowQuickProcess;
                    soSRTypeGeneral.FreightAcctDefault = exempleOrderType.FreightAcctDefault;
                    var soSRTypeTemplate = graph.operations.Insert();
                    soSRTypeTemplate.OrderType = soSRTypeGeneral.OrderType;
                    soSRTypeTemplate.Active = true;
                    soSRTypeTemplate.InvtMult = -1;
                    soSRTypeTemplate.Operation = "I";
                    soSRTypeTemplate.INDocType = "INV";
                    soSRTypeTemplate.OrderPlanType = "60";
                    soSRTypeTemplate.ShipmentPlanType = "61";
                    var soSRTypeQuickParam = graph.quickProcessPreset.Insert();
                    soSRTypeQuickParam.OrderType = soSRTypeGeneral.OrderType;
                    soSRTypeQuickParam.UpdateIN = true;
                    soSRTypeQuickParam.CreateShipment = true;
                    soSRTypeQuickParam.PrepareInvoice = true;
                    soSRTypeQuickParam.ConfirmShipment = true;
                    graph.Persist();
                    WriteLog(string.Format("{0} type has been added", newOrderTypeName));
                }
            }