Last active
May 4, 2017 01:57
-
-
Save DiyahM/7b298a459f8048de60ed to your computer and use it in GitHub Desktop.
Salesforce Apex Trigger Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Apex trigger | |
trigger Accepted on Training__c (before update) { | |
for (Training__c training : Trigger.new) { | |
Training__c oldTraining = Trigger.oldMap.get(training.id); | |
Boolean oldAccepted = oldTraining.Accepted__c.equals('YES'); | |
Boolean newAccepted = training.Accepted__c.equals('YES'); | |
if (!oldAccepted && newAccepted) { | |
SendNewParticipant.sendParticipant(JSON.serialize(training)); | |
} | |
} | |
} | |
//Apex class | |
public class SendNewParticipant{ | |
@future(callout=true) | |
public static void sendParticipant(String participant) | |
{ | |
//Replace below URL with api endpoint | |
String addr = 'https://example.com/salesforce_connector/new_participant'; | |
HttpRequest req = new HttpRequest(); | |
req.setEndpoint( addr ); | |
req.setMethod('POST'); | |
req.setHeader('Content-Type','application/json'); | |
req.setHeader('Accept', '*/*'); | |
JSONGenerator generator = JSON.createGenerator(true); | |
generator.writeStartObject(); | |
generator.writeObjectField('participant', participant); | |
generator.writeEndObject(); | |
req.setBody(generator.getAsString()); | |
Http http = new Http(); | |
HttpResponse response = http.send(req); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment