Listen to this article

Send messages from Apex

How to Send Messages from Apex

Automation has paved several ways to accelerate business operations. And one of the crucial instances of automation in business is text automation. Automated texts have enabled employees to send texts at a defined time for meaningful interaction without being rigorously involved in text operations. This further helps employees to save time and focus on core business priorities. For automation, Salesforce offers various tools like Process Builder, Flows, Workflows, etc. Users can also send messages from Apex to automate text messages through coding.

What is Apex?

Apex stands for Advanced Programming Experience in Salesforce. It is an object-oriented programming language developed by It allows developers to write code that works on the platform and create SAAS applications accessing client-server interfaces and’s back-end database. 

Besides, developers can make API calls to and execute flow and transaction control statements using Apex. Users can initiate Apex Code from triggers on objects and Web service requests.

When to use and Send Messages from Apex

Users can automate and send messages using a process builder. Also, Flow builder is one of the Salesforce automation tools that help to automate a guided visual experience and start a behind-the-scenes business process. But, users can count on Apex when they need more capabilities than what is provided by Flow Builder. They can use Apex to accommodate more complex automation use-cases.

How to Send Messages from Apex

Apex class can be executed in Process Builder, Flows, or trigger code.

Certain parameters need to be defined to send messages using Apex.

The Apex classes accept parameters in a comma-separated string.


  • Select action name as send SMS APEX


  • Select Apex class as send SMS from Process Builder


  • Set Apex variable and define parameters.


Param1: Add the Id of the primary object from which you are triggering the message. This should match your template object. Also, the outbound SMS will relate to this Salesforce primary object only. 

Param2: The API name of the phone field for that object or an actual valid phone number.

Param3: This parameter can be any one of the three-  

  • An SMS template Id, or
  • A question Id, or
  • A string

Template Id: 

You can extract a Template Id from the URL of an SMS template. Its object should match the Salesforce object defined in Param1. 

Question Id: 

Add a question Id of the first Survey question, which you want to trigger. After triggering the Survey’s first question, all responses will be handled automatically. 

String (Straight Text): 

You can add a string in this parameter if you do not want to use templates. This works well for simple text messages. The string also supports ‘Merge tags’. For example, Okay {Contact.firstname}. This is a message template.

Param4: Add an outbound phone number that you want to use to send an outbound text. It will pick the default phone number for the org or the first phone number mentioned in the 360 SMS user configuration tables if Param4 is blank.

After defining parameters, you are all set to automate messages.

Pick the Best-Suited Tool and Minimize Effort

Though there are different tools available in Salesforce to automate text messages, every tool has its own importance and unique capabilities. With adequate knowledge of tools like Process Builder, Apex code, etc., business ventures can use the appropriate method to automate and send text messages. This helps them pick the best-suited tool, minimize team effort and perform text operations at a much faster rate.

To know more about no-code and standard automation in Salesforce, turn to our experts at or contact us here.