Download Brochure

Apex Rest Web Services

views
image-1

Introduction

Integration is possible in two ways one is we can Make a callout to external services from salesforce and get a service like get a data or post a data or we can Use our apex class as Restful webservice and expose it to external Systems as resources.

The Api for our exposed web service is simply the extension to the base API endpoint  with the url mapping we provide in the class declaration.  It is a Open/public APIs. APIs are basically set of rules for communication between applications and systems. Endpoints are the locations from where we want to reach through a callout or the address of the service we are trying to hit. 

Apex as a web service


We can expose our apex class as a web service to external systems which can make a callout to our apex class and take the service. Service may have different types of task that class can perform according to the functionality and purpose of the class itself. We can have different methods for different tasks inside our apex class like Get , post , Patch , delete . We can define all this method inside the class and external systems can make use of this method by making a callout. We can basically Expose it as either Rest or Soap services .

How to Expose our class as Restful web Service

Exposing our apex class as a web service is pretty simple we have to follow -

The few mandatory steps we define our class as global,  define methods as global static , use Http Anotations before class and method name.

Apex Restful class :


@RestResource(urlMapping = ’/Lead/ * ‘)
Global with sharing class NewRestClass{
@Httpget
Global static Lead getRecord(  ) {
//sample code here 
}
}
 
@RestResources
The first line before declaring class has RestResource Annotaion which tells that our class is available as web service and the urmapping inside the brackets define which particular object we want to external system to interact with our salesforce org.
 
@HttpGet 
This annotation is before the method declaration that tells which type of function a that method performs if it is GEt ,Post ,Patch.
Along With Get we Also have annotations for other task @HttpPost , @HttpPatch ,@HttpDelete .
For parsing the incoming link through the callout we use Json deserialize method to take the object name or take any fields or parameters that has been sent with the link and Request. The request we receive include the detail of the transaction and we deserialize the Request to get Request body and  information needed for the particular task. 
The methods in an invokable class also has a return type it returns a response with response body , response.Status and Response.Code , ID or the Object itself.

About @HttpPost


If we are making a , Post Request to the method we have Request body mandatorily in the Request as it tells the method we want to use and what we want to post , delete ,patch , upsert in the Salesforce. An example of a Simpe Post method Declaration is show here below :
 
@HttpPost
Global static Id createAccount(string AccountName , string  website )
{
Account acc = new Account( ) ;
acc.Name= AccountName;
 
Acc.website = website ;
 
Database.Saveresult Sr = database.Insert(Insert acc)
if(Sr == true)
{
//More code 
}
Return acc.Id;
}
}


Comments

Avatar

Leave a Comment

Your email address will not be published. Required fields are marked *

Want to Know More?

Are you interested in learning more about our business and what we offer? Feel free to reach out!