Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Unanswered

How to pass multiple list parameters to a custom service?

(1) ShareShare
ReportReport
Posted on by 159
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. 
 
  • Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    How to pass multiple list parameters to a custom service?
    Trying to guess where things failed isn't an easy task. Fortunately, there is a tool that can tell you what happens in your code: the debugger.
     
    For example, you can put a breakpoint at the beginning of your service operation method and walk through your code, or you can place it in Info.add() and when processing stops there, you can look at the stack trace. In either case, you can review values of variables at the time of the failure.
     
    If knowing where your code failed and on what data isn't sufficient for you to resolve the problem, you'll at least have much more concrete information to share with us.
  • Arif Hussain Profile Picture
    Arif Hussain 159 on at
    How to pass multiple list parameters to a custom service?
    Sir Martin Dráb ,

    My apology sir. Actually, I have created all the parmMethods in my request class but I have not showed them in the post because I thought they are just repeating the same and it would save the space as well. I am using parmName, parmCustPaymTerm, parmCustGroup, and all other parmMethods expect the parmCustAccount for this insertion method. 

    Here is my complete 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;
        }
        [DataMember("Name")]
        public Name parmName(Name _name = name)
        {
            if (!prmIsDefault(_name))
            {
                name = _name;
            }
            return name;
        }
        [DataMember("CustPaymTermId")]
        public CustPaymTermId parmCustPaymTerm(CustPaymTermId _custPaymTerm = custPaymTerm)
        {
            if (!prmIsDefault(_custPaymTerm))
            {
                custPaymTerm = _custPaymTerm;
            }
            return custPaymTerm;
        }
        [DataMember("CustGroupId")]
        public CustGroupId parmCustGroup(Name _custGroup = custGroup)
        {
            if (!prmIsDefault(_custGroup))
            {
                custGroup = _custGroup;
            }
            return custGroup;
        }
        [DataMember("CustCurrencyCode")]
        public CustCurrencyCode parmCustCurrency(CustCurrencyCode _custCurrency = custCurrency)
        {
            if (!prmIsDefault(_custCurrency))
            {
                custCurrency = _custCurrency;
            }
            return custCurrency;
        }
        [DataMember("CustClassificationId")]
        public CustClassificationId parmCustClassification(CustClassificationId _custClassification = custClassification)
        {
            if (!prmIsDefault(_custClassification))
            {
                custClassification = _custClassification;
            }
            return custClassification;
        }
        [DataMember("CustCreditRating")]
        public CustCreditRating parmCustCreditRating(CustCreditRating _custCreditRating = custCreditRating)
        {
            if (!prmIsDefault(_custCreditRating))
            {
                custCreditRating = _custCreditRating;
            }
            return custCreditRating;
        }
        [DataMember("InventSiteId")]
        public InventSiteId parmInventSiteId(InventSiteId _custInventSite = custInventSite)
        {
            if (!prmIsDefault(_custInventSite))
            {
                custInventSite = _custInventSite;
            }
            return custInventSite;
        }
        [DataMember("InventLocationId")]
        public InventLocationId parmInventLocationId(InventLocationId _custInventLocation = custInventLocation)
        {
            if (!prmIsDefault(_custInventLocation))
            {
                custInventLocation = _custInventLocation;
            }
            return custInventLocation;
        }
    }
  • Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    How to pass multiple list parameters to a custom service?
    Let me try to explain it once more.
     
    The only data member in your contract class is CustAccount. This is the only thing you can set. But you're not setting it either, because - as you're saying - you don't need it. This means that the contract class, and therefore the whole service, is useless, because you'll pass any data there. You have no information about any customer.
     
    According to your JSON, you want to pass properties like Name and CustGroupId, but your CustomerRequest class doesn't support them. You have variables like name and custGroup, but you don't have corresponding parm methods with DataContract attribute.
  • Arif Hussain Profile Picture
    Arif Hussain 159 on at
    How to pass multiple list parameters to a custom service?
    Sir Martin Dráb,

    The data is being send in correct order, I am not using CustAccount in this insert method as the method is getting number sequence itself but custAccount is used in another update method but not for now. 

    The error this time I am facing is:

  • Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    How to pass multiple list parameters to a custom service?
    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.
  • Arif Hussain Profile Picture
    Arif Hussain 159 on at
    How to pass multiple list parameters to a custom service?
    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
    Martin Dráb 230,842 Most Valuable Professional on at
    How to pass multiple list parameters to a custom service?
    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
    Arif Hussain 159 on at
    How to pass multiple list parameters to a custom service?
    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
    Martin Dráb 230,842 Most Valuable Professional on at
    How to pass multiple list parameters to a custom service?
    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).
  • Layan Jwei Profile Picture
    Layan Jwei 7,624 Super User 2025 Season 1 on at
    How to pass multiple list parameters to a custom service?
    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

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans