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
Varun ChopraVarun Chopra 

Apex Rest API issue

Hi

I tried to create a webservice using apex in salesforce. But when I am trying to access it from another class it is throwing following error.

[{"errorCode":"NOT_FOUND","message":"Could not find a match for URL /FieldCase"}]

My Webservice Code:
 
@RestResource(urlMapping='/FieldCase')
global with sharing class RESTCaseController {

@HttpGet   
  global static List<Account> createNewCase() {
       List<Account> company = [Select ID, Name, BillingState from Account where Name = 'AproposeInc'];
        return company;
  }
}

Class code in which I am accessing the the webservice
public with sharing class CallRestCaseController {
    
     public Pagereference callAPI()
    {
         ValidaCredenciais('reee ','eeee', null);
         return null;
    }
    
    
    public String ValidaCredenciais(String Usuario, String Senha, String SessionId) {
        try
        { 
       
            HttpRequest req = new HttpRequest();         
            Http http = new Http();         
            req.setMethod('GET');         
            req.setEndpoint('https://salesfoce.instance.name/services/apexrest/FieldCase');         
           // req.setBody('{"usuario": "' + Usuario + '", "senha": "' + Senha + '", "orgId": "' + System.Userinfo.getOrganizationId() + '"}');
            req.setHeader('Content-Type', 'application/json');
            req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
          //  req.setBody('{"companyName":"AproposeInc"}');
            HTTPResponse resp = http.send(req);
            String RetornoValida = resp.getBody();    
           
            system.debug('comingg ============= ???? '+RetornoValida);
            return RetornoValida;
         }
        catch(System.CalloutException e)
        {
        System.debug('Sorry, You have an error ' + e.getMessage());
        }
        return null;
     }

}

I have also added the salesforce instance name in remote sites. But still no success. I am accessing it in the same org.
Please help.

Also I have to access it for another org, then what should I do ??

Thanks in advance !
 
Himanshu ParasharHimanshu Parashar
Hi Varun,

You url is not mapped correctly. Update your code with following and then try to run from workbench (https://workbench.developerforce.com/restExplorer.php)
 
@RestResource(urlMapping='/FieldCase/*')


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.