I am creating a Apex trigger on a custom object in SF. Right now I have the object Appointment that I would like to trigger a callout when the a new record is saved. Due to our internal processes, we do not need to worry about updates. Only new records.
That being said, I have the basics of the trigger and class created and they work. The trigger runs the class that is labeled as Future to run async. However, the class is where I get lost.
I want to pass some variables into the code from the record in the Appointment object that was being created. My code sends a HTTP POST off to a service that sends a SMS to the customer. I want to use the phone number, customer name and a message that has the appointment date and time in the message. The fields this data is stored in are:
i360__Prospect_Phone__c
i360__Correspondence_Name__c
i360__Start__c
i360__Start_Time__c
So for example, I need phone number and name to transfer into the class code below. As far as the message, I would like to to be sent out in a string with the variables in it. Example: "Hello i360__Correspondence_Name__c, your appointment with COMPANY has been scheduled on i360__Start__c at i360__start_Time__c. Please respond or call if you have any questions."
Here is my trigger code:
trigger sendtext on i360__Appointment__c (after insert) {
System.debug('Making future call to update account');
for (i360__Appointment__c app : Trigger.New) {
PodiumText.updateAccount(app.Id, app.Name);
}
}
Here is my Class code:
public class PodiumText {
//Future annotation to mark the method as async.
@Future(callout=true)
public static void updateAccount(String id, String name) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.podium.com/api/v2/conversations');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Accept', 'application/json');
request.setHeader('Authorization', 'IDSTRING');
request.setBody('{"customerPhoneNumber":"PHONENUMBER","message":"Testing","locationId":"49257","customerName":"CORRESPONDENCENAME"}');
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}
Any help would be great.