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 get a certain JSON format?

(0) ShareShare
ReportReport
Posted on by 465

Hi

let's say i selected two sales orders

First one: salesId = 1, AccountNum = 01 with two lines (item = A , Qty =1, Item = B, Qty = 2)

Second one: salesId = 2, AccountNum = 02 with 1 line (item = C , Qty = 5)

I want to convert this to JSON like this:

{
  "SalesHeaderContract": 
  [
    {
      "AccountNum": "01",
      "SalesOrderLinesContract": [
        {
          "ItemId": "A",
          "Qty": 1.0
        },
        {
          "ItemId": "B",
          "Qty": 2.0
        }
      ],
      "SalesId": "1"
    },
    {
      "AccountNum": "02",
      "SalesOrderLinesContract": [
        {
          "ItemId": "C",
          "Qty": 5.0
        }
      ],
      "SalesId": "2"
    }
  ]
}

But here's what i'm getting (lines are repeating in each one)

{
  "SalesHeaderContract": 
  [
    {
      "AccountNum": "01",
      "SalesOrderLinesContract": [
        {
          "ItemId": "A",
          "Qty": 1.0
        },
        {
          "ItemId": "B",
          "Qty": 2.0
        },
        {
          "ItemId": "C",
          "Qty": 5.0
        }
      ],
      "SalesId": "1"
    },
    {
      "AccountNum": "02",
      "SalesLinesContract": [
        {
          "ItemId": "A",
          "Qty": 1.0
        },
        {
          "ItemId": "B",
          "Qty": 2.0
        },
        {
          "ItemId": "C",
          "Qty": 5.0
        }
      ],
      "SalesId": "2"
    }
  ]
}

Here's what i did:

MultiSelectionHelper selection = MultiSelectionHelper::createFromCaller(_args.caller());
SalesTable                      salesTable = selection.getFirst();
SalesLine                       salesLine;
SalesHeaderContract             salesHeaderContract; //it has salesId,AccountNum and List of SalesLinesContract
SalesLinesContract              salesLinesContract; // it has item and quantity
SalesHeaderContractSecond       salesHeaderContractSecond = new SalesHeaderContractSecond(); // it has list of SalesHeaderContract
List                            listHeader = new List(Types::Class);
List                            listLines = new List(Types::Class);

while (salesTable)
{
    salesHeaderContract= new SalesHeaderContract();
    salesHeaderContract.SalesId(salesTable.SalesId);
    salesHeaderContract.CustAccount(salesTable.CustAccount);
    listHeader.addEnd(salesHeaderContract); 
    while select salesLine where salesLine.SalesId==salesTable.SalesId
    {
        salesLinesContract = new mzSalesOrderLinesContract();
        salesLinesContract.ItemId(salesLine.ItemId);
        salesLinesContract.Qty(salesLine.QtyOrdered);
        listLines.addEnd(mzSalesOrderLinesContract);
        salesHeaderContract.SalesLinesContract(listLines);
    }
    salesHeaderContractSecond.SalesHeaderContract(listHeader);
    salesTable = selection.getNext();
}

 str msg = FormJsonSerializer::serializeClass(salesHeaderContractSecond);
 File::SendStringAsFileToUser(msg,"file");

How can i change this code to make it work?

I have the same question (0)
  • nmaenpaa Profile Picture
    101,166 Moderator on at

    Could you perhaps share with us what problems you have with your code? This way it's much easier for us to help. Thanks!

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

    Hi D365FO user,

    You need to move the initialization of "listLines" list before "while select salesLine" statement, but inside "while (salesTable)" statement. At the moment you don't clean this list for a new order and each next order will have all lines from previous orders.

  • D365FO user Profile Picture
    465 on at

    Hi Nicolas, i already wrote the code and i mentioned that the problem i was facing was that lines are getting repeated.

    Thanks Sergie, it works now :)

    Last question: Is there a way to do it without making 2 header contracts as i did? can i do it with only one contract header class?

  • nmaenpaa Profile Picture
    101,166 Moderator on at

    Hi D365FO user, yes I saw the code, just wanted to ask what problems you have with it. But perhaps you get help to your problem even without describing the problem :) Thanks to Sergei.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 468 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 333 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans