Adding Apex Class for Automated Messaging
- Navigate to
Setup
. - In the Quick Find box, type
Apex Classes
. - Click on
Apex Classes
underDevelop
. - Click on
New
to create a new Apex Class. - Paste the following Apex code:
global class AutomatedSendSMS {
public class SendSMSRequest {
@InvocableVariable(label='Phone Number' required=true)
public String phoneNumber;
@InvocableVariable(label='SMS Message' required=true)
public String smsMessage;
}
@InvocableMethod(label='Send SMS' description='Send an SMS message.')
public static List<SendSMSResult> sendSMSFromFlow(List<SendSMSRequest> requests) {
List<SendSMSResult> results = new List<SendSMSResult>();
crm_messaging__CRM_Messaging_Settings__c settings = crm_messaging__CRM_Messaging_Settings__c.getInstance();
String bearerToken = settings.crm_messaging__token__c;
String fromNumber = settings.crm_messaging__fromnum__c;
for(SendSMSRequest request : requests) {
sendSMSAsync(request.phoneNumber, request.smsMessage, bearerToken, fromNumber);
results.add(new SendSMSResult());
}
return results;
}
@future(callout=true)
global static void sendSMSAsync(String phoneNumber, String message, String apiToken, String fromNumber) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://app.crm-messaging.cloud/index.php/Api/sendMsg');
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer ' + apiToken);
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String body = 'to=' + EncodingUtil.urlEncode(phoneNumber, 'UTF-8');
body += '&msg=' + EncodingUtil.urlEncode(message, 'UTF-8');
body += '&fromnum=' + fromNumber;
request.setBody(body);
HttpResponse response = http.send(request);
}
public class SendSMSResult {
@InvocableVariable(label='API Response')
public String apiResponse;
@InvocableVariable(label='Message Sent')
public Boolean messageSent;
}
}
This class enables the functionality of sending SMS through Process Builder and Flows.
Using in Process Builder
- Open your Process Builder where you want to implement SMS sending.
- In the actions section, you should now see an option for
Send SMS
. - Configure this action by specifying the Phone Number and the SMS Message. You can set these values using field references or static values.
Using in Flows
- Open your Flow where you want to implement SMS sending.
- In the elements panel, drag and drop an
Action
element into the canvas. - Configure this action just like in Process Builder, specifying the Phone Number and the SMS Message.
By implementing this, you can automate the sending of SMS or other types of messages directly from Salesforce Process Builder or Flows.
Note
- Make sure you have set the API token, From Number, and other necessary settings in your
CRM Messaging Custom Settings
for this to work as expected.