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
Nikhil Jaitly 8Nikhil Jaitly 8 

Service Not Found : Rest

I have created a simple REST service which fetches the Account Id,Name and the associated Contacts. Whenever I try to reach this web service from Workbench using this URI : services/apexrest/checkCMT/Accounts/0019000001NQXpJ/contacts , it gives the error 'Service Not Found'

where checkCMT is the namespace I am using in my org.

Here is the code that I have written.

@RestResource(urlMapping='/Accounts/<Account_ID>/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;
        
    }


}
Best Answer chosen by Nikhil Jaitly 8
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= demo.substringBetween('Account/', '/Contacts');
        Account result =  [SELECT Id,Name FROM Account WHERE Id = :AccountId];               
        return result;
   }
 
}
Please check below post for similer issue
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BZhiIAG

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

Let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

Just replace your rest resource URL with this and then try:

@RestResource(urlMapping='/Account/*/Contacts/')
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= demo.substringBetween('Account/', '/Contacts');
        Account result =  [SELECT Id,Name FROM Account WHERE Id = :AccountId];               
        return result;
   }
 
}
Please check below post for similer issue
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BZhiIAG

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

Let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Your API

@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;      
    }
}

Your URL should be :- /services/apexrest/Account/0019000001NQXpJ/Contacts
Method :- Get

No need to NameSpace in URL

Let us know if this will help you

Thanks
Amit Chaudhary
Nikhil Jaitly 8Nikhil Jaitly 8
Hi,

I changed my url in RestResource as : urlMapping='/Accounts/*/contacts' and had to use the namespace to get it working.

So the URI that I used was : /services/apexrest/checkCMT/Accounts/0019000001NQXpJ/contacts

where checkCMT is my namespace.


Thank you very much for your help.


Nikhil
Nikhil Jaitly 8Nikhil Jaitly 8
Hi Amit,

I did try the URI without the namespace as well, but it didn't work.

Anyways, your suggestion on changing the url did the trick. Thanks

Nikhil
sachin kumar 175sachin kumar 175
Hi Nikhil,
Thank you for introducing the namespace, i also added the name space in the url and its working fine.
this is my url :  /services/apexrest/Sachin1/AccountManagementService/
"Sachin1" is the namespace i have created in lightning.