Exposing salesforce data through Apex REST API

Exposing salesforce data through Apex REST API, apex rest api, sfdcpanda, sfdc panda, salesforce basics, salesforce tutorial, salesforce baisc examples, basics of salesforce

Exposing salesforce data through Apex REST API:-

By using APEX REST API we can build a light weight API to access salesforce data through either XML or JSON communication medium from third party application.

APEX REST API supports CRUD (create, read, update and delete) operation through its GET,POST,DELETE,PUT,PATCH HTTP methods.

HTTP GET METHOD :- Requests data from salesforce.

HTTP POST METHOD :- Submits data to be processed(creates a new record) into salesforce.

HTTP DELETE METHOD :- Performs deletion(Deleting a record) operation into salesforce.

HTTP PUT AND PATCH METHOD :-PUT and PATCH can be utilized to modify and update existing records in salesforce.

 

What is Apex REST endpoint ?

You want your Java application should create a case record in salesforce through REST API, For that you need to build an REST API Endpoint URL in salesforce where java application can access that URL and can do CRUD(create, read, update and delete) operation.

How to create Apex REST endpoint ?

Before start writing Apex class, you need to annotate “@RestResource” and define the urlMapping by giving a name like below,

@RestResource(urlMapping='/RESTCaseAccess/*')

Once you done with defining urlMapping, then your custom REST API url is ready for use and it looks like below

“https://YourserverInstance.salesforce.com/services/apexrest/RESTCaseAccess

then the global Apex class can declare different methods depending on if the incoming request is an HTTP GET, POST, PUT or DELETE request.

HTTP POST REST API Example :-

@RestResource(urlMapping='/RESTCaseAccess/*')
 global with sharing class RESTCreateCase {

@HttpPost
 global static String createNewCase(String companyName, String caseType) {

List<Account> company = [Select ID, Name, BillingState from Account where Name =:companyName];
 Case c = new Case();
 c.OwnerId = '00541000000dxZM';
 c.AccountId = company[0].Id;
 c.Subject = caseType + ' for '+companyName;
 c.Status = 'Open';
 c.Type = caseType;
 c.priority = 'High';
 c.origin = 'Phone';
 insert c;
 return 'Case created ';
 }
 }

Testing this Example in Workbench REST Explorer :-

You can test the above example in Workbench REST Explorer which is available under ‘Utilities’ in top section.

Choose the HTTP Method as POST and provide this Json string in body content,

{“companyName” : ” Give exact Account name from your org”, “caseType” : “”}

Note : Json parameter must match with apex parameter.

 

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*