Expose Rest Webservice :
Apex rest webservice code:
Apex rest webservice code:
/**=====================================
*Class Name : RestWebserviceExampleOne
*Test Class Name :
*Description : RestWebserviceExampleOne it is a rest webservice class.
*Developer Name : Pratiksha Narvekar
*Created Date : 31-10-2017
=====================================**/
@Restresource(urlmapping='/createLead/*')
global class RestWebserviceExampleOne {
global static list<Response> listRes = new list<Response>();
global class Response{
ID LeadID{get;set;}
string status{get;set;}
string message{get;set;}
}
@HttpPost
global static list<Response> RestMethod(String name, String ContactNumber, String EmailID){
if(!string.isBlank(name) && !string.isBlank(ContactNumber) && !string.isBlank(EmailID)){
Lead objLead = new Lead();
Response objRes = new Response();
objLead.LastName = name;
objLead.Email = EmailID;
objLead.MobilePhone = ContactNumber;
objLead.Company = 'test';
Database.SaveResult objSr = Database.insert(objLead, false);
if(objSr.isSuccess()){
objRes.LeadID = objSr.getID();
objRes.status = '200';
objRes.message = 'Lead created successfully...';
}
else{
for(Database.Error err : objSr.getErrors()) {
objRes.LeadID = null;
objRes.status = String.valueof(err.getStatusCode());
objRes.message = err.getMessage()+err.getFields();
}
}
listRes.add(objReq);
}
return listRes;
}
}
Connected App:
Steps :
1.Click on setup
2.Search Remote access
3.Create new Connected App,here you will get consumer key and secret which is needed for authentication.
Testing in Postman :
first u need to generate access_token.
Request URL :
https://ap5.salesforce.com//services/apexrest/createLead/
Request body :
{
"name" : "pratiksha",
"ContactNumber" : "9049121212",
"EmailID" : "pratiksha@gmail.com"
}
Response body :
[
{
"status": "200",
"message": "Lead created successfully...",
"LeadID": "00Q7F000002hJoIUAU"
}
]
Consume Rest Webservice in SFDC with platform events:
First we will create platform event. To create platfrom event go to setup and search 'Platform Events'.
Click on 'New Platfrom Event' and create new platfrom event.
It will look like this:
If you want to call any webservice in sfdc from trigger then you need to add either @future annotation on method or need to write batch class.
Here is the simple example of apex class with future annotation. Here I am passing request in form of JSON string.
Note : before testing this don't forgot to add endpoint URL in remote site.
/**=====================================
*Class Name : ConsumeRestAPI
*Test Class Name :
*Description : ConsumeRestAPI.......
*Developer Name : Pratiksha Narvekar
*Created Date : 03/05/2019
=====================================**/
global class ConsumeRestAPI{
@future(callout=true)
public static void CreatePayload(string sObjectWithOperation){
system.debug('...inside method...'+sObjectWithOperation);
String iNull ='null';
JSONGenerator gen = JSON.createGenerator(true);
string correlationId = sObjectWithOperation.substringBetween('cStart','cEnd');
string sObjectID = sObjectWithOperation.substringBetween('iStart','iEnd');
string EntityType = sObjectWithOperation.substringBetween('eStart','eEnd');
string OperationType = sObjectWithOperation.substringBetween('oStart','oEnd');
string mergeToId = sObjectWithOperation.substringBetween('rStart','rEnd');
string parentId = sObjectWithOperation.substringBetween('pStart','pEnd');
string userEmail = sObjectWithOperation.substringBetween('uStart','uEnd');
string timeStamp = sObjectWithOperation.substringBetween('tStart','tEnd');
try{
if(sObjectWithOperation != ''){
gen.writeStartObject();
gen.writeStringField('entity',EntityType);
gen.writeStringField('id',sObjectID);
gen.writeStringField('operation',OperationType);
gen.writeStringField('mergeToId',mergeToId);
gen.writeStringField('parentId',ParentId);
gen.writeStringField('userEmail',userEmail);
gen.writeStringField('timeStamp',timeStamp);
gen.writeStringField('correlationId',correlationId);
gen.writeEndObject();
}
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://jsonplaceholder.typicode.com/posts');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody(gen.getAsString());
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 200) {
System.debug('...The status code returned was not expected:... ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug('...response....'+response.getBody());
}
System.debug('... Account Structure:...'+gen.getAsString());
}
catch(Exception e){
System.debug('...Exception .....'+e);
}
}
Helper class :
/**=====================================
*Class Name : SFDC_BLOGTriggerHelper
*Test Class Name :
*Description : SFDC_BLOGTriggerHelper.......
*Developer Name : Pratiksha Narvekar
*Created Date :
=====================================**/
public with sharing class SFDC_BLOGTriggerHelper {
public static void CreateAccountMethod(list<SFDC_BLOG__e > newEventList){
String input;
if(!newEventList.isEmpty()){
for(SFDC_BLOG__e objCW : newEventList){
input = 'iStart'+objCW.Id+'iEnd';
input = input+'eStart'+ userinfo.getUserEmail()+'eEnd';
}
if(input != null && input !=''){
ConsumeRestAPI.CreatePayload(input);
}
}
}
}
*Test Class Name :
*Description : SFDC_BLOGTriggerHelper.......
*Developer Name : Pratiksha Narvekar
*Created Date :
=====================================**/
public with sharing class SFDC_BLOGTriggerHelper {
public static void CreateAccountMethod(list<SFDC_BLOG__e > newEventList){
String input;
if(!newEventList.isEmpty()){
for(SFDC_BLOG__e objCW : newEventList){
input = 'iStart'+objCW.Id+'iEnd';
input = input+'eStart'+ userinfo.getUserEmail()+'eEnd';
}
if(input != null && input !=''){
ConsumeRestAPI.CreatePayload(input);
}
}
}
}
Handler Class:
/**=====================================
*Class Name : SFDC_BLOGTriggerHandler
*Test Class Name :
*Description : SFDC_BLOGTriggerHandler.......
*Developer Name : Pratiksha Narvekar
*Created Date :
=====================================**/
public with sharing class SFDC_BLOGTriggerHandler{
public void PlatformEventsMethod(List<SFDC_BLOG__e > listP){
SFDC_BLOGTriggerHelper.CreateAccountMethod(listP);
}
}
Trigger :
trigger SFDC_BLOGTrigger on SFDC_BLOG__e (after insert) {
SFDC_BLOGTriggerHandler objCTH = new SFDC_BLOGTriggerHandler ();
objCTH.PlatformEventsMethod(trigger.new);
}
good
ReplyDelete