Sunday, 17 October 2021

D 365 FO to create Sales order from X++ Code

 Hi,

the below x++ code will help you to create the Sales Order from the code behind without any form or pop up this code can be used for creating the Sales Order for the Integration forms.


class RAM_CreateSalesOrderFromCodeBehind

{

    public static void main(Args _args)

    {        

        SalesTable salesTable;

        SalesLine salesLine;

        InventTable inventTable;

        InventDim inventDim;

        CustTable custTable;

        CustAccount custAccount;

        NumberSeq numberSeq;

        SalesId salesID;

        str warehouse;

        try

        {

            ttsbegin;

            salesTable.clear();


            numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());

            numberSeq.used();

salesID = numberSeq.num();

            salesTable.SalesId = salesID;

            

            salesTable.initValue();

            

            if(CustTable::find([Customer ID]))

            {

                salesTable.CustAccount = [Customer ID];

            }

            else

            {

                info(strFmt("Customer account %1 doesn't exist.", [Customer ID]));

            }


            

            salesTable.initFromCustTable();

            if(InventLocation::find([warehouse ID]).InventLocationId != "")

            {

                salesTable.InventSiteId = InventLocation::find(warehouse).InventSiteId;

                salesTable.InventLocationId = inventlocation::find(warehouse).InventLocationId;

            }

            salesTable.insert();

            try

            {

                inventTable.clear();

                inventDim.clear();

                salesLine.clear();


                select * from inventTable

                    where inventTable.itemId == [Item ID];

                    

                salesLine.clear();

                salesLine.SalesId = salesID;

                salesLine.ItemId = inventTable.ItemId;

                salesLine.itemIdChanged();


                

                salesLine.initFromInventTable(InventTable::find(salesLine.ItemId));


                if(Inventlocation::find(warehouse).InventLocationId != "")

                {

                    inventdim.InventSiteId = InventLocation::find(warehouse).InventSiteId;

                    inventdim.InventLocationId = Inventlocation::find(warehouse).InventLocationId;

                }

                salesLine.InventDimId = InventdIm::findOrCreate(inventDim).inventDimId;


                salesLine.createLine(NoYes::Yes, // Validate

                NoYes::Yes, // initFromSalesTable

                NoYes::No, // initFromInventTable

                NoYes::Yes, // calcInventQty

                NoYes::Yes, // searchMarkup

                NoYes::Yes); //


                //Set the values as per your scenario

                salesLine.SalesPrice = [Item Sales Price];

                salesLine.SalesQty = [Qty];

                salesLine.LineDisc = [Line discount];

                salesLine.LineAmount= salesLine.calcLineAmount();

                salesLine.update();

                ttscommit;

            }

            catch(Exception::Error)

            {

                ttsabort;

            }


            

            SalesFormLetter formLetterObj;

            formLetterObj = SalesFormLetter::construct(DocumentStatus::Invoice);

            formLetterObj.update(SalesTable::find(salesID));

            

            info(strFmt("Sales order created with Sales ID: %1",salesID));

        }

        catch(Exception::Error)

        {

            ttsabort;

        }

    }

}


Keep DAXing :)

No comments: