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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to iterate data contract class in D365fo

(0) ShareShare
ReportReport
Posted on by 30

Hi All,

How could i iterate the data contract class ?

MyDataContractClass myDataContractClass = FormJsonSerializer::deserializeObject(classNum(MyDataContractClass),responseJson);

[DataContractAttribute]
class MyDataContractClass
{
    String50        invoiceId, profBillId, status, message;

    [DataMemberAttribute('proformaId')]
    public String50 parmProfBillId(String50 _profBillId = profBillId)
    {
        profBillId = _profBillId;
        return profBillId;
    }

    [DataMemberAttribute('invoiceId')]
    public String50 parmInvoiceId(String50 _invoiceId = invoiceId)
    {
        invoiceId = _invoiceId;
        return invoiceId;
    }

    [DataMemberAttribute('status')]
    public String50 parmStatus(String50 _status = status)
    {
        status = _status;
        return status;
    }

    [DataMemberAttribute('error')]
    public String50 parmMessage(String50 _message = message)
    {
        message = _message;
        return message;
    }

}
 

Thanks,

Masha

  • Sergei Minozhenko Profile Picture
    23,099 on at

    Hi Masha,

    Do you want to iterate over parm methods from contract class?

  • it.dev Profile Picture
    30 on at

    Hi Sergei,

    yeah , i tried to deserialized the response json i get to data contract class and then want to iterate that.

    Thanks,

    Masha

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,099 on at

    Hi Masha,

    You can use Dict* classes to iterate over members of the contract class. But what is the purpose of that iteration? You have only 4 members in your class.

    SysDictClass contractDictClass = new SysDictClass(your contract class id);
    
    Set methods = contractDictClass.methods(true, false, true);
    
    SetEnumerator methodsEnumerator = methods.getEnumerator();
    
    while(methodsEnumerator.moveNext())
    {
        DictMethod dictMethod = methodsEnumerator.current();
    
        DataMemberAttribute dataMember = dictMethod.getAttribute(classStr(DataMemberAttribute));
    
        if (dataMember)
        {
            // Do something
        }
    }

  • it.dev Profile Picture
    30 on at

    Hi sergei,

    for the example, i get this json response

    [
        {
            "proformaId": "XXX",
            "invoiceId": "YYY",
            "error": "Unit not exists",
            "status": "failed"
        },
        {
            "proformaId": "ZZZ",
            "invoiceId": "AAA",
            "status": "success"
        }
    ]

    and want to iterate that into my data contract class attribute

    Thanks,

    Masha

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,099 on at

    Hi Masha,

    So you have several entries in JSON and want them all to be converted to contract. In this case, you need to use deserializeCollection method

    List collection = FormJsonSerializer::deserializeCollection(classNum(List), json_string, types::Class, classStr(MyContract));

  • it.dev Profile Picture
    30 on at

    Hi Sergei,

    Thanks a lot, really appreciate for your help

    Masha

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 515 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 417 Super User 2026 Season 2

#3
CU10121822-0 Profile Picture

CU10121822-0 360

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans