Create Service Order from Sales Order using X++

Dynamics 365 for finance and operations which is earlier known as Dynamics AX is nothing but Microsoft’s flagship ERP solution. Small, medium and large companies can use this ERP suite to become efficient and to streamline their processes. It is an application that helps companies stay flexible. There are so many excellent features in this particular software that will help users become a lot more efficient than they were before. 

Since it is a cloud-based, and all the changes that occur in it are real-time. Teams can work with team members that are working across the world with ease. New users might struggle with a few aspects such as creating service orders from the sales order when they are using X++.

In this blog article, we will see how we can create Service Order from Sales Order using X++ in Dynamics 365 Operations. I have created a button in Sales Order form which will run a class (MenuItem – Action) to create Service Order Header.

Create a new Class:

class CFSServiceOrderCreateFromSalesOrder
{
    SMAServiceOrderTable serviceOrderTable;
    SMAServiceOrderLine serviceOrderLine;
    SMAserviceTaskRelation serviceTaskRelation;
    SalesTable salesTable;
}

Create a main method:

public static void main(Args _args)
    {       
        Args serviceOrderTableArgs = new Args();
        CFSServiceOrderCreateFromSalesOrder serviceOrderCreateFromSalesOrder = CFSServiceOrderCreateFromSalesOrder::construct();               
       
        ttsbegin;

        //Call to method createSMAServiceOrder() for Service Order Header creation
        serviceOrderTable = serviceOrderCreateFromSalesOrder.createSMAServiceOrder(_args.record());
        ttscommit;

        //Infolog to display service order id if service order created else failure message
        if(serviceOrderTable)
        info(strFmt("ServiceOrderHeader created with ID: %1", serviceOrderTable.ServiceOrderId));
        else
        info("ServiceOrderHeader creation failed");
    }

Create SMAServiceOrder() method: This method is used to create a Service Order header record.

public SMAServiceOrderTable createSMAServiceOrder(SalesLine _salesLine)
    {
        
        //initialize SMAServiceOrderTable
        serviceOrderTable.initvalue();

        //Initialize Service Order ID
        NumberSeq NumberSeq;
        NumberSeq = NumberSeq::newGetNum(SMAParameters::numRefServiceOrderId(),true);

        serviceOrderTable.ServiceOrderId = NumberSeq.num();

        serviceOrderTable.CustAccount = _salesLine.custAccount;

        serviceOrderTable.ProjId = _salesLine.ProjID;
        
        //display Customer Name as Service Order Description
        serviceOrderTable.Description = CustTable::find(_salesLine.CustAccount).name(); 
 
        //insert Service Address
        serviceOrderTable.updateCustAddress();

        serviceOrderTable.insert();
        
        return serviceOrderTable;
    }

So, this will create a Service Order Header from Sales Order. Let me know your reviews. I will soon come up with more articles, as I further explore D365 Operations.

 


Share Story :