Saturday 7 September 2013

How to Send SMS on Mobile from Salesforce

Hi  Folks,

Today I brought up a very new type of implementation in Salesforce. We integrate Communication API and Salesforce APIs.
We can control our text/voice communication from few lines of code in Salesforce. I thought the same and made it possible by integrating SALESFORCE with TWILIO (one of the best cloud communication third party system).

Here is a way I followed to achieve to it: 

First of all we need to set up ANT to get some resource pool in our Salesforce from Twilio. You may set up ANT by following steps : 

Step 1 Download apache-ant-1.8.2-bin from Web.
Step 2 Install jdk1.6.0_25 or higher version.
Step 3 Go to run command use command sysdm.cpl press "advanced" tab then press "environment variable" tab then press on tab "New", create two variable ANT_HOME , JAVA_HOME and PATH 
ANT_HOME D: \apache-ant-1.8.2-bin\apache-ant-1.8.2 (if your ant folder is in D- drive)                    
JAVA_HOME  C:\Program Files\Java\jdk1.6.0_25 (give path of jdk)
PATH %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\ProgramFiles\Java\jdk1.6.0_25\jre\bin;D:\apache-ant-1.8.2-bin\apache-ant-1.8.2\bin;
(if path variable is alreadyavailable then append this also other wise create new Path variable)

Step 4. Now login in your Salesforce org and navigate Set up>-App Setup>Develop > Tool> force.com Migration Tool download this and pick one file "ant-salesforce.jar" and put it in lib folder of Apache( where we have ANT setup).

Step 5. We need to import all required classes of Twilio prepared fro Salesforce we need to click here  https://github.com/twilio/twilio-salesforce  and download one folder from this link and unzip it. From unzipped folder navigating nested folder and in "install" folder we will get some build files.

Step 6. Now we need to open command prompt and need to write path of folder where we have build.property and build.xml file  as below :-
Step 7. When Build gets started run below message we will get ( as confirmation that we have downloaded all files from Twilio in to Salesforce).

Step 8. Here you can find in org that Twilio classes have been imported ( just to check of process Completion)



Step 9. My Customization around this package.
A) Custom Setting Setup
When we get library files along with it one custom setting also get configured in org automatically so we need to manage this custom setting with some values :
ACCOUNT_SID, AUTH_TOKEN & Sender's Phone number (for these values we need to create a trail account in Twilio site and then we will get these three parameters).we will keep this information in custom setting.

B) We need to create some record on contact object for this we have created a new field Is_RegisteredTwilioUser = true other then this we need to put Mobile number with ISD code (e.g. INDIA then +91 should be prefixed).
Note : This number should be verified from your Twilio account as below :

Now time to come on screen I developed to send SMS to contacts

(C) Customization to create a screen from where we can manage communication (Text SMS)
Visualforce Page :

Apex Class :
public with sharing class TwilioCloudCommunicationClass {

// Public Properties
public String SelectedMobileNumber{get;set;}
public String OtherMobileNumber{get;set;}
public String textMessage{get;set;}

// Default construtor
public TwilioCloudCommunicationClass()
{
    SelectedMobileNumber  = '' ;
    OtherMobileNumber = '' ;
}

Public List getPersonList()
{
    Try{
        List localList = new List();
        localList.add(new SelectOption('' , '--Select--'));
        for(contact cont : [select Name,MobilePhone from contact where TwilioRegisteredUser__c = true ])
        {
            localList.add(new SelectOption(cont.MobilePhone , cont.Name));          
        }      
        localList.add(new SelectOption('other' , 'Other'));
        return localList ;
    }
    catch(Exception e)
    {
        ApexPages.addMessages(e);      
        return null;
    }
}

public void SendSMS()
{
    Try{      
        SelectedMobileNumber = (SelectedMobileNumber == '')? OtherMobileNumber:SelectedMobileNumber ;
        if(SelectedMobileNumber != '')
        {
            List AdminInfo = TwilioConfig__c.getall().values();
            String ACCOUNT_SID = '';
            String AUTH_TOKEN  = '' ;            
            String SenderMobileNumber = '' ;
            // Informaton getting from custom setting
            if(AdminInfo.size()>0)
            {          
                ACCOUNT_SID             = AdminInfo[0].AccountSid__c;
                AUTH_TOKEN              = AdminInfo[0].AuthToken__c;                
                SenderMobileNumber      = AdminInfo[0].Admin_Mobile_Number__c;    
            }            
            TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
           
            Map properties = new Map {
                        'To'   => SelectedMobileNumber ,
                        'From' => SenderMobileNumber,
                        'Body' => textMessage
                };
            TwilioSMS message = client.getAccount().getSmsMessages().create(properties);
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Message has been sent'));
        }
        else
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error, 'Pelase provide valid Mobile Number '));
        }
    }catch(Exception e )
    {
        ApexPages.addMessages(e);      
        return ;
    }  
}

}

Tuesday 3 September 2013

How to override View page of SObject based on Record Type

Hi Folks,

Today I brought up very small and useful trick for overriding View page of s-object based on record type.

Lets say we have an Opportunity Object with record type 'JapanType' & 'USType' and we need to show 'JapanType' with standard layout while 'USType' with Custom Screen.

It is not possible by any customization in salesforce but we can achieve it with small script over the page: 

  

SO if even we have overridden view page of opportunity with VF page but if that record type is of 'JapanType' page gets it's standard layout.