BrightGen
BrightGen Limited
a bright new generation
of business solutions
News
Our news and articles of interest

Blog Archive

Search the Archive


  • 01

    Aug

Bookmark and Share


User-added image


One of BrightGen's app exchange offerings is BrightSMS. This is a free app that allows the sending of single or bulk SMS messages to UK or international numbers - you only pay for the messages that you send.

Earlier versions of BrightSMS provided functionality to send SMS messages from Visualforce pages, based on the user typing the message in and selecting a mobile number/contact/lead as the message recipient.   Version 3.0 introduces support for sending SMS from @future methods, which means that messages can be sent from triggers when record details change.

Getting Started

Installation and setup is covered in the Installation Guide.  Once you have installed the package, follow the User Guide to register an account.  The first account you register in your Salesforce org gets 10 free messages.  Note that the account is tied to a mobile phone number and you can only register a number once - so the 10 free messages is a single shot deal.  Once I've been through the installation I usually send myself an SMS through the packaged pages to confirm its all working.

The Code

My example trigger is on the case sobject, and sends an SMS to the contact associated with the case when the status changes, as long as the contact has supplied a mobile phone number. 
 

trigger Case_au on Case (after update)
{
	List<Id> caseIdsToSMS=new List<Id>();
	// pull back the contact for each case
	Set<Id> contactIds=new Set<Id>();
	for (Case cs : trigger.new)
	{
		if (null!=cs.contactId)
		{
			contactIds.add(cs.contactId);
		}
	}
	
	Map<Id, Contact> contsById=new Map<Id, Contact>();
	contsById.putAll([select id, MobilePhone from Contact where id in:contactIds]);
	
	for (Case cs : trigger.new)
	{
		if (null!=cs.ContactId)
		{
			Contact cnt=contsById.get(cs.ContactId);
			if ( (cs.Status != trigger.oldMap.get(cs.id).Status) &&
		    	 ( (null!=cnt.MobilePhone) && (cnt.MobilePhone.length()>0) )
		   	)
			{
				caseIdsToSMS.add(cs.id);
			}
		}
	}
	
	CaseUtils.SendCaseUpdatedSMS(caseIdsToSMS);
}

and the future method that sends the SMS:

public with sharing class CaseUtils 
{	
	@Future(callout=true)
	public static void SendCaseUpdatedSMS(list<Id> csIds)
	{ 
		for (Case cs : [Select CaseNumber, Contact.MobilePhone, Contact.FirstName, Contact.LastName, Status from Case where Id IN : csIds] )
		{ 
			BG_SMS.BrightSMSSubmitSMSMessage bsms = new BG_SMS.BrightSMSSubmitSMSMessage();
			bsms.brightSmsAccountId = '<AccountIdGoesHere>';
			bsms.rateCode = '1';
			bsms.senderID = 'BrightGen';
			bsms.mobileNumber = cs.Contact.MobilePhone.trim();
			bsms.smsMessage = 'Hi ' + cs.Contact.FirstName + 
							  ', Just to let you know the status of your case {' + 
							  cs.CaseNumber + 
							  '} has been changed to ' + 
							  cs.Status;
			bsms.submitSMSMessage();
   		}
	}
}


Replace AccountIdGoesHere with the id of the SMS account that you registered in the Getting Started section.  The senderId will be displayed as the sender of the SMS, so replace this with something specific to you or your company.  Finally, the smsMessage contains the message text that will be sent.  BrightSMS allows you to send up to a maximum of 459 characters, but these will be sent as 3 individual SMS messages, as the maximum individual SMS message size is 153 characters.

Note that as there are a limit of 10 callouts per transaction, if there are more than 10 case updates to be sent, this code will produce an error.  If you have a large amount of messages to send on a regular basis, its better to use Batch Apex and split the processing up into multiple transactions.

Sending Messages

First up, I create a contact in my org and supply a mobile number (note, this is not my real mobile number):
 

Screen Shot 2013 01 12 at 11 14 16


I then create a case and associate my contact record with it:
 

Screen Shot 2013 01 12 at 11 18 23

If the status of the case is then changed from New to Working, I automatically receive an SMS informing me of the change:

IMG 0705

 



BrightGen Ltd, Ginetta House, 155-157 Newland Street, Witham, Essex CM8 1BE   01376 520578 / 01376 519888   info@brightgen.com
Salesforce, salesforce.com, Sales Cloud, Service Cloud, Collaboration Cloud, Custom Cloud, Force.com and Chatter are trademarks of salesforce.com, inc.