Send a SMS Message from Apex

Prerequisites

The developer will need proficiency in:

  • Salesforce.com Object model 
  • Apex programming

The APIs’ can be worked on via this link

Apex is a strongly-typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce Lightning Platform server, together with calls to the API.

Object & Fields Information

There is a custom object in the SMSMagic Interact Managed package  known as SMS History,  and the corresponding API name is smagicinteract__smsMagic__c. This object stores SMS message data. Considering the need of complex customization for implementing various business workflows,  we have provided a simple way to send SMS from Apex code. 

This table contains fields that must be populated to successfully send messages from Apex. Continue reading below to see how to send a SMS message from Apex code.

NameField API namePurposeRequired
SenderIdsmagicinteract__SenderId__cPhonenumber or business identity of your businessYes
Mobile Numbersmagicinteract__PhoneNumber__cPhone number of Contact/Lead whom you are sending message to.Yes
Namesmagicinteract__Name__cName of person whom you are sending this message to.No
SMSTextsmagicinteract__SMSText__cContent of messageYes
Disable SMS on Triggersmagicinteract__
disableSMSOnTrigger__c
This is used to control trigger. If set 1, trigger is deactivated, so message won’t be sent out only record will be created. If set 0, message will be sent out. It should be 0 by default.Yes
External Fieldsmagicinteract__external_field__cThis is indexed and unique field used as a reference to update delivery reports.Yes
Object Typesmagicinteract__ObjectType__cIdentifier of the object from which message will be sent outNo
Directionsmagicinteract__Direction__cWith 1.49 onwards, this new field is used to indicate it’s an  outgoing or incoming message. Set its value as “OUT” for sending out message.Yes

Send an SMS message from Apex code

The developer would first create an instance of the SMS History object, populate all required fields, and then insert the instance of that object using a database insert. SMS-Magic provides a custom trigger that will (a) execute after insertion of the record and (b) send out messages to the Mobile Number. The trigger will also populate other fields in the SMS History object instance with default values.

This sample code sends an SMS messages. Feel free to copy it and modify it according to your environment.

List  smsObjectList = new List ();
String senderId = 'smsMagic'; // Please replace the 'smsMagic' with your relevant sender ID.
String templateText = 'test SMS by Screen Magic'; // you can fetch the template text by querying the record on smagicinteract__SMS_Template__c object
smagicinteract__smsMagic__c smsObject = new smagicinteract__smsMagic__c();
smsObject.smagicinteract__SenderId__c = senderId;
smsObject.smagicinteract__PhoneNumber__c = contact.MobilePhone;
smsObject.smagicinteract__Name__c = contact.Name; // records name
smsObject.smagicinteract__ObjectType__c = 'Contact'; // record type
smsObject.smagicinteract__disableSMSOnTrigger__c = 0; // this field either be 0 or 1, if you specify the value as 1 then sms will not get send but entry of sms will get create under SMS History object
smsObject.smagicinteract__external_field__c = smagicinteract.ApexAPI.generateUniqueKey();
smsObject.smagicinteract__SMSText__c = templateText;
smsObjectList.add(smsObject);
Database.insert(smsObjectList, false);

Troubleshooting

If you encounter any problems, consider the following:

  • Ensure that your code is not invoked from a scheduled method of any other trigger.
  • A user on whose behalf this code is executed must have permission to use SMS History objects.

Was This Article Helpful?

xTo test this chatbot, please submit your details on this page
Note: After submitting your details you will be redirected to this page and access chatbot