function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Natasha Ali 3Natasha Ali 3 

First RESTful API integration HELP!!!

Hi,
I'm completely new to development and I need to develop an integration between Salesforce and a third party service (BKSB). Salesforce needs to consume the REST API. 

I need to write a trigger that will make a callout when a status changes on the custom 'Placement__c' object from '8 - Offer Accepted' to '9 - Induction Booked'. I have the following code so far (asked another dev):
trigger PlacementTrigger on Placement (before update) {
 Map<Id,Placement__c> newPlacList1=new Map<Id,Placement__c>();
 Map<Id,Placement__c> oldPlacList2=new Map<Id,Placement__c>();
 List<Placement__c> newPlacementList=new List<Placement__c>();
 newPlacList1=trigger.new;
 oldPlacList2=trigger.old;
    if(Trigger.IsUpdate && Trigger.isbefore ){
 for(Id placId : newPlacList1.keySet()){
  if(oldPlacList2.get(placId).Status__c == '8 - Offer Accepted' &&
        newPlacList1.get(placId).Status__c == '9 - Induction Booked'){
  newPlacementList.add(placList1.get(placId));
  }
 }
  PlacementTriggerHandler.handlerFunction(newPlacementList); 
    }
}
To identify the correct user's data to GET from the BKSB, the unique identifier is the email address on the Salesforce record. How do I incorporate this so the correct record is chosen in BKSB?
And where do I go from the trigger?

ANY help is much appreciated!!
Many Thanks,
Natasha 
 
Naren9Naren9
Hi Natasha,
Trigger is to capture the event do a callout to external system.
In the above trigger, you have to call the external Rest API. So to call the BKSB Rest API, you have to import the WSDL of BkSB into Salesforce, then it will create a Apex class. 
In the above trigger, call that apex class to connect and send the data to BKSB.

First do the below trailhead tutorial, so that you will get the good idea of apex callout.

https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

Thanks,
Naren
NehaDabas16NehaDabas16
Hi Natasha,

You will have to use future methods to make REST callout to the external system. Following is a code model for that:
 
//Trigger
trigger PlacementTrigger on Placement (before update) {
	if(Trigger.IsUpdate && Trigger.isbefore ){
		PlacementTriggerHandler.findInductionBookedPlacements(trigger.New,trigger.oldMap); 
    }
}

//Trigger handler class
public with sharing class PlacementTriggerHandler(){
	public static void findInductionBookedPlacements(List<Placement__c>  newPlacList, Map<Id,Placement__c>  oldPlacMap){
		
		Set<Id> inductionPlacementIdSet=new Set<Id>();
		
		for(Placement__c plac : newPlacList){
			if(oldPlacMap.get(placId).Status__c == '8 - Offer Accepted' && plac.Status__c == '9 - Induction Booked')
				inductionPlacementIdSet.add(plac);
		}
		
		if(inductionPlacementIdSet.size() > 0){
			// call future method
			//As triggers cannot make callouts, we need to call an asynchronous future method to make the callout. 
			//As future methods can only accept primitive data types as parameters, we are passing a set of Ids for Placement records and not the list of records
			calloutSerive.makeCalloutBKSB(inductionPlacementIdSet);
			
		}
	}
}

// callout class
public with sharing class calloutSerive(){
	
	//future method to make the callout
	@future(callout = true)
	public static void makeCalloutBKSB(Set<Id> placementIdSet){
	
	//fetch the placement records using placementIdSet
	
	//Construct the Httprequest
	
	//Make callout
	
	//handle response
	
	
	}
}

Please go through the Apex REST Callouts unit from : https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services trailhead module. Also as far as uniquely identifying the records in BKSB is concerned, they should let you know the JSON format for the request. for example a sample JSON request may look like this:
"placmentList" :[    "placement":{
                                "email" : "ajax@gmail.com",
                                "name" : "ajax",
                                "institute" : "XXX University"
                                },
                    "placement":{
                                "email" : "zenon@gmail.com",
                                "name" : "zenon",
                                "institute" : "YYY University"
                                },
                    "placement":{
                                "email" : "beta@gmail.com",
                                "name" : "beta",
                                "institute" : "ZZZ University"
                            }
                ]

If this helps, please like and mark resolved.