function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tolby HuntTolby Hunt 

SMS to lead upon creation

Hello everyone! i am trying to send a text to my leads with twilio with a trigger event once they are created. I am new to salesforce so most of this is a whirlwind to me. If you could fix my code and then explain it that would be best.

Thanks

Apex Class
 
public class Send_SMS_ControllerLead {
    public String smsBody {get; set;}
    public boolean hasNumber {get; set;}
    public final String  fromNumber = '+1xxxxxxxxx';// get it from twilio
      //after login click Phone Numbers tab in left sidebar

    public string dialCode = '+1'; // Add your dial Code of your country.
    public Send_SMS_ControllerLead(ApexPages.StandardController controller) {
       hasNumber = true;
    }
    public static void Send()
    {
        String account = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Account SID on home tab
        String token   =   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'';//AUTH Token on home tab
        TwilioRestClient client = new TwilioRestClient(account, token);

        Id leadId = ApexPages.currentPage().getParameters().get('id');

        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        Lead a = [SELECT Phone FROM Lead WHERE Id = :leadId];

        if(a.Phone != null)
        {
            String phoneNumber = a.Phone;
            if(!phoneNumber.Contains('+1'))
            phoneNumber = '+1'+phoneNumber;// If by default dialCode is not
             //on Phone number we will ad it.
            System.debug('phoneNumber'+phoneNumber);
             Map<String,String> params = new Map<String,String> {
            'To'   => phoneNumber,
            'From' => '+1xxxxxxxxxx',
            'Body' => 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.'
             };
             TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
             SMS_Lead__c sentSMS = new SMS_Lead__c(From__c = '+1xxxxxxxxxxx',To__c = phoneNumber, Body__c = 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.');
             insert sentSMS;

        }
        
    }
     
}

and my trigger

Apex Trigger
 
trigger SMS_To_New on Lead (after insert, after update) {
    
    Send_SMS_ControllerLead.Send();
}



Let me know

Thanks!​
Tolby HuntTolby Hunt
Also, Bonus question....

I would also like to text out when a specific field with a picklist has a certain item. If they have that item i would like to send them a text message. I would set it up a very similar Apex Class most likely, not sure how to check if it has that field with the correct info. Thanks Again
Amit Chaudhary 8Amit Chaudhary 8
Hi,

You need to send Lead record from trigger to handler class like below
trigger SMS_To_New on Lead (after insert, after update) {
    
    Send_SMS_ControllerLead.Send(Trigger.new);
}

And your Handler class should be like below
public class Send_SMS_ControllerLead {
    public String smsBody {get; set;}
    public boolean hasNumber {get; set;}
    public final String  fromNumber = '+1xxxxxxxxx';// get it from twilio
      //after login click Phone Numbers tab in left sidebar

    public string dialCode = '+1'; // Add your dial Code of your country.
    public Send_SMS_ControllerLead(ApexPages.StandardController controller) {
       hasNumber = true;
    }
    public static void Send()
    {
        String account = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Account SID on home tab
        String token   =   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'';//AUTH Token on home tab
        TwilioRestClient client = new TwilioRestClient(account, token);

        Id leadId = ApexPages.currentPage().getParameters().get('id');

        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        Lead a = [SELECT Phone FROM Lead WHERE Id = :leadId];

        if(a.Phone != null)
        {
            String phoneNumber = a.Phone;
            if(!phoneNumber.Contains('+1'))
            phoneNumber = '+1'+phoneNumber;// If by default dialCode is not
             //on Phone number we will ad it.
            System.debug('phoneNumber'+phoneNumber);
             Map<String,String> params = new Map<String,String> {
            'To'   => phoneNumber,
            'From' => '+1xxxxxxxxxx',
            'Body' => 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.'
             };
             TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
             SMS_Lead__c sentSMS = new SMS_Lead__c(From__c = '+1xxxxxxxxxxx',To__c = phoneNumber, Body__c = 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.');
             insert sentSMS;

        }
        
    }
     
	// This method for Trigger 
    public static void Send(List<Lead> newLead)
    {
        String account = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Account SID on home tab
        String token   =   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'';//AUTH Token on home tab
        TwilioRestClient client = new TwilioRestClient(account, token);

        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
		List<SMS_Lead__c> lstSMSLead = new List<SMS_Lead__c>();
		
		for(Lead leadObj : newLead)
		{
			if(leadObj.Phone != null)
			{
				String phoneNumber = leadObj.Phone;
				if(!phoneNumber.Contains('+1'))
				phoneNumber = '+1'+phoneNumber;// If by default dialCode is not
				 //on Phone number we will ad it.
				System.debug('phoneNumber'+phoneNumber);
				 Map<String,String> params = new Map<String,String> {
				'To'   => phoneNumber,
				'From' => '+1xxxxxxxxxx',
				'Body' => 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.'
				 };
				 TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
				 
				 SMS_Lead__c sentSMS = new SMS_Lead__c(From__c = '+1xxxxxxxxxxx',To__c = phoneNumber, Body__c = 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.');
				 //insert sentSMS;
				lstSMSLead.add(sentSMS);
			}
		}

		if(lstSMSLead.size() > 0 )
		{
			insert lstSMSLead;
		}	
    }
}

Let us know if this will help you
 
Tolby HuntTolby Hunt
Thanks for your help. I now have an error code that ill post below. 
SMS_To_New: execution of AfterInsert caused by: System.CalloutException: Callout from triggers are currently not supported. Class.TwilioClient.request: line 428, column 1 Class.TwilioClient.safeRequest: line 579, column 1 Class.TwilioResource.TwilioListResource.createInstance: line 402, column 1 Class.TwilioSmsList.create: line 76, column 1 Class.Send_SMS_ControllerLead.Send: line 64, column 1 Trigger.SMS_To_New: line 3, column 1
Amit Chaudhary 8Amit Chaudhary 8
Please use Future method like below

Trigger like below
trigger SMS_To_New on Lead (after insert, after update) {
    
    Send_SMS_ControllerLead.Send(Trigger.newMap().keySet());
}


 
public class Send_SMS_ControllerLead {
    public String smsBody {get; set;}
    public boolean hasNumber {get; set;}
    public final String  fromNumber = '+1xxxxxxxxx';// get it from twilio
      //after login click Phone Numbers tab in left sidebar

    public string dialCode = '+1'; // Add your dial Code of your country.
    public Send_SMS_ControllerLead(ApexPages.StandardController controller) {
       hasNumber = true;
    }
    public static void Send()
    {
        String account = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Account SID on home tab
        String token   =   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'';//AUTH Token on home tab
        TwilioRestClient client = new TwilioRestClient(account, token);

        Id leadId = ApexPages.currentPage().getParameters().get('id');

        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        Lead a = [SELECT Phone FROM Lead WHERE Id = :leadId];

        if(a.Phone != null)
        {
            String phoneNumber = a.Phone;
            if(!phoneNumber.Contains('+1'))
            phoneNumber = '+1'+phoneNumber;// If by default dialCode is not
             //on Phone number we will ad it.
            System.debug('phoneNumber'+phoneNumber);
             Map<String,String> params = new Map<String,String> {
            'To'   => phoneNumber,
            'From' => '+1xxxxxxxxxx',
            'Body' => 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.'
             };
             TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
             SMS_Lead__c sentSMS = new SMS_Lead__c(From__c = '+1xxxxxxxxxxx',To__c = phoneNumber, Body__c = 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.');
             insert sentSMS;

        }
        
    }
     
	// This method for Trigger 
	@future
    public static void Send(Set<Id> setId)
    {
	
		List<Lead> newLead = [select id,Phone from Lead where id in :setId];
		
        String account = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Account SID on home tab
        String token   =   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'';//AUTH Token on home tab
        TwilioRestClient client = new TwilioRestClient(account, token);

        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
		List<SMS_Lead__c> lstSMSLead = new List<SMS_Lead__c>();
		
		for(Lead leadObj : newLead)
		{
			if(leadObj.Phone != null)
			{
				String phoneNumber = leadObj.Phone;
				if(!phoneNumber.Contains('+1'))
				phoneNumber = '+1'+phoneNumber;// If by default dialCode is not
				 //on Phone number we will ad it.
				System.debug('phoneNumber'+phoneNumber);
				 Map<String,String> params = new Map<String,String> {
				'To'   => phoneNumber,
				'From' => '+1xxxxxxxxxx',
				'Body' => 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.'
				 };
				 TwilioSMS sms = client.getAccount().getSMSMessages().create(params);
				 
				 SMS_Lead__c sentSMS = new SMS_Lead__c(From__c = '+1xxxxxxxxxxx',To__c = phoneNumber, Body__c = 'Thanks for applying with Fundwise. An email was sent to you for the next step that will expedite the process. An agent will contact you shortly.');
				 //insert sentSMS;
				lstSMSLead.add(sentSMS);
			}
		}

		if(lstSMSLead.size() > 0 )
		{
			insert lstSMSLead;
		}	
    }
}

Let us know if this will help you
 
Tolby HuntTolby Hunt
The trigger now has an error of 
Compile Error: Variable does not exist: Trigger at line 3 column 34
Tolby HuntTolby Hunt
Okay i was able to solve the issue. i changed the trigger like so.
trigger SMS_To_New on Lead (after insert, after update) {
    
    Send_SMS_ControllerLead.Send(Trigger.newMap.keySet());
}

But it still isnt sending the texts...
Tolby HuntTolby Hunt
I am not getting an error code but no text is being sent from twilio. I have changed the X's to the correct info but it still doesnt work.
 
Amit Chaudhary 8Amit Chaudhary 8
We are doing future callout. So you need to check long from Apex Jobs

Setup->
Apex Jobs.

NOTE:- Please check your old code was working on not.
 
Tolby HuntTolby Hunt
That fixed it. thanks!
Tolby HuntTolby Hunt
Now im getting errors when trying to deploy because it doesnt have enough code coveage?