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

Notifications

Announcements

Community site session details

Community site session details

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

How to pass multiple list parameters to a custom service?

(1) ShareShare
ReportReport
Posted on by 206
Hi experts,

I am trying to consume custom service and trying to create multiple customer using it. I have created single customer using custom service but now I am working on creating multiple ones. I have passed multiple list as a parameter but it gives me the following error on Postman. 





I am creating the following classes. 
[DataContractAttribute]
class MultipleCustomerRequest
{
    List    name;
    List    custPaymTerm;
    List    custGroup;
    List    custCurrency;
    List    custClassification;
    List    custCreditRating;
    List    custInventSite;
    List    custInventLocation;
    List    custAccount;
    [DataMember(/CustAccount/),
        AifCollectionTypeAttribute('CustAccount', Types::String)]
    public List parmCustAccount(List _custAccount = custAccount)
    {
        if (!prmIsDefault(_custAccount))
        {
            custAccount = _custAccount;
        }
        return custAccount;
    }
}


Reponse Class
 
[DataContractAttribute]
class CustomerResponse
{
    boolean         success;
    str             errorMessage;
    CustAccount     custAccount;
    [DataMember(/ErrorMessage/)]
    public str parmErrorMessage(str _value = errorMessage)
    {
        if (!prmIsDefault(_value))
        {
            errorMessage = _value;
        }
        return errorMessage;
    }
    [DataMember(/Success/)]
    public Boolean parmSuccess(Boolean _value = success)
    {
        if (!prmIsDefault(_value))
        {
            success = _value;
        }
        return success;
    }
    [DataMember(/CustAccount/)]
    public CustAccount parmCustAccount(CustAccount _custAccount = custAccount)
    {
        custAccount = _custAccount;
        return custAccount;
    }
}




//Service class
 
public class MultipleCustomerService
{
    public CustomerResponse createMultipleCustomer(MultipleCustomerRequest _request)
    {
        ListEnumerator nameIterator, custPaymTermIterator, custGroupIterator, custCurrencyIterator, custClassificationIterator, 
            custCreditRatingIterator, custInventSiteIdIterator, custInventLocationIterator;
        var     response                = new CustomerResponse();
        List    nameList                = _request.parmName();
        List    custPaymTermList        = _request.parmCustPaymTerm();
        List    custGroupList           = _request.parmCustGroup();
        List    custCurrencyList        = _request.parmCustCurrency();
        List    custClassificationList  = _request.parmCustClassification();
        List    custCreditRatingList    = _request.parmCustCreditRating();
        List    custInventSiteList      = _request.parmInventSiteId();
        List    custInventLocationList  = _request.parmInventLocationId();
        Name                    name;
        CustPaymTermId          custPaymTerm;
        CustGroupId             custGroup;
        CustCurrencyCode        custCurrency;
        CustClassificationId    custClassification;
        CustCreditRating        custCreditRating;
        InventSiteId            custInventSite;
        InventLocationId        custInventLocation;
        CustTable                    custTable;
        NumberSeq                    numberSeq;
        if (!nameList.empty() && !custPaymTermList.empty() && !custGroupList.empty() && !custCurrencyList.empty()
            && !custClassificationList.empty() && !custCreditRatingList.empty() && !custInventSiteList.empty() && !custInventLocationList.empty())
        {
            nameIterator                = nameList.getEnumerator();
            custPaymTermIterator        = custPaymTermList.getEnumerator();
            custGroupIterator           = custGroupList.getEnumerator();
            custCurrencyIterator        = custCurrencyList.getEnumerator();
            custClassificationIterator  = custClassificationList.getEnumerator();
            custCreditRatingIterator    = custCreditRatingList.getEnumerator();
            custInventSiteIdIterator    = custInventSiteList.getEnumerator();
            custInventLocationIterator  = custInventLocationList.getEnumerator();
            while (nameIterator.moveNext() && custPaymTermIterator.moveNext() && custGroupIterator.moveNext() && custCurrencyIterator.moveNext()
                && custClassificationIterator.moveNext() && custCreditRatingIterator.moveNext() && custInventSiteIdIterator.moveNext() 
                && custInventLocationIterator.moveNext())
            {
                Name                = nameIterator.current();
                custPaymTerm        = custPaymTermIterator.current();
                custGroup           = custGroupIterator.current();
                custCurrency        = custCurrencyIterator.current();
                custClassification  = custClassificationIterator.current();
                custCreditRating    = custCreditRatingIterator.current();
                custInventSite      = custInventSiteIdIterator.current();
                custInventLocation  = custInventLocationIterator.current();
                custTable.initValue();
                try
                {
                    numberSeq                       = NumberSeq::newGetNum(CustParameters::numRefCustAccount());
                    custTable.AccountNum            = numberSeq.num();
                    custTable.PaymTermId            = custPaymTerm;
                    custTable.CustGroup             = custGroup;
                    custTable.Currency              = custCurrency;
                    custTable.CustClassificationId  = custClassification;
                    custTable.CreditRating          = custCreditRating;
                    custTable.InventSiteId          = custInventSite;
                    custTable.InventLocation        = custInventLocation;
                    custTable.insert(DirPartyType::Organization, name);
                    if(custTable.AccountNum)
                    {
                        response.parmCustAccount(custTable.AccountNum);
                        response.parmSuccess(true);
                    }
                }
                catch (Exception::CLRError)
                {
                    System.Exception interopException = CLRInterop::getLastException();
                    response.parmSuccess(false);
                    response.parmErrorMessage(interopException.ToString());
                }
            }
        }
        return response;
    }
}
 
 


Please let me know if where I am wrong. I will be really grateful to you. Thanks in advance. 
 
I have the same question (0)
  • Martin Dráb Profile Picture
    238,286 Most Valuable Professional on at
    You have a wrong value in the first parameter of AifCollectionTypeAttribute. The method argument is called _custAccount, not CustAccount. Either fix the value or rename the parameter.
     
    The error Postman may also be caused by passing wrong data.
     
    By the way, your code would be much simpler if you created a list of data contracts, instead of having eight separate lists and iterating each of them.
  • Arif Hussain Profile Picture
    206 on at
    Sir Martin Dráb,

    Thank you again for the prompt response. I get you but can you please share some sample of code how to created a list of data contracts as you suggested. It would be a great help to me. 
  • Martin Dráb Profile Picture
    238,286 Most Valuable Professional on at
    You already know how to create a data contract class and (more or less) how to create a data member with List as the type. You just one more piece of information: that AifCollectionTypeAttribute has a third argument where you'll say which class it is. For example:
    [DataContract]
    class MainContract
    {
        List items; // List of ItemContract objects
    
        [
            DataMember,
            AifCollectionType('_items', Types::Class, classStr(ItemContract)),
            AifCollectionType('return', Types::Class, classStr(ItemContract)),
        ]
        public List parmItems(List _items)
        {
            ...
        }
    }
    If you need just the list, you could use it directly as a parameter of your service operation (without having MainContract class).
     
  • Arif Hussain Profile Picture
    206 on at
    Sir Martin Dráb ,

    I am a little bit confused. The sample code you shared is for the request, or service class considering my code?

    I am referencing to the suggestion you told, "By the way, your code would be much simpler if you created a list of data contracts, instead of having eight separate lists and iterating each of them."
  • Layan Jwei Profile Picture
    8,158 Super User 2025 Season 2 on at
    Hi Arif,

    Try something like this:
     
    
    class MultipleCustomerService
    {
        [AifCollectionTypeAttribute('_request, Types::Class, classStr(CustomerRequest)),
         AifCollectionTypeAttribute('return', Types::Class, classStr(CustomerResponse))
        ]
        public List createMultipleCustomer (List _customers)
        {
            List                            response    = new List(Types::Class);
    
            ListEnumerator                  custEnum;
            MultipleCustomerRequest reqContract;
    
            if(_orders)
            {
                custEnum = _customers.getEnumerator();
    
                if(custEnum)
                {
                    while (custEnum.moveNext())
                    {
                        reqContract = custEnum.current();
    
                        response.addEnd(this.createSingleCustomer(reqContract));
                    
                    }
                }
            }
    
            return response;
        }
    
      private CustomerResponse createSingleCuctomer(CustomerRequest _customerReq)
      {
         //add logic   
      }
    }
    
    
    
    [DataContractAttribute]
    class CustomerRequest
    {
        str    name;
        str    custPaymTerm;
        str    custGroup;
        str    custCurrency;
        str    custClassification;
        str    custCreditRating;
        str    custInventSite;
        str    custInventLocation;
    }
     
    Thanks,
    Layan Jweihan
  • Martin Dráb Profile Picture
    238,286 Most Valuable Professional on at
    The MainContract class I shared with you is a data contract class. As you already know, it's used to define parameters for service operations. It's not a service class itself.
     
    Layan showed you what I mentioned as an alternative in the last sentence (using a list of data contracts directly as a parameter of a service operation).
  • Arif Hussain Profile Picture
    206 on at
    Sir Layan Jwei,

    I have modified your code according to my requirements and below is the new code. But, I have multiple customers and each customer containing multiple detail like I am passing the details of customer as well like the customerGroup, currency, name etc for each of the customers. How can I tackle that with you code? 
     
       
    class NewMultipleCustomerService
    {
        [AifCollectionTypeAttribute('_request', Types::Class, classStr(CustomerRequest)),
         AifCollectionTypeAttribute('return', Types::Class, classStr(CustomerResponse))
        ]
        public List createMultipleCustomer (List _customers)
        {
            List    response = new List(Types::Class);
            
            ListEnumerator custEnum;
            CustomerRequest reqContract;
            if(_customers)
            {
                custEnum = _customers.getEnumerator();
                if(custEnum)
                {
                    while (custEnum.moveNext())
                    {
                        //The below line gives error if assigned directly to the contract variable
                        //reqContract = custEnum.current();
                        reqContract.parmCustPaymTerm(custEnum.current());
                        response.addEnd(this.createSingleCustomer(reqContract));
                    
                    }
                }
            }
            return response;
        }
        public CustomerResponse createSingleCustomer(CustomerRequest _request)
        {
            var                     response            = new CustomerResponse();
            Name                    name                = _request.parmName();
            CustPaymTermId          custPaymTerm        = _request.parmCustPaymTerm();
            CustGroupId             custGroup           = _request.parmCustGroup();
            CustCurrencyCode        custCurrency        = _request.parmCustCurrency();
            CustClassificationId    custClassification  = _request.parmCustClassification();
            CustCreditRating        custCreditRating    = _request.parmCustCreditRating();
            InventSiteId            custInventSite      = _request.parmInventSiteId();
            InventLocationId        custInventLocation  = _request.parmInventLocationId();
            CustTable                    custTable;
            NumberSeq                    numberSeq;
            custTable.initValue();
            try
            {
                numberSeq                       = NumberSeq::newGetNum(CustParameters::numRefCustAccount());
                custTable.AccountNum            = numberSeq.num();
                custTable.PaymTermId            = custPaymTerm;
                custTable.CustGroup             = custGroup;
                custTable.Currency              = custCurrency;
                custTable.CustClassificationId  = custClassification;
                custTable.CreditRating          = custCreditRating;
                custTable.InventSiteId          = custInventSite;
                custTable.InventLocation        = custInventLocation;
                custTable.insert(DirPartyType::Organization, name);
                if(custTable.AccountNum)
                {
                    response.parmCustAccount(custTable.AccountNum);
                    response.parmSuccess(true);
                }
            }
            catch (Exception::CLRError)
            {
                System.Exception interopException = CLRInterop::getLastException();
                response.parmSuccess(false);
                response.parmErrorMessage(interopException.ToString());
            }
            return response;
        }
    }
  • Martin Dráb Profile Picture
    238,286 Most Valuable Professional on at
    The fix you did - assigning a CustomerRequest object to custPaymTerm variable - makes no sense. These are different things and different types. So it's no solution at all.
     
    If reqContract = custEnum.current() fails, it likely means that what you have in _customers variable isn't really a list of CustomerRequest object. And I see a bug - you have a wrong a value in AifCollectionTypeAttribute. You declare the type of elements of _request parameter, but your parameter is actually called _customers.
  • Arif Hussain Profile Picture
    206 on at
    Sir Martin Dráb,

    I have changed the code as suggested and here is the new version


    // Request Class
    [DataContractAttribute]
    class CustomerRequest
    {
        Name                    name;
        CustPaymTermId          custPaymTerm;
        CustGroupId             custGroup;
        CustCurrencyCode        custCurrency;
        CustClassificationId    custClassification;
        CustCreditRating        custCreditRating;
        InventSiteId            custInventSite;
        InventLocationId        custInventLocation;
        CustAccount             custAccount;
        [DataMember("CustAccount")]
        public CustAccount parmCustAccount(CustAccount _custAccount = custAccount)
        {
            if (!prmIsDefault(_custAccount))
            {
                custAccount = _custAccount;
            }
            return custAccount;
        }
    }


    //Response class
    [DataContractAttribute]
    class CustomerResponse
    {
        boolean         success;
        str             errorMessage;
        CustAccount     custAccount;
        [DataMember("ErrorMessage")]
        public str parmErrorMessage(str _value = errorMessage)
        {
            if (!prmIsDefault(_value))
            {
                errorMessage = _value;
            }
            return errorMessage;
        }
        [DataMember("Success")]
        public Boolean parmSuccess(Boolean _value = success)
        {
            if (!prmIsDefault(_value))
            {
                success = _value;
            }
            return success;
        }
        [DataMember("CustAccount")]
        public CustAccount parmCustAccount(CustAccount _custAccount = custAccount)
        {
            custAccount = _custAccount;
            return custAccount;
        }
    }


    //Here is the service class
    class NewMultipleCustomerService
    {
        [AifCollectionTypeAttribute('_customers', Types::Class, classStr(CustomerRequest)),
         AifCollectionTypeAttribute('return', Types::Class, classStr(CustomerResponse))
        ]
        public List createMultipleCustomer (List _customers)
        {
            List    response = new List(Types::Class);
            
            ListEnumerator custEnum;
            CustomerRequest reqContract;
            if(_customers)
            {
                custEnum = _customers.getEnumerator();
                if(custEnum)
                {
                    while (custEnum.moveNext())
                    {
                        //The below line gives error if assigned directly to the contract variable
                        //reqContract = custEnum.current();
                        reqContract.parmCustPaymTerm(custEnum.current());
                        response.addEnd(this.createSingleCustomer(reqContract));
                    
                    }
                }
            }
            return response;
        }
        public CustomerResponse createSingleCustomer(CustomerRequest _request)
        {
            var                     response            = new CustomerResponse();
            Name                    name                = _request.parmName();
            CustPaymTermId          custPaymTerm        = _request.parmCustPaymTerm();
            CustGroupId             custGroup           = _request.parmCustGroup();
            CustCurrencyCode        custCurrency        = _request.parmCustCurrency();
            CustClassificationId    custClassification  = _request.parmCustClassification();
            CustCreditRating        custCreditRating    = _request.parmCustCreditRating();
            InventSiteId            custInventSite      = _request.parmInventSiteId();
            InventLocationId        custInventLocation  = _request.parmInventLocationId();
            CustTable                    custTable;
            NumberSeq                    numberSeq;
            custTable.initValue();
            try
            {
                numberSeq                       = NumberSeq::newGetNum(CustParameters::numRefCustAccount());
                custTable.AccountNum            = numberSeq.num();
                custTable.PaymTermId            = custPaymTerm;
                custTable.CustGroup             = custGroup;
                custTable.Currency              = custCurrency;
                custTable.CustClassificationId  = custClassification;
                custTable.CreditRating          = custCreditRating;
                custTable.InventSiteId          = custInventSite;
                custTable.InventLocation        = custInventLocation;
                custTable.insert(DirPartyType::Organization, name);
                if(custTable.AccountNum)
                {
                    response.parmCustAccount(custTable.AccountNum);
                    response.parmSuccess(true);
                }
            }
            catch (Exception::CLRError)
            {
                System.Exception interopException = CLRInterop::getLastException();
                response.parmSuccess(false);
                response.parmErrorMessage(interopException.ToString());
            }
            return response;
        }
    }

     
    and here how am I passing the list to the body of a postman post request.




    But still I am getting the error. Please correct me anywhere if I am wrong. 
  • Martin Dráb Profile Picture
    238,286 Most Valuable Professional on at
    First of all, send data that match your data contact. Your contract has a single data member, CustAccount, but you don't have any CustAccount in the JSON object.

    If it doesn't help, please tell us what error you're getting. And use the debugger to find out what values you currently have in _customers variable.

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

News and Announcements

Season of Giving Solutions is Here!

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
Abhilash Warrier Profile Picture

Abhilash Warrier 843 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 342 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans