› Dynamics 365, Enterprise › Create Service Order from Sales Order using X++

Create Service Order from Sales Order using X++

 Sohagani Shah   Leave a comment

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.

 


Liked it? Share it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.