0

I am using Postman to get all account records from salesforce. The below code is working for only one record

@HttpGet global static Account doGet() 
    {

        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
     Account result = [SELECT Id, Name, Phone, Website FROM Account Limit 1];
        return result;
    }

The problem is when I use the list in order to get all accounts record Please check the code below

@HttpGet global static Account doGet() 
    {

        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String AccNumber = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
      list<Account> result = [SELECT Id, Name, Phone, Website FROM Account LIMIT 2];
        return result;
    }

its show error ...."Illegal conversion from List<Account> to Account " I don't know how to solve this

2

You don't need to write a custom API class to query all Accounts. Use the out-of-the-box SOQL Query endpoint instead.

The specific problem in this code is that your method is declared to return one Account:

@HttpGet global static Account doGet() 

but you are in fact returning a List<Account>, which is not the same type:

  list<Account> result = [SELECT Id, Name, Phone, Website FROM Account LIMIT 2];

While you could change the method signature to fix this by returning List<Account>, you should instead use the standard REST API endpoint.

Note that much of this code does nothing at all; the method reduces to

return [SELECT Id, Name, Phone, Website FROM Account LIMIT 2];
1
  • its work David thanks – fawad May 5 at 19:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.