web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Serialize Json with data from an dataContract class in D365FO

Chaitanya Golla Profile Picture Chaitanya Golla 17,225

Hi,

In this post we will see the process of serializing the data to Json string from a data contract class. Developed a new data-contract class by name CG_CustomerDataContract which hosts the AccountNum and CustomerGroupId fields from table "CustTable". 

DataContract Class: CG_CustomerDataContract 

 [DataContractAttribute]

class CG_CustomerDataContract
{
    CustAccount custAccount;
    CustGroupId custGroupId;
 
    [
        DataMemberAttribute('CustAccount'),
        SysOperationLabelAttribute(literalstr("Customer")),
        SysOperationHelpTextAttribute(literalstr("Customer account"))
    ]
    public CustAccount parmCustAccount(CustAccount _custAccount = custAccount)
    {
        custAccount = _custAccount;
        return custAccount;
    }
 
    [
        DataMemberAttribute('custGroupId'),
        SysOperationLabelAttribute(literalstr("Customer group")),
        SysOperationHelpTextAttribute(literalstr("Customer groupId"))
    ]
    public CustGroupId parmCustGroupId(CustGroupId _custGroupId = custGroupId)
    {
        custGroupId = _custGroupId;
        return custGroupId;
    }
 
}

Runnable Class: CG_SerializeJsonFromDataContract
class CG_SerializeJsonFromDataContract
{       
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {     
        CustTable               custTable;
        CG_CustomerDataContract dataContract = new CG_CustomerDataContract();
        str                     json;
 
        select firstonly custTable
            where custTable.AccountNum == "1000";
        dataContract.parmCustAccount(custTable.AccountNum);
        dataContract.parmCustGroupId(custTable.CustGroup);
 
        json = FormJsonSerializer::serializeClass(dataContract);       
 
        info(strFmt("%1", json));
    }
 
}

Output: https://drive.google.com/open?id=1xMOaJr35PY3WRFm_TjdGj-1jgTglBryo

Regards,

Chaitanya Golla

Comments

*This post is locked for comments