Send SMS Using Batch Class in Salesforce
Que: Integrate SMS Functionality in salesforce using batch Class
Requirement: Send Sms to user if user has birthday today.
Solution: Fristly Create a batch class and schedule this class every day
//Note: you need to create Custom Setting for Username password sender id and etc
//and also Add Url in Remote Site Setting
global class ClassName implements Database.Batchable<Sobject>,Database.AllowsCallouts
{
List<Contact> lstofContactBirthdate = new List<Contact>();
static CustomSettingName__c objCS = CustomSettingName__c.getOrgDefaults();// Custom setting
List<string> lstofSMSString;
//Get date and Month from Today Date
Date currentdate = system.Today();
Integer TodayDay = currentdate.Day();
Integer TodayMonth = currentdate.month();
String CurrentDayandMonth = TodayDay+'/'+TodayMonth;
global Database.Querylocator start(Database.Batchablecontext BC)
{
// Birth_Day_and_Month__c This is formula field this get Day and Month of Birthdate
// Ex Birthday : 24/04/2005 then it return 24/04
system.debug('CurrentDayandMonth&&&'+CurrentDayandMonth);
return Database.getQueryLocator([Select id,Name,Birthdate,
from Contact where
and Birth_Day_and_Month__c = :CurrentDayandMonth
]);
}
global void execute(Database.Batchablecontext BC,List<sobject> scope)
{
string strPhoneNo = '';
list<contact> lstofContact = [Select id,Birthdate ,Mobile_No_2__c,Phone,
Birth_Day_and_Month__c
from Contact where id in:scope];
string strContent = '';
if(lstofContact != null && lstofContact.size() > 0)
{
system.debug('lstofContact'+lstofContact);
for(Contact objCon : lstofContact)
{
if(objCon.Birth_Day_and_Month__c != null && objCon.Birth_Day_and_Month__c == CurrentDayandMonth)
{
lstofContactBirthdate.add(objCon);
}
}
if(lstofContactBirthdate != null && lstofContactBirthdate.size() > 0)
{
lstofSMSString = new List<string>();
strContent = 'Happy B\'day ';
for(Contact objCon : lstofContactBirthdate)
{
if(objCon.Phone != null)
{
lstofSMSString.add(objCon.Phone);
}
else if(objCon.Mobile_No_2__c != null)
{
lstofSMSString.add(string.valueof(objCon.Mobile_No_2__c));
}
}
if(lstofSMSString != null && lstofSMSString.size()>0)
{
for(string str : lstofSMSString)
{
strPhoneNo += str +',';
}
string URLSMS = 'http://UrlProvidedBYClint='+objCS.User_Name__c+'&passwd='+objCS.Password__c+'&mobilenumber='+strPhoneNo+'&message='+EncodingUtil.urlEncode(strContent,'UTF-8')+'&sid='+objCS.Sender_Id__c+'&mtype=N&DR=Y';
system.debug('@@@@'+URLSMS);
SendSMS(URLSMS);
}
}
}
}
public void SendSMS(string URLSMS)
{
try{
HttpRequest req = new HttpRequest();
req.setEndpoint(URLSMS);
req.setTimeout(60000);
req.setMethod('GET');
Http httpC = new Http();
HTTPResponse res = new HTTPResponse();
res = httpC.send(req);
System.debug('res: '+res.getBody()+'getStatus:: '+res.getStatus());
}
catch(exception ex)
{
}
}
global void finish(Database.Batchablecontext BC)
{
}
}
Requirement: Send Sms to user if user has birthday today.
Solution: Fristly Create a batch class and schedule this class every day
//Note: you need to create Custom Setting for Username password sender id and etc
//and also Add Url in Remote Site Setting
global class ClassName implements Database.Batchable<Sobject>,Database.AllowsCallouts
{
List<Contact> lstofContactBirthdate = new List<Contact>();
static CustomSettingName__c objCS = CustomSettingName__c.getOrgDefaults();// Custom setting
List<string> lstofSMSString;
//Get date and Month from Today Date
Date currentdate = system.Today();
Integer TodayDay = currentdate.Day();
Integer TodayMonth = currentdate.month();
String CurrentDayandMonth = TodayDay+'/'+TodayMonth;
global Database.Querylocator start(Database.Batchablecontext BC)
{
// Birth_Day_and_Month__c This is formula field this get Day and Month of Birthdate
// Ex Birthday : 24/04/2005 then it return 24/04
system.debug('CurrentDayandMonth&&&'+CurrentDayandMonth);
return Database.getQueryLocator([Select id,Name,Birthdate,
from Contact where
and Birth_Day_and_Month__c = :CurrentDayandMonth
]);
}
global void execute(Database.Batchablecontext BC,List<sobject> scope)
{
string strPhoneNo = '';
list<contact> lstofContact = [Select id,Birthdate ,Mobile_No_2__c,Phone,
Birth_Day_and_Month__c
from Contact where id in:scope];
string strContent = '';
if(lstofContact != null && lstofContact.size() > 0)
{
system.debug('lstofContact'+lstofContact);
for(Contact objCon : lstofContact)
{
if(objCon.Birth_Day_and_Month__c != null && objCon.Birth_Day_and_Month__c == CurrentDayandMonth)
{
lstofContactBirthdate.add(objCon);
}
}
if(lstofContactBirthdate != null && lstofContactBirthdate.size() > 0)
{
lstofSMSString = new List<string>();
strContent = 'Happy B\'day ';
for(Contact objCon : lstofContactBirthdate)
{
if(objCon.Phone != null)
{
lstofSMSString.add(objCon.Phone);
}
else if(objCon.Mobile_No_2__c != null)
{
lstofSMSString.add(string.valueof(objCon.Mobile_No_2__c));
}
}
if(lstofSMSString != null && lstofSMSString.size()>0)
{
for(string str : lstofSMSString)
{
strPhoneNo += str +',';
}
string URLSMS = 'http://UrlProvidedBYClint='+objCS.User_Name__c+'&passwd='+objCS.Password__c+'&mobilenumber='+strPhoneNo+'&message='+EncodingUtil.urlEncode(strContent,'UTF-8')+'&sid='+objCS.Sender_Id__c+'&mtype=N&DR=Y';
system.debug('@@@@'+URLSMS);
SendSMS(URLSMS);
}
}
}
}
public void SendSMS(string URLSMS)
{
try{
HttpRequest req = new HttpRequest();
req.setEndpoint(URLSMS);
req.setTimeout(60000);
req.setMethod('GET');
Http httpC = new Http();
HTTPResponse res = new HTTPResponse();
res = httpC.send(req);
System.debug('res: '+res.getBody()+'getStatus:: '+res.getStatus());
}
catch(exception ex)
{
}
}
global void finish(Database.Batchablecontext BC)
{
}
}
Thanks for code sir.
ReplyDelete