function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ranveer singh 8ranveer singh 8 

Apex classes as webservice using rest

Hi guys,

Below is the code for making a callout to the webservice apex class

public class accountmanagerintegration{

 public string i;
public accountmanagerintegration(string a){
this.i=a;

httprequest req = new httprequest();
string endpoint = 'https://ap1.salesforce.com/services/apexrest/Accounts/'+i+'/contacts';
req.setendpoint(endpoint);
Req.setHeader('Authorization', 'Bearer 00D90000000w2Jp!AQEAQF9ngGD6kRrKjH6Qzg21GWkHWpU4naYXdUi6pdFk3bhCdHhidUpU_oNNp1QD2ZSXTj4ekRRVzTMxSKVXT5Qsr4yN.aWr'); 
Req.setheader('X-PrettyPrint','1'); 
req.setmethod('GET');

http p = new http();
httpresponse res = new httpresponse();
res = p.send(req);

system.debug('----status---'+res.getstatus());
system.debug('----status code---'+res.getstatuscode());
system.debug('----body---'+res.getbody());
}
}


And the webservice class is

@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
   
 
@HttpGet
global static list<list<sobject>> getAccount(){
       
// To obtain the url
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
      
String restrequestURL = URL.getSalesforceBaseUrl().toExternalForm();        
restrequestURL=restrequestURL+'/services/apexrest'+RestContext.request.requestURI;

for(String s:RestContext.request.params.keyset()){
    restrequestURL=restrequestURL+s+'='+RestContext.request.params.get(s)+'&';
}

restrequestURL=restrequestURL.removeEnd('&');
system.debug('*******'+restrequestURL);
        
string id1 = restrequestURL.mid(54,15);
system.debug('--------id1 to process records------'+id1);
        
list<list<sobject>> accres = [FIND 'go*' IN ALL FIELDS RETURNING Account(id,name where id =: +id1),contact(id,lastname where accountid =: +id1)];
system.debug('------output-----'+accres);
return accres;
 }

}

here i am expecting list<list<sobject>> as out put instead i am getting two empty lists as output ,even i have made sure thatthe value which is passed to id1 is existing id in the org ...can anybody help me on this
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BZhiIAG
2) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BaDUIA0

Please check below post for step by step process with screen shot.
1) http://amitsalesforce.blogspot.com/2016/04/rest-api-in-salesforce-execute-rest-api.html

Do minar change your code like below :-
@RestResource(urlMapping='/Account/Contacts/*')
global with sharing class AccountManager
{
  @HttpGet
   global static Account getAccount()
   {
      RestRequest request = RestContext.request;
        String AccountId= request.requestURI.substring(   request.requestURI.lastIndexOf('/')+1);
        Account result =  [SELECT Id,Name FROM Account WHERE Id = :AccountId];               
        return result;
   }
 
}
or
@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {
    @HttpGet
    global static Account getAccount() {
        RestRequest request = RestContext.request;
        // grab the AccountId from the mid of the URL
        String accountId = request.requestURI.substringBetween('Accounts/', '/contacts');
        Account result =  [SELECT Id,Name, (select Id,Name from Contacts)
                        FROM Account
                        WHERE Id = :accountId];
        return result;      
    }
}


 
ranveer singh 8ranveer singh 8
@amit actually i want the return type to be list of sobjects i.e both contact and account ....as per ur code the return type is account ..anyhow i got it buddy..thanks for help...really appriciating