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
s_macs_mac 

Steps to call apex rest service from workbench rest explorer

I have created apex rest service and  I am calling it from workbench rest explorer,but I am getting an error
Created the following class 
@RestResource(urlMapping='/Account/Contacts')
global with sharing class AccountManager{
  @HttpGet
   global static Account getAccount(){
      RestRequest request = RestContext.request;
      // grab the caseId from the end of the URL
        String AccountId= request.requestURI.substring(
          request.requestURI.lastIndexOf('/')+1);
        /*Account result =  [SELECT Id,Name,(Select Name from Contacts) FROM Account
                        WHERE Id = :AccountId];  */
          Account result =  [SELECT Id,Name FROM Account WHERE Id = :AccountId];               
        return result;

   }
 
}

And then when

I am giving the url as /services/apexrest//Account/Contacts/<AccountID>
Error is: Service not found at: /services/apexrest/Account/Contacts/001......

Can some body please help me,why is this error or  with the steps to followed..

Thanks in Advance... 
Best Answer chosen by s_mac
Amit Chaudhary 8Amit Chaudhary 8
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;
   }
 
}
I just added * in your URL


Your URL should be :- /services/apexrest/Account/Contacts/<AccountID>
Method :- Get

Ley us know if this will help you

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
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;
   }
 
}
I just added * in your URL


Your URL should be :- /services/apexrest/Account/Contacts/<AccountID>
Method :- Get

Ley us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
Pankaj_GanwaniPankaj_Ganwani
Is there any namespace defined in your org? If this is defined, then you will have to use below mentioned syntax to access your service:

/services/apexrest/namespace/Account/Contacts/001...
CloudGeekCloudGeek
I am giving the url as /services/apexrest//Account/Contacts/<AccountID>
Error is: Service not found at: /services/apexrest/Account/Contacts/001......

Remove that extra "/" and try like below :
/services/apexrest/Account/Contacts/<AccountID>
 
s_macs_mac
 if my url is @RestResource(urlMapping='/Account/<AccountID>/Contacts/'),how can I get the Account ID from the URL.
CloudGeekCloudGeek
Hello s_mac,

Try this :
 
String accId = req.requestURI.substringBetween('Account/', '/Contacts');



 
Amit Chaudhary 8Amit Chaudhary 8
Hi Mac,

I will recommend you to please start new thread for new issue. So that other can also help you

Well you can try below code.
@RestResource(urlMapping='/Account/*/Contacts/')
global with sharing class AccountManager
{
  @HttpGet
   global static Account getAccount()
   {
      RestRequest request = RestContext.request;
        String AccountId= demo.substringBetween('Account/', '/Contacts');
        Account result =  [SELECT Id,Name FROM Account WHERE Id = :AccountId];               
        return result;
   }
 
}

Let us knw if this will help you

 
s_macs_mac
Thanks every one for ur reply,its working fine...
Charles FinemanCharles Fineman
Amit will rightly criticize me for adding on to the already tangential comment but... recently the following capability was released:

GET /services/data/v36.0/sobjects/Account/{id}/contacts

More info here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_relationships.htm
Anshu Kumar 19Anshu Kumar 19
Hello,
I am having similar problem. I am making POST request to my REST API. I also have managed package. I amm getting the following error with workbench : Service not found at: /services/apexrest/My_Package/zendeskAPI
@RestResource(urlMapping='/zendeskAPI/*')
global with sharing class zendeskAPI {
    @HttpPost
    global static String doPost(){
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String reqBody = req.requestBody.toString();
        return  reqBody;
    }
}
can anyone please help
Yuval Vardi 15Yuval Vardi 15
Got the same error - took me some time till I figured out that I am updating the Case status with the wrong value => should be => Status = 'Closed' in order for the IsClosed to be true.