Wednesday, July 26, 2023

Export of vendor payment journal info message to JSON format in D365 X++

 My requirement is to export the vendor payment journal to JSON format through custom button.

And to generate the response in JSON file based on validations occured.

Custom button clicked Event Handler :

internal final class DaxLedgerJournalTable_EventHandler

{

   /// </summary>

   /// <param name="sender"></param>

   /// <param name="e"></param>

   [FormControlEventHandler(formControlStr(LedgerJournalTable, Json), FormControlEventType::Clicked)]

   public static void Json_OnClicked(FormControl sender, FormControlEventArgs e)

   {

       MenuFunction                    menuFunction;

       FormRun formRun                 = sender.formRun();

       FormDataSource                  LedgerJournalTable_ds = formRun.dataSource(1);

       LedgerJournalCheckpost          _ledgerJournalCheckpost;

       str                             message,msg,info,setvalue;

       container                       con;

       Set                             setOne;

       SetEnumerator                   enumerator;

       setOne                          = new Set(types::String);

       LedgerJournalTable              ledgerJournalTable;

       ledgerJournalTable              = LedgerJournalTable_ds.cursor();

       LedgerJournalTrans              ledgerJournalTrans;

       System.IO.StringWriter          stringWriter;

       Newtonsoft.Json.JsonTextWriter  jsonWriter;

       stringWriter                    = new System.IO.StringWriter();

//validation

       ledgerJournalTable              = LedgerJournalTable::find(ledgerjournaltable.JournalNum);

       _LedgerJournalCheckPost         = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::No);

       _LedgerJournalCheckPost.run();

       message                         = _LedgerJournalCheckPost.parmErrorMsg();

       if(message)

       {

           msg             = Global::parmSysInfologStr();

       }

       info                = Global::parminfo();

       if(info != "Journal is OK.")

       {

           con = str2con(info);

       }

       setOne.add(message);

       setOne.add(msg);

       enumerator = setOne.getEnumerator();

       while (enumerator.moveNext())

       {

           setvalue += enumerator.current();

       }

       con = conIns(con,1,setvalue);

       jsonWriter                      = new Newtonsoft.Json.JsonTextWriter(stringWriter);

       jsonWriter.WriteStartObject();// add '{'

       jsonWriter.WritePropertyName("Journal Num");

       jsonWriter.WriteValue(ledgerJournalTable.JournalNum);

       jsonWriter.WritePropertyName("Journal Name");

       jsonWriter.WriteValue(ledgerJournalTable.JournalName);

       jsonWriter.WritePropertyName("Lines");

       jsonWriter.WriteStartArray();

       while select ledgerJournalTrans

           where ledgerJournalTrans.JournalNum == ledgerjournaltable.JournalNum

       {

           jsonWriter.WriteStartObject();// add '{'

           jsonWriter.WritePropertyName("Voucher");

           jsonWriter.WriteValue(ledgerJournalTrans.Voucher);

           jsonWriter.WritePropertyName("Vendor Name");

           jsonWriter.WriteValue(ledgerJournalTrans.LedgerDimensionName);

           jsonWriter.WriteEndObject(); // add '}'

       }

       jsonWriter.WriteEndArray(); //add ']'

       jsonWriter.WriteEndObject(); //add '}'

       if(info == "Journal is OK." && !msg && !message)

       {

           jsonWriter.WriteStartObject();

           jsonWriter.WritePropertyName("Response");

           jsonWriter.WriteStartArray();

           jsonWriter.WriteStartObject();// add '{'

           jsonWriter.WritePropertyName("Status");

           jsonWriter.WriteValue("Success");

           jsonWriter.WritePropertyName("Remark");

           jsonWriter.WriteValue(info);

       }

       else

       {

           jsonWriter.WriteStartObject();

           jsonWriter.WritePropertyName("Response");

           jsonWriter.WriteStartArray();

           jsonWriter.WriteStartObject();// add '{'

           jsonWriter.WritePropertyName("Status");

           jsonWriter.WriteValue("Failed");

           jsonWriter.WritePropertyName("Remark");

           jsonWriter.WriteValue(con2Str(con));

       }

       jsonWriter.WriteEndObject();

       jsonWriter.WriteEndArray();

       jsonWriter.WriteEndObject();

       info(strFmt("%1,",stringWriter.ToString()));

       //info(strFmt("my message %1", message)); //Warnings

       //info(strFmt("my msg %1", msg)); //warnings

       //info(strFmt("my info %1", info)); // info's

   }

}

Code to get warnings and info messages :

[ExtensionOf(classStr(Global))]

static final class DaxGlobal_Extension

{

   public static SysInfologStr  message;

   public static SysInfologStr  info;

   static boolean checkFailed(SysInfoLogStr txt, URL helpURL, SysInfoAction _sysInfoAction)

   {

       boolean     ret;

       ret = next checkFailed(txt,helpURL,_sysInfoAction);

       Global::parmSysInfologStr(txt);

       return ret;

   }

   static SysInfologStr parmSysInfologStr(SysInfologStr  _message = message)

   {

       message = getPrefix()+_message;

       return message;

   }

   static SysInfologStr parminfo(SysInfologStr  _info = info)

   {

       info = getPrefix()+_info;

       return info;

   }

   static Exception info(SysInfoLogStr txt, URL helpUrl, SysInfoAction _sysInfoAction)

   {

       boolean     ret;

       ret =next info(txt,helpUrl,_sysInfoAction);

       Global::parminfo(txt);

       return ret;

   }

}

Code to get the current record validation message :

[ExtensionOf(classStr(LedgerPostingMessageLog))]

internal final class DaxLedgerPostingMessageLog_Extension

{

   public SysInfologStr message;

   public boolean logCheckFailed(SysInfologStr _message, URL _helpURL)

   {

       boolean ret;

       ret= next logCheckFailed(_message,_helpURL);

       this.parmSysInfologStr(_message);

       return ret;

   }

   public SysInfologStr parmSysInfologStr(SysInfologStr  _message = message)

   {

       message = _message;

       return message;

   } 

}

Code to get the above class message in ledgerjournalcheckpost for use in custom button :

[ExtensionOf(classstr(LedgerJournalCheckPost))]

internal final class Dax_LedgerJournalCheckpost_Extension

{

   public SysInfologStr message;

   protected boolean checkJournalStatus()

   {

       boolean ret;

       ret = next checkJournalStatus();

       message = getPrefix() + ledgerPostingMessageCollection.parmSysInfologStr();

       return ret;

   }

   public SysInfologStr parmErrorMsg()

   {

       return message;

   }

}


Output :





Other Simplest way to get Info Message is :

Validation(info message)  is saved in Log field of Ledger Journal Table.











No comments:

Post a Comment