Sunday, 2 September 2012

Handling Exceptions In REST API wrappers in salesforce and setting proper HTTP error in header of HTTP response sent from Salesforce

To set the proper error code in the response packet returned from salesforce there is default HTTP error code table

The documentation of valid http error codes supported is in the http response is accessed on clicking below Documentation on HTTP error codes

To handle this in code here is a below simple example that demonstrates how to set the HTTP error codes in salesforce

@RestResource(urlMapping='/Account/*')
global with sharing class accountrest {
@HttpGet
global static Account getMerchandiseById() {
RestRequest req = RestContext.request;
RestResponse res=RestContext.response;
Account result;
String accId = req.requestURI.substring(
req.requestURI.lastIndexOf('/')+1);
 try{
  result =[SELECT Name,AccountNumber,NumberofLocations__c FROM Account WHERE Id = :accId];
}
catch(exception e){
System.debug(e.getMessage());
res.statusCode=400;//clear that error was due to account ID missing and hence BAD request
}
return result;
}

2 comments:

  1. Thankyou for sharing this.
    Web Designing Company In Chandigarh-Robtechworld cares about understanding you and your customers.
    Website Designing Company In Chandigarh

    ReplyDelete
  2. Thank you for sharing this.
    The objective of this PG Diploma course is to provide the candidates with the best Detail knowledge and skills in the Process Safety Engineering discipline to facilitate faster learning curves while on the job.
    Process Safety Engineering Course

    ReplyDelete

Introducing Lightning Base Components

Lightning Base Components are great addition to the platform and in fact revolutionary .One of the concerns around lightning component ...