x++ code to create call center order for a sales order in d365 f&o

 class BAS_CallCenterOrderCreation

{

    /// <summary>

    /// Runs the class with the specified arguments.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    public static void main(Args _args)

    {

        SalesOrderHeaderV2Entity soHeaderEntity;

        SalesOrderLineV2Entity   soLineEntity;

        MCRCustPaymTable         mcrCustPaymTable;

        // Sales Order Header Creation

        soHeaderEntity.initValue();

        soHeaderEntity.OrderingCustomerAccountNumber = '004003';

        soHeaderEntity.insert();

        // Getting saleId to create lines

        SalesId salesId = soHeaderEntity.SalesOrderNumber;

        // Sales Order Line Creation

        soLineEntity.initValue();

        soLineEntity.SalesOrderNumber     = salesId;

        soLineEntity.ItemNumber           = '0001';

        soLineEntity.OrderedSalesQuantity = 1;

        soLineEntity.insert();

        Info(strFmt("Sales order %1 has been created",soHeaderEntity.SalesOrderNumber));

 

        SalesTable         salesTableBuf = SalesTable::find(salesId);

        CurrencyCode       currency = 'USD';

        str                paymentType = 'Cash';

        MCRCustPaymStatus  mcrCustPaymStatus = MCRCustPaymStatus::NotSubmitted;

        SalesTotals        salesTotals       = salesTotals::construct(salesTableBuf,SalesUpdate::All);

        real               totalAmount = salesTotals.totalAmount();


        info(Strfmt("Net Amount %1",salesTotals.totalAmount()));

        try

        {

            ttsbegin;

            mcrCustPaymTable.clear();

            mcrCustPaymTable.Channel        = salesTableBuf.retailSalesTable().RetailChannel;

            mcrCustPaymTable.CurrencyCode   = currency;

            mcrCustPaymTable.TenderTypeId   = '1';

            mcrCustPaymTable.RefTableId     = salesTableBuf.TableId;

            mcrCustPaymTable.RefRecId       = salesTableBuf.RecId;

            mcrCustPaymTable.CustAccount    = salesTableBuf.CustAccount;

            mcrCustPaymTable.CardTypeId     = 'Cash';

            mcrCustPaymTable.Status         = mcrCustPaymStatus;

            mcrCustPaymTable.CustPaymType   = MCRCustPaymTable::getMCRTypeFromTender(mcrCustPaymTable.TenderTypeId,

                mcrCustPaymTable.Channel,mcrCustPaymTable.CardTypeId);


            switch (mcrCustPaymStatus)

            {

                case MCRCustPaymStatus::NotSubmitted:

                case MCRCustPaymStatus::Authorized:


                    mcrCustPaymTable.Amount = CurrencyExchange::round(totalAmount, currency);

                    break;

            }


            mcrCustPaymTable.insert();


            ttscommit;

            MCRSalesOrderTotals mcrSalesOrderTotals = new MCRSalesOrderTotals(salesTableBuf, true);

            //complete

            MCREndOrder::endOrder(salesTableBuf.SalesId, true, mcrSalesOrderTotals, false);

        }

        catch

        {

            RetailTracer::Error('RetailTransactionPaymentsHelper', funcName(), strFmt('Error during Call Center payment (MCRCustPaymTable) creation:\nSalesId [$%1]\n', salesTableBuf.RecId));

        }

        //Updating sales order by SalesFromLetter class

        SalesFormLetter salesFormLetter;

        //Posting SO Confirmation,I guess its mandatory

        //You cannot do invoice without doing SO confirm

        salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);

        salesFormLetter.update(SalesTable::find(salesId));

        //Posting Packing slip

        salesFormLetter = SalesFormLetter::construct(DocumentStatus::PackingSlip);

        salesFormLetter.update(SalesTable::find(salesId));

        info("Packing slip posted");

        //Posting SO Invoice

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

        salesFormLetter.update(SalesTable::find(salesId));

        info("Invoice posted");

    }


}

Comments

Popular posts from this blog

How to Create a wizard in x++ d365

how to post trade agreement journals automatically using x++ code

X++ code to CREATE AND POST A TRADE AGREEMENT IN MICROSOFT DYNAMICS 365 in D365 F & O