Trigger for sending SMS on Lead update

May 16, 2014

Send SMS on New Lead Creation

Salesforce does not allow any custom action on workflow rules. So, for implementing such workflow rules we can use triggers.

Trigger for sending SMS on Lead update
Below is an example, where SMS will be sent as soon as a lead is updated.
* @Trigger : bouncedTrigger
* @description : This is Trigger for sending SMS to Lead when
* its custom checkbox field bounced__c is changed from false to True.
* bounced__c is checkbox custom field on Lead object.
trigger bouncedTrigger on Lead (after update) {
List<smagicinteract__smsmagic__c> smsObjects = new List<smagicinteract__smsmagic__c>();
String tplText = null;
Id leadId = null;
//Get list of leads which are getting updated.
List<Lead> leadList =;</code>

for (Lead lead : leadList){
leadId =;

//It gives previous instance of Lead object.
Lead beforeUpdateLead = System.Trigger.oldMap.get(lead.Id);
// when bounced__c custom check box value set to true from false then only it will send an SMS to that updated Lead.
// here we are checking old and new value of bounced__c
if( beforeUpdateLead.bounced__c == false && lead.bounced__c == true){
if(lead.MobilePhone != null){
//template text to be send
tplText = ‘test SMS for change in Lead..’;
//create object of SMS history
smagicinteract__smsMagic__c smsObj = new smagicinteract__smsMagic__c();
smsObj.smagicinteract__PhoneNumber__c = lead.MobilePhone;
smsObj.smagicinteract__SMSText__c = tplText;

smsObj.smagicinteract__senderId__c = ‘smsMagic’;
smsObj.smagicinteract__Name__c = lead.Name;
insert smsObjects;
Test class for bouncedTrigger trigger:
* This class is test class for bouncedTrigger. This can vary as per your org.
public with sharing class bouncedTriggerTestClass {

static testMethod void testbouncedTrigger() {
Lead lead = new Lead();
lead.LastName ='test lead';
lead.Company='SMS Magic';
insert lead;
Lead leadObj = [select Id, Name, bounced__c from Lead where Id =: lead.Id];
update leadObj;

Related Posts

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>