1

I am new this rest api concept in salesforce and i tried my best to get my requirement. What my requirement is

I want to return account fields like id, name, phone and industry(and etc) using wrapper class only. i tried with following class and this class is returning only one record, not all the records which meets the condition.

REST Class:

@RestResource(urlMapping='/wrapper/*')
global with sharing class campclass{
    @HttpGet
       global static Accountwrapper doget() {

            RestRequest req = RestContext.request;
            String accphone = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
            RestResponse res = RestContext.response;

            List<Account> result = [Select Id, name,phone from account where  phone=:accphone];
            System.debug('Result:::'+result);

            Accountwrapper accWrap = new Accountwrapper();
            for(Account acc : result){
                    accWrap.accId = acc.Id;
                    accWrap.Name = acc.Name;
                    accWrap.Phone = acc.Phone;
             } 
            return accWrap;
       }
        global class Accountwrapper {
           Public Id accId;
           public String Name;
           public String Phone;

       }
}

Could any help me please, how to get bulk records by using above class.

Thanks and regards.

0

You need to return list of accounts instead of single wrapperclass instance

    @RestResource(urlMapping='/wrapper/*')
global with sharing class campclass{
    @HttpGet
       global static List<Accountwrapper> doget() {

            RestRequest req = RestContext.request;
            String accphone = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
            RestResponse res = RestContext.response;

            List<Account> result = [Select Id, name,phone from account where  phone=:accphone];
            System.debug('Result:::'+result);

            List<Accountwrapper> accWrapList = new List<Accountwrapper>();
            for(Account acc : result){
                    Accountwrapper accWrap = new Accountwrapper();
                    accWrap.accId = acc.Id;
                    accWrap.Name = acc.Name;
                    accWrap.Phone = acc.Phone;
                    accWrapList .add(accWrap);
             } 
            return accWrapList ;
   }
        global class Accountwrapper {
           Public Id accId;
           public String Name;
           public String Phone;

       }
}

This will return all Accounts.

I am successfully able to save this as well. enter image description here

  • Hi Tushar Sharma, While i am trying with above changes, its giving an error like NOT_ACCEPTABLE and Unsupported return type – user37550 Dec 2 '16 at 6:17
  • @Lakshmi i am able to save this. Check API version of your class. – Tushar Sharma Dec 2 '16 at 6:33
  • @Sharma, class is saving successfully. But, i got this error while i am passing REST url and by calling get method in postman – user37550 Dec 2 '16 at 6:35
  • 1
    Yes @Sharma, Now i got my requirement and its working now. Thanking you – user37550 Dec 2 '16 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy