Skip to main content

Notifications

Announcements

No record found.

Constructing JSON String with array of values in D365FO

Hi,

In this post, we will create a JSON string that can have array of values. Table CustTable is chosen from which data is extracted and its contact information is provided in the form of array.

class CG_BuildJSONWithArrayValues
{       
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        System.IO.StringWriter          stringWriter;
        Newtonsoft.Json.JsonTextWriter  jsonWriter;
        CustTable                       custTable;
        LogisticsElectronicAddress      address;
        DirPartyLocation                dirPartyLocation;
        LogisticsLocation               location;
        DirPartyTable                   dirPartyTable;
        LogisticsLocationRole           locationRole;
        LogisticsElectronicAddressRole  addressLocationRole;
 
        stringWriter       = new System.IO.StringWriter();
        jsonWriter         = new Newtonsoft.Json.JsonTextWriter(stringWriter);
 
        select firstonly AccountNum, BankAccount, Party
            from custTable
                where custTable.AccountNum == "1000"
                join dirPartyTable
            where dirPartyTable.RecId == custTable.Party;
 
        str sJSON = "";
        jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("AccountNum");
        jsonWriter.WriteValue(custTable.AccountNum); 
        jsonWriter.WritePropertyName("Name");
        jsonWriter.WriteValue(custTable.nameAlias()); 
        jsonWriter.WritePropertyName("BankAccount");
        jsonWriter.WriteValue(custTable.BankAccount);  
        jsonWriter.WritePropertyName("Contact Information");
        jsonWriter.WriteComment("Type - Description - Value");
        jsonWriter.WriteStartArray();
        while select Location, RecId from dirPartyLocation
            where dirPartyLocation.Party == dirPartyTable.RecId
            join RecId from location
                where location.RecId == dirPartyLocation.Location
                join address
                    where address.Location == location.RecId
                    join RecId from addressLocationRole
                            where (addressLocationRole.ElectronicAddress == address.RecId)
                            join Name from locationRole
                                where ((addressLocationRole.LocationRole == locationRole.RecId)
                                    && (locationRole.IsContactInfo == NoYes::Yes))
        {
            jsonWriter.WriteValue(strFmt("%1 - %2 - %3", address.Type, address.Description, address.Locator));
        }
        jsonWriter.WriteEndArray();
        jsonWriter.WriteEndObject();
 
        sJSON = stringWriter.ToString();
        Info(strFmt("%1", stringWriter.ToString()));
    }
 
}

Output: https://drive.google.com/open?id=1qCkdVDWU7czKtuZtSLk4UMk3MDPpq5Ez

Regards,

Chaitanya Golla

Comments

*This post is locked for comments