Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

changing contract class

(1) ShareShare
ReportReport
Posted on by 1,167
Hi,

I have a custom service that accepts this contract class as an input
{
    "_input": 
    {
        "header": 
        {
            "dataArea": "A1",
            "mainId": "1",
            "line": 
            [
                {
                   
                    "fieldA": 1,
                    "id": "Id1"
                },
		{
                   
                    "fieldA": 2,
                    "id": "Id2"
                },
                {
                   
                    "fieldA": 3,
                    "id": "Id1"
                }
            ] 
        }
    }
}

After accepting this contract as an input to the custom service, how can i add x++ code to convert it to the format below? ( i want to group the ids in lines)
{
    "_input": 
    {
        "header": 
        {
            "dataArea": "A1",
            "mainId": "1",
            "ids": 
            [
                {
                    "id": "id1",
                    "Line":
                    [
                        {
                            "fieldA": 1
                        },
                        {
                            "fieldA": 3
                        }
                    ]
                },
                {
                    "id": "id2",
                    "line":
                    [
                        {
                            "fieldA": 2
                        }
                    ]
                }
            ]
        }
    }
}
 
  • Martin Dráb Profile Picture
    Martin Dráb 230,503 Most Valuable Professional on at
    changing contract class
    My point is that the code you showed is irrelevant to your question. Forget JSON and focus on objects in X++. Please show us your contract classes instead and explain where you got stuck when trying to write your X++.
     
    The first thing you'll need is iterating line contract objects. Do you know how to iterate elements of a list or do you need our help with that too?
  • .. Profile Picture
    .. 1,167 on at
    changing contract class
    Hi Martin,
     
    Yes I mean the contract class i have represents this Json.
     
    However, i want to re-arrange the received contract to the 2nd format. How can i do that? How can i group the first contract by Ids to achieve the 2nd format? Do i need to use maps for example an what to do exactly?
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,503 Most Valuable Professional on at
    changing contract class
    These are JSON strings, not contract classes. You'll define a contract class for the input JSON, use it as a parameter of your custom service and what you'll receive is an instance of your contract class populated with the data from the JSON string. You'll not receive the JSON string and therefore you don't need to worry at all about how to parse it or convert it in X++. If you want take data from your contract object and put it to another object, it's just normal object-oriented programming.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,431 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans