Skip to main content

Notifications

Announcements

No record found.

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

Issue with list class in Custom Service

Posted on by 1,869
Dears,
 
I'm trying to pull the list of customers. I use below code to get the list of customers. But the method is not returned any customer list.  
 
public class CustomerService
{
    [AifCollectionTypeAttribute('return', Types::Class, classStr(CustomerRequest))
    
    ]
       public List getCust()
    {
        // var   response   = new CustomerResponse();
        CustomerRequest _request;
        List custlist = new List(Types::Class);
        ListEnumerator CustEnumerator;
        CustTable custTable;
        while select *from custTable
        {
            _request = new CustomerRequest();
            _request.parmCustAccount(custTable.AccountNum);
            custlist.addEnd(_request);
        }
        return custlist;
 
    }

    }
 
  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Issue with list class in Custom Service
    It's strange you're happy with it, because you have a serious bug there. You filter CustTable by DataAreaId, but you forgot to make the query cross-company, therefore it'll work correctly only if you're currently logged to the same company as in dataAreaId variable. You should add crossCompany keyword to the select statement. An alternative approach is using a changeCompany block.
  • Verified answer
    Faqruddin Profile Picture
    Faqruddin 1,869 on at
    Issue with list class in Custom Service
    Thank you for your valuable advices. I change my code as follows as per your advice. Its little bit tricky part while using List class. inside the get() method to return as collection data. It works as follows.
     
    //[DataContractAttribute]
    [DataContract]
    public class AXCustomerContract
    {
        DataAreaId dataAreaId;
        [DataMemberAttribute("dataAreaId")]
        public DataAreaId parmDataAreaID(DataAreaId _areaId = dataAreaId)
        {
            dataAreaId = _areaId;
            return dataAreaId;
        }
        //[DataMemberAttribute("CustomerAccount")]
        //public CustAccount paramCustomerAccount(CustAccount _custAccount = custAccount)
        //{
        //    custAccount = _custAccount;
        //    return custAccount;
        //}
    }
     
     
     class AXCustomerGeterSetter
    {
        CustAccount     custAccount;
        DataAreaId dataAreaId;
        [DataMemberAttribute("dataAreaId")]
        public DataAreaId parmDataAreaID(DataAreaId _areaId = dataAreaId)
        {
            dataAreaId = _areaId;
            return dataAreaId;
        }
        [DataMemberAttribute("custAccount")]
        public custAccount parmCustAccount(custAccount _custAccount = custAccount)
        {
            custAccount = _custAccount;
            return custAccount;
        }
    }
     
     
    public class AXCustomerService
    {
        [AifCollectionType('return', Types::Class, classStr(AXCustomerGeterSetter))]
        public List getCust(AXCustomerContract _req)
        {
            // var   response   = new CustomerResponse()
            CustTable custTable;
            DataAreaId dataAreaId;
            List custlist = new List(Types::Class);
          
            ;
            dataAreaId = _req.parmDataAreaID();
            while select *from custTable where custTable.DataAreaId == dataAreaId
            {
                AXCustomerGeterSetter response = new AXCustomerGeterSetter();
                
                response.parmDataAreaID(custTable.DataAreaId);
                response.parmCustAccount(custTable.AccountNum);
                custlist.addEnd(response);
            }
            return custlist;
     
        }
    }
     
  • Verified answer
    Layan Jwei Profile Picture
    Layan Jwei 7,347 Super User 2024 Season 2 on at
    Issue with list class in Custom Service
    Hi Faqruddin,
     
    When you debug, does it enter the while loop? Or does it skip it? If it skips it, as Martin said, check CustTable.DataAreaId when debugging, it might be DAT company
     
    Thanks,
    Layan Jweihan
     
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Issue with list class in Custom Service of Dynamics 365 FO
    Maybe the method works flawlessly and the reason why it doesn't return any customers is that you're using a wrong company, where no customers exists.
     
    Note that you don't need to just observe the result and guess what's going on. You can use the debugger to see what exactly happens in your code.

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans