X++ code to get /Print error messages in D365FO of any operations

 This code will help us to fetch /get inner exceptions or errors 

1. To get error messages 

class BASMSGetErrorJob

{

    /// <summary>

    /// Class entry point. The system will call this method when a designated menu 

    /// is selected or when execution starts and this class is set as the startup class.

    /// </summary>

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

    public static void main(Args _args)

    {

    SysInfoLogEnumerator        infoLogEnum;

    SysInfologMessageStruct     infoMessageStruct;

    List                        errorList = new List(Types::String);

          try

        {

       //Logic 

 //Declaration of data entities

            SalesOrderHeaderV2Entity salesTable;

            SalesOrderLineV2Entity salesLine;


            ttsbegin;

            //Sales order header creation

            salesTable.initValue();

            salesTable.OrderingCustomerAccountNumber = "013805";

            salesTable.insert();

            //004056


            //Getting saleId to create lines

            SalesId salesId = salesTable.SalesOrderNumber;


            //Sales order line creation

            salesLine.initValue();

            salesLine.SalesOrderNumber = salesId;

            salesLine.ItemNumber = "0002";

            salesLine.OrderedSalesQuantity = 5;

            salesLine.ShippingSiteId = "Central";

            salesLine.ShippingWarehouseId = "DC-Central";

            salesLine.insert();

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

        }

        catch(Exception::Error)

            {

                infoLogEnum   = SysInfoLogEnumerator::newData(infolog.infologData());

                while(infoLogEnum.moveNext())

                {

                    infoMessageStruct      = SysInfologMessageStruct::construct(infoLogEnum.currentMessage());

                    str integrationMessage = infoMessageStruct.message();

//You can use info to print error msg

                    errorList.addEnd(integrationMessage);

                                 }

            }

    }


}

2. Exceptions :- 

class BASMSGetErrorMsgJob

{

    /// <summary>

    /// Class entry point. The system will call this method when a designated menu

    /// is selected or when execution starts and this class is set as the startup class.

    /// </summary>

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

    public static void main(Args _args)

    {

        System.Exception ex;


        try

        {

            //Declaration of data entities

            SalesOrderHeaderV2Entity salesTable;

            SalesOrderLineV2Entity salesLine;


            ttsbegin;

            //Sales order header creation

            salesTable.initValue();

            salesTable.OrderingCustomerAccountNumber = "013805";

            salesTable.insert();

            //004056


            //Getting saleId to create lines

            SalesId salesId = salesTable.SalesOrderNumber;


            //Sales order line creation

            salesLine.initValue();

            salesLine.SalesOrderNumber = salesId;

            salesLine.ItemNumber = "0002";

            salesLine.OrderedSalesQuantity = 5;

            salesLine.ShippingSiteId = "Central";

            salesLine.ShippingWarehouseId = "DC-Central";

            salesLine.insert();

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


            ////Updating sales order by SalesFromLetter class

            //SalesFormLetter salesFormLetter;

            //SalesTable salesUpate = SalesTable::find(salesId);

            ////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");

            ttscommit;


        }

        catch(ex)

        {

            System.String messageEx = ex.Message;

            warning(strFmt("There is an error %1",messageEx));

        }

    }


}

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 submit and approve and reject the invent movement workflow in d365 F&O