You need to sign in to do that
Don't have an account?
ranveer 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
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
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 :- or