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 :
Finance | Project Operations, Human Resources, ...
Unanswered

How can i pass list of object and return this list in custom service

(0) ShareShare
ReportReport
Posted on by 1,025

Hello expert, I need some help to pass a list of object and return it

1- Create data contract 

[DataContract('Customers')]
class AHCustomerDataContract
{
    AccountNum  accountNum;
    Name        custName;

    [DataMember('AccountNum')]
    public AccountNum parmAccountNum(AccountNum _accountNum = accountNum)
    {
        accountNum = _accountNum;
        return accountNum;
    }

    [DataMember('CustName')]
    public Name parmCustName(Name _custName = custName)
    {
        custName = _custName;
        return custName;
    }

    protected void new()
    {
    }

    private static AHCustomerDataContract construct()
    {
        return new AHCustomerDataContract();
    }

    public static AHCustomerDataContract newFromTableRecord(CustTable   _custTable)
    {
        AHCustomerDataContract  contract = AHCustomerDataContract::construct();

        contract.parmAccountNum(_custTable.AccountNum);
        contract.parmCustName(_custTable.name());

        return contract;
    }

}

2- Create list data contract 

[DataContractAttribute]
class AHCustomerListDataContract
{
    List customerList;
    protected void new()
    {
        customerList = new List(Types::Class);
    }

    public static AHCustomerListDataContract construct()
    {
        return new AHCustomerListDataContract();
    }

    public void addCustomerToList(AHCustomerDataContract _customerDataContract)
    {
        customerList.addEnd(_customerDataContract);
    }

    [DataMember,AifCollectionType('return',Types::Class,classStr(AHCustomerDataContract))
        ,AifCollectionType('_custList',Types::Class,classStr(AHCustomerDataContract))
        ]
    public List parmCustomerList(List _custList = customerList)
    {
        customerList = _custList;
        return customerList;
    }

}

3- Create service class

[AifCollectionTypeAttribute('customerList',Types::Class,classStr(AHCustomerDataContract))]
    public AHCustomerListDataContract getCustomerList(List customerList)
    {
        ListIterator    iterator;
        ListEnumerator  enumerator;
        ListIterator    literator;
        AHCustomerListDataContract  customerListDataContract = AHCustomerListDataContract::construct();
        AHCustomerDataContract      customerContract;

        enumerator = customerList.getEnumerator();

        while(enumerator.moveNext())
        {
            customerContract = enumerator.current();

            if(customerContract != null)
            {
                customerContract = AHCustomerDataContract::newFromTableRecord(CustTable::find(customerContract.parmAccountNum()));
                customerListDataContract.addCustomerToList(customerContract);
            }
        }
        return customerListDataContract;
    }

4- consume the service in JSON

static void Main(string[] args)
    {
        GetCustomerList();
    }
    
class AHContract
    {
        public string parmAccountNum { get; set; }
        public string AccountNum { get; set; }
    }

public static void GetCustomerList()
        {
            // In the AOT you will find UserSessionService in Service Groups and AifUserSessionService under Services.
            string sessionUrl = "/api/services/AHServices/CustomerServices/getCustomerList";
            string GetUserSessionOperationPath = string.Format("{0}{1}", ClientConfiguration.Default.UriString.TrimEnd('/'), sessionUrl);
            var request = HttpWebRequest.Create(GetUserSessionOperationPath);

            List custList = new List
            {
                new AHContract{AccountNum = "US-001"},
                new AHContract{AccountNum = "US-003"}
            };

            var contract = new { customerList = custList };

            string json = JsonConvert.SerializeObject(contract);
            Byte[] byteArray = Encoding.UTF8.GetBytes(json);
            request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader(true);
            request.Method = "POST";
            request.ContentLength = byteArray.Length;
            request.ContentType = "application/json";

            using (var stream = request.GetRequestStream())
            {
                stream.Write(byteArray, 0, byteArray.Length);
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        string responseString = streamReader.ReadToEnd();

                        Console.WriteLine(responseString);
                    }
                }
            }

            Console.ReadLine();
        }

My problem is I get only the transaction related to customer US-003

pastedimage1612282973032v1.png

Thank's in advance 

I have the same question (0)
  • Martin Dráb Profile Picture
    237,892 Most Valuable Professional on at

    What did you find when you stepped through your code in a debugger?

  • source258147 Profile Picture
    1,025 on at

    I only get this customer US-003

    pastedimage1612284272854v1.png

  • Martin Dráb Profile Picture
    237,892 Most Valuable Professional on at

    Start with customerList variable. If you don't have the right data there, the body of the method isn't important, because what's wrong is the input.

    If you have the right data there, then you have a problem in code and you should use the debugger to identify it.

  • source258147 Profile Picture
    1,025 on at

    please check this print screen

    I send two customer 

    pastedimage1612301676861v2.png

    and i get in custom service only one 

    pastedimage1612301595775v1.png

  • Martin Dráb Profile Picture
    237,892 Most Valuable Professional on at

    Yes, I did see that you sent two items, but I didn't know what you received.

    So you don't get just one customer contract - you get two, but both with the same accountNum.

    Unfortunately I don't have any explanation at the moment. :-(

  • source258147 Profile Picture
    1,025 on at

    I problem solved when I comment the new method in my code

    [DataContract('Customers')]
    class AHCustomerDataContract
    {
        AccountNum  accountNum;
        Name        custName;
    
        [DataMember('AccountNum')]
        public AccountNum parmAccountNum(AccountNum _accountNum = accountNum)
        {
            accountNum = _accountNum;
            return accountNum;
        }
    
        [DataMember('CustName')]
        public Name parmCustName(Name _custName = custName)
        {
            custName = _custName;
            return custName;
        }
    /*
        protected void new()
        {
        }
    */
        private static AHCustomerDataContract construct()
        {
            return new AHCustomerDataContract();
        }
    
        public static AHCustomerDataContract newFromTableRecord(CustTable   _custTable)
        {
            AHCustomerDataContract  contract = AHCustomerDataContract::construct();
    
            contract.parmAccountNum(_custTable.AccountNum);
            contract.parmCustName(_custTable.name());
    
            return contract;
        }
    
    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans