Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to handle or retri...
Finance forum
Answered

How to handle or retrieve data contract with list ?

Posted on by 65
Hello expert,
 
I have data contract of Header and Detail, lets say Header class name is MyHeader and Detail class is MyDetail, which is as normal the detail is declared in Header data contract as a list just at the bottom of the Header data contract, something like this :
 
[DataMember(/myDetails/),
    DataCollection(Types::Class, classStr(myDetail)),
    AifCollectionTypeAttribute('_myDetails', Types::Class,classStr(myDetail)),
    AifCollectionTypeAttribute('return', Types::Class,classStr(myDetail))]
    public List parmMyDetails(List _myDetails = myDetails)
    {
        if (!prmIsDefault(_myDetails))
        {
            myDetails = _myDetails;
        }
        return  myDetails;
    }
 
 
And how the code run later in my API is something like : 
myHeader  response = new myHeader();
 
and there is new variable List      myList to cater all the information to pass to Detail data contract.
There is actually nothing's wrong to pass on the details, which usually by create a list variable as well and pass it to the response like this :
response.parmMyDetails(myList)
 
Up to here is fine. But currently I have intention to retrieve again what's passed to that response.parmMyDetails and put in a string variable.
 
May I know how to do this ?
 
I think we cannot just call again this /response.parmMyDetails/, and put it in the list, like below :
 
ListIterator    listIterator = new ListIterator(response.parmMyDetails());
 
 
how can I achieve my intention, to retrieve again the list ? This retrieval, btw, is still inside the same function, so my API process actually already run well up to pass to API response, but now I'm gonna need to have addition of getting those details back into a string, for other purpose (logging)
 
Sorry, understand this can be confusing.
 
Let me know if I need to rephrase all this. Thanks.
  • CU03050731-0 Profile Picture
    CU03050731-0 65 on at
    How to handle or retrieve data contract with list ?
    Hi Layan,
     
    Solved. I'm using "responseStr = formJsonSerialzier::serializeClass(response);"
    Turns out it is very simple using this method.
     
    Many thanks.
  • Verified answer
    Layan Jwei Profile Picture
    Layan Jwei 3,447 Super User on at
    How to handle or retrieve data contract with list ?
    Hi,
     
    Yes will need to do it one by one OR as i told you try the formJsonSerialzier if the output will work with you
     
    while (listEnum.moveNext())
          {
             detailLine = listEnum.current();
             responseStr = formJsonSerialzier::serializeClass(detailLine);
             if(listEnum.moveNext())
             {
                responseStr += delimeter; 
             }
          }
     
    If your question is answered, then please verify the answers that helped.
  • CU03050731-0 Profile Picture
    CU03050731-0 65 on at
    How to handle or retrieve data contract with list ?
    Hi Layan,
     
    Yeah probably so, guess it means I need to one by one add and specify with the parm, right ? I mean cannot using list loop.  let me try first. 
    For the string, actually nothing specific, separate with comma or hyphen or semicolon will do just fine.
     
    Thanks again.
  • Layan Jwei Profile Picture
    Layan Jwei 3,447 Super User on at
    How to handle or retrieve data contract with list ?
    Hi,
     
    I think It's because the list is of type class, so you can't say str = iterator.current() 
     
    As you saw in my previous reply, i assigned the class to enumerator.current() then i took one of the parm methods and assigned it to the string variable like this:
    detailLine = listEnum.current();
    responseStr = detailLine.parmField1();
     
    But i think what you are trying to achieve is to put all fields of detailLine contract to string. Then you can do sth like this
    responseStr += detailLine.patmField1() + ',' + detailLine.parmField2()...etc
     
    So maybe u can seperate the same contract fields with comma and when you loop to sth new then u can seperate them with dash
     
     
    OR you can convert the contract to json string by using FormJsonSerializer class
     
     
    It's better to show us how would u like the string to look like in order to help you.
  • CU03050731-0 Profile Picture
    CU03050731-0 65 on at
    How to handle or retrieve data contract with list ?
    Hi,
     
    Sorry, I understand it is a bit confusing, but let me try to explain again in other way. I think it is better for me to explain how the list is built.
     
    1. I have a contract class which contain some details like this :
        
    [DataContractAttribute]
    class PaymentDetail
    {
        private str     settlementVoucher;
        private str     paymentDate;
        
    
        [DataMember("SettlementVoucher")]
        public str parmSettlementVoucher(str _value = settlementVoucher)
        {
            if (!prmIsDefault(_value))
            {
                settlementVoucher = _value;
            }
            return settlementVoucher;
        }
    
        [DataMember("PaymentDate")]
        public str parmPaymentDate(str _value = paymentDate)
        {
            if (!prmIsDefault(_value))
            {
                paymentDate = _value;
            }
            return paymentDate;
        }
    
        
    
    }
     
    2. Subsequently, this PaymentDetail is filled in by using their Parm method
     
    paymentDetail.parmSettlementVoucher("Voucher1234");
    paymentDetail.parmPaymentDate("2024-05-23");
     
    3. I create variable List like this "List        paymentList = new List(Types::Class);" and assign the value by using this :
    paymentList.addEnd(paymentDetail);
    4.  And then, for my response, I'm assigning it by this :
    response.parmPaymentDetails(paymentList);
     
    The paymentDetail, (Step 2) in my debug screen is something like this :
     
    The paymentList after Step 3 will be :
     
     
    And now my intention, provided it was already being passed as a response, I would like to get that again and put as a string. 
     
    The last effort that I'm choose right now, is to forget about the "response" thing, and just use the list variable, in this case is the "paymentList"
     
    As I tried to add like below:
    String255   strDetailLine;
    ListEnumerator listEnum;
    listEnum = paymentList.getEnumerator();
    
    if(listEnum)
    {
          ListIterator    listIterator = new ListIterator(paymentList);
          while (listIterator.more())
          {
                strDetailLine += listIterator.value();
    
                listIterator.next();
    
                if (listIterator.more())
                {
                    strDetailLine += listIterator.value();
    
                    listIterator.next();
                    
                    if (listIterator.more())
                    {
                      strDetailLine += ";";
                    }
                }
            }
    }
    However getting stuck, usually at the assignment to string, with conversion error, something like this :
     
     
    Thanks
  • Layan Jwei Profile Picture
    Layan Jwei 3,447 Super User on at
    How to handle or retrieve data contract with list ?
    Hi,

    ok my code should still work but u need to add small sth to it. So how would you like them to be separated in the string? maybe a comma?
     
    str delimeter = ",";
    str responseStr;
    List list = response.ParmDetails();
    DetailLine detailLine; ListEnumerator listEnum;
    ​​​​​​​ if(list && list.elements()) { listEnum = list.getEnumerator(); if(listEnum) { while (listEnum.moveNext()) { detailLine = listEnum.current();
      responseStr = detailLine.ParmField1(); if(listEnum.moveNext()) { responseStr += delimeter; } } } }
    or check out this link

    https://d365ffo.com/2021/08/03/ax-d365fo-convert-list-to-string-with-delimited-values/

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
     
  • Martin Dráb Profile Picture
    Martin Dráb 224,463 Super User on at
    How to handle or retrieve data contract with list ?
    It's up to you to decide what string you want to get; only then you can start writing code to provide this result.
     
    If you have a list with three elements (A, B and C), one of many possible options is separating values with comma ("A,B,C"). You already know how to get the values, the other necessary information is that you can use + operator to join strings (e.g. value + ',').
  • CU03050731-0 Profile Picture
    CU03050731-0 65 on at
    How to handle or retrieve data contract with list ?
    Hi Layan,
     
    Sorry, I'm saying I want to retrieve back my response.ParmDetails(). This ParmDetails is a list.
     
    I think what Martin ask, "Then you need to decide how you want to convert the list of elements to a single string." 
    is the one I'm asking.
     
    because I want to retrieve the list back and put in a string.
    As mentioned, I tried this -> 
          ListIterator    listIterator = new ListIterator(response.parmMyDetails()); but has error conversion.
     
    So how to get the list from response.parmDetails() and convert it to a string ?
     
    Thanks,
  • Layan Jwei Profile Picture
    Layan Jwei 3,447 Super User on at
    How to handle or retrieve data contract with list ?
    Hi,

    i will just give you a generic example of how to iterate through a list filled in contract class and you can apply what you need
     
    [DataContractAttribute]
    class HeaderRequestContract
    {
       List  detailLines; 
    
        [DataMemberAttribute,
        AifCollectionTypeAttribute('_detailLines',Types::Class,classStr(DetailLinesContract)),
        AifCollectionTypeAttribute('return',Types::Class,classStr(DetailLinesContract))]
        public List DetailLines(List _detailLines = detailLines)
        {
            detailLines = _detailLines;
            return detailLines;
        }
    }
     
     public ResponseContract createOrder(HeaderRequestContract _request)
     {           
            List               detailLines     = new List(Types::Class);
            ResponseContract   resContract     = new ResponseContract();
    
            DetailLinesContract       detailLine;
            ListEnumerator            listDetailLinesEnum;
            System.Exception          ex;
    
            try
            {
                detailLines = _request.DetailLines();
                if(!detailLines ||!detailLines.elements())
                {
                    throw error('No line records found for this order');
                }
    
    
                listDetailLinesEnum = detailLines.getEnumerator();
    
    
                if(listDetailLinesEnum)
                {
                   while (listDetailLinesEnum.moveNext())
                  {
                      detailLine = listDetailLinesEnum.current();
                      //add logic
                  }
                }
            }
            catch(ex.Message)
            {
            }
    }


    Are you saying now, that you want to pass the detailLines back to your response class as string? what does your response class contain? does it contain DetailLines as list or does it contain it as string? I didn't get what do u need exactly

    Thanks,
    Layan Jweihan
  • Martin Dráb Profile Picture
    Martin Dráb 224,463 Super User on at
    How to handle or retrieve data contract with list ?
    Calling the method gives you the list. If you want to iterate it, you can either use an iterator or an enumerator (response.parmMyDetails().getEnumerator()). Then you need to decide how you want to convert the list of elements to a single string.

Helpful resources

Quick Links

Can you answer this forum question?

You could make someone's day!

Community Newsletter - May 2024

Kudos to our community stars!

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 283,623 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 224,463 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Featured topics

Product updates

Dynamics 365 release plans