Tuesday, January 17, 2017

How to Create Web Service(REST Resource) in Salesforce (Article by Sunil Sharma & Prateek Garg)



This Article aims to describe how to create a REST based Web Service in Salesforce using Apex. For demonstrating purpose we are considering the Salesforce Standard Object - CASE. We are going to expose CASE object to external world. You can expose the Salesforce Objects using Apex Class methods as a REST or SOAP web service operation. By making the class methods callable through the web, your external application can integrate with Salesforce to perform all CRUD Operations (Create, Retrieve, Update and Delete). You just need to consume the Web Service to integrate with any external system.

Here are some steps required to expose a CASE Object and Create a Web Service for the same:


1. Define class as ‘global’.
2. Annotate the class with  @RestResource(urlMapping='/<SObject>/*’). The URL mapping       is case-sensitive.
3. Define methods as ‘global static’ and add an annotation to associate it with an HTTP 
    method. For e.g @HttpGet is for pulling or retrieving the records, @HttpPost for creation       or insertion, @HttpDelete for deletion, @HttpPut for Update or Upsert and @HttpPatch         for only update.


Note : Each annotation can only be used once in an Apex class.

 Class Code
=============
@RestResource(urlMapping = '/Cases/*')
global with sharing class CaseManager
{
   @HttpGet
   global static List<Case> getCaseId()
   {
       RestRequest req = RestContext.request;
       List<Case> result = [Select CaseNumber, Subject, Status, Origin, Priority from Case];
       return result;
   }
   
   @HttpPost
   global static ID createCase(String subject, String status, String origin, String priority)
   {
       Case thisCase = new Case(Subject = subject, Status = status, Origin = origin, Priority = priority);
       insert thisCase;
       return thisCase.id;
   }
   
   @HttpDelete
   global static void deleteCase()
   {
       RestRequest req = RestContext.request;
       String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
       Case thisCase = [Select Id from Case where Id = :caseId];
       delete thisCase;
   }
   
   @HttpPut
   global static ID upsertCase(String subject, String status, String origin, String priority, String id)
   {
       Case thisCase = new Case(Id = id, Subject = subject, Status = status, Origin = origin, Priority = priority);
       //Match case by id, if present.
       //Otherwise, create new case.
       upsert thisCase;
       return thisCase.id;
   }
   
   @HttpPatch
   global static ID updateCaseFields()
   {
       RestRequest req = RestContext.request;
       String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
       Case thisCase = [Select id from case where id = :caseId];
       //Deserialize the JSON string into name-value pairs
       Map<String, Object> params = (Map<String, Object>) JSON.deserializeUntyped(req.requestBody.toString());
       //Iterate through each parameter field value
       for (String fieldName : params.keySet())
       {
           //Set the field and value on the Case Object
           thisCase.put(fieldName, params.get(fieldName));
       }
       update thisCase;
       return thisCase.id;
   }
}

6 comments:

  1. very usefull informatation.and iam looking for more posts like this please keep updating us........

    ReplyDelete
  2. Are you looking for Distance Learning Courses in India most of the students choose and apply, Talentedgenex there are many popular courses which attract the students for having distance education. For more info visit this site:- Distance learning courses in India ,

    ReplyDelete
  3. Talentedgenext Way of Online Learning, Distance Education, is an increasing number of becoming popular all over the world due as it has many benefits. For further details visit in this site:- Distance Education Website,

    ReplyDelete

Governor Limits Example-Too Many DML Statements :151

Governor Limits Example : Too Many DML Statements :151 (Sunil Sharma) Example:Code Hitting Governor Limits System.LimitExceptio...