Wednesday 25 December 2013

Google Map Integration With Salesforce

I got a requirement of showing google map of Account Billing address on detail page of Account. In the starting I was thinking how to do it, but didn't get anything, but now at the end I tried this below code and everything is working as fine as visible down there.

In this below scenario user don't have to submit their APIKey of Google Map to get the Map of the Address. and one more thing here am not using any Controller/Apex class only and only page is doing everything.

Here it is the Detail Page of Account and the Location of Account billing address using Google Map


Below is the Visual force Page

<!--
/**
* Description : Component for showing google map of Account Address.
*
* Created Date : 12/25/2013
*
* Created By : Abhi Tripathi
*
* Version : V1_0
**/
-->
<apex:page standardController="Account">
    
    <!-- Import Necessary Jquery js File and StyleSheets-->
    <apex:includeScript value="{!URLFOR($Resource.jQuery_GoogleMap, 'js/GSensor.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.jQuery_GoogleMap, 'js/jquery-1.6.2.min.js')}"/>
    <script>
    var map,geocoder,infowindow;
    var latLngs = [];
    $j = jQuery.noConflict();
    $j(document).ready(function(){
        initialize();
    });
    
    function initialize() {
        geocoder = new google.maps.Geocoder(37.09024, -95.712891);
        //initial cordinates for map init
        var latlng = new google.maps.LatLng();
        var myOptions = {
            center: latlng,
            zoom: 2,
            maxZoom: 14,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        //load the map
        map = new google.maps.Map($j('#map')[0], myOptions);
        codeAddress();
    }
    
    /*This function codes the address using the Billing Address in the Acoount*/
    function codeAddress(){
        //prepare a string for geocoding
        var address = '{!JSENCODE(Account.BillingStreet)},{!JSENCODE(Account.BillingCity)},{!JSENCODE(Account.BillingCountry)},{!JSENCODE(Account.BillingPostalCode)}';
        console.log(address);
        
        //geocode the address
        geocoder.geocode( { 'address': address }, function(results, status) {
            //if it is a success
            if (status == google.maps.GeocoderStatus.OK) {
                var location = results[0].geometry.location;
                var marker=addMarker(location );
            }
            else {
                // alert(status);
            }
        });
    }
    
    /*
*This method adds a marker to the provided location
**/
    function addMarker(location) {
        marker = new google.maps.Marker({
            position: location,
            map: map
        });
        //set the bounds and initial zoom
        var latlngbounds = new google.maps.LatLngBounds();
        latlngbounds.extend(marker.getPosition());
        map.fitBounds(latlngbounds);
        map.setZoom(14);
        return marker;
    }
    
    </script>
    <style>
        #map {
        width:100%;
        height:500px;
        margin-left:59.5%;
        }
    </style>
    <div id="map" class="ui-widget-content ui-corner-all ui-state-default"></div>
</apex:page>


Save that above code before that save this Jquery files and after that save them into your static resource with their corresponding name

Click Below Links for the JQuery files

GSensor
jquery-1.6.2.min
jquery-ui-1.8.16.custom.min

After saving the above jquery files in Static Resource and saving your visualforce page.

Go to
Customize --> Accounts --> Page Layout --> Edit(One of page layout) --> Visualforce Pages(sidebar of layout)

Search your page and add it to the page layout. and see the magic
Note: Billing address need to be filled with correct addres otherwise its will show a default map.

Happy Coding..!!

Tuesday 24 December 2013

Parsing XML file in Apex Class

Somedays ago I was in need to parse the XML response, Which I was getting in reponse. it require a bit labour to get a particular value from that XML file.

In salesforce most of the time when we are working with webservice, we need to parse JSON or XML. So here I am doing a small POC of parsing XML file in salesforce.
Here am trying to fetch the value of "R10TnxID" from the file.

So below is the code, for more detail you can visit on This Link
Here you need learn a bit about DOM

/** Description : Parse XML file. String Response is the XML file which is going to be parsed.
*
* Created By : Abhi Tripathi 
*
* Created Date : 07/09/2013
*
* Revisison Log : 07/09/2013 
*
* Version : V1.0
**/
public with sharing class domXmlParsing {
    
    //method to parse xml file 
    public static string walkThrough(){
        
        //XML string to parse
        String response = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">'
            + '<S:Body>'
            + '<wss:createPendingTicketResponse xmlns:wss="http://www.boomi.com/connector/wss">'
            + '<wss:createPaymentTransctionLogResponse/>'
            + '<PendingTicket xmlns:pay="http://www.boomi.com/v1b/Payment">'
            + '<pay:Response>'
            + '<pay:R10TillID>test001</pay:R10TillID>'
            + '<pay:R10SequenceNumber>test002</pay:R10SequenceNumber>'
            + '<pay:SFTnxId>test003</pay:SFTnxId>'
            + '<pay:R10TnxID>'+ 'TESTME' +'</pay:R10TnxID>'
            + '<pay:Org>test004</pay:Org>'
            + '</pay:Response>'
            + '</PendingTicket>'
            + '</wss:createPendingTicketResponse>'
            + '</S:Body>'
            + '</S:Envelope>';
        
        //dom
        Dom.Document doc = new Dom.Document();
        
        //string
        string result;
        
        //load document
        doc.load(response);
        
        Dom.XMLNode envelope = doc.getRootElement();
        system.debug('!!!!!!!!! value of envelope' + envelope);
        
        //body
        Dom.XMLNode body = envelope.getChildElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/'); 
        system.debug('@@@@@ value of body' + body);
        
        //child element of createPendingTicketResponse
        Dom.XMLNode createPendingTicketResponse = body.getChildElement('createPendingTicketResponse', 'http://www.boomi.com/connector/wss'); 
        system.debug('@@@@@ value of createPendingTicketResponse' + createPendingTicketResponse);
        
        //child element of createPaymentTransctionLogResponse
        Dom.XMLNode createPaymentTransctionLogResponse = createPendingTicketResponse.getChildElement('createPaymentTransctionLogResponse', 'http://www.boomi.com/connector/wss'); 
        system.debug('@@@####@@ value of createPaymentTransctionLogResponse' + createPaymentTransctionLogResponse);
        
        //child element of pending Ticket
        Dom.XMLNode PendingTicket = createPendingTicketResponse.getChildElement('PendingTicket', null); 
        system.debug('@@@####@@ value of PendingTicket' + PendingTicket);
        
        //child element of Response 
        Dom.XMLNode Respon = PendingTicket.getChildElement('Response', 'http://www.boomi.com/v1b/Payment'); 
        system.debug('@@@####@@ value of Response' + Respon); 
        
        //child element of R10TnxID
        Dom.XMLNode R10TnxID = Respon.getChildElement('R10TnxID', 'http://www.boomi.com/v1b/Payment'); 
        system.debug('@@@####@@ value of R10TnxID' + R10TnxID); 
        
        //here is the value in R10TnxID
        result = R10TnxID.getText();
        system.debug('@@@####@@ value of result' + result); 
        
        return result;
    }
}


After saving this above class, go to your Developer Console and write as below shown and then check your debug logs, you'll get the value of R10TnxID


Happy Coding....!!!!!!

Friday 13 December 2013

Salesforce Comparable Interface Using It to Sort Wrapper Class

Salesforce have an Interface named as "Comparable" , it is used to add List sorting support for your Apex class

To know more about the Comparable click on this link Comparable Interface

So I used it to compare created date of my history Account records , I have a picklist name Grade__c on Account, whenever user change it , saleforce created a history record for that change.

Note : You need to activate the history tracking for Account and for that field too, for which you want history tracking.

So now am going to tell you how my code works,

Whenever user Updates OR change value of Grade on Account object it will be added on the report chart of history

Here is the snapshot of that report chart


For that I have created a main class and as well as a wrapper class

Wrapper Class
 /**  
 * Description : Controller class for page of Account History of Grade  
 *  
 * Created Date : 12/10/2013  
 *  
 * Created By : Abhi Tripathi  
 *  
 * Version : V1.0   
 **/  
 global class AccountHistoryWrapper implements Comparable{  
   public Account account {get; set;}  
   public List<AccountHistory> accountHistories {get; set;}  
   //Calling Constructor  
   global AccountHistoryWrapper(Account account, List<AccountHistory> accountHistories) {  
     this.account = account;  
     this.accountHistories = accountHistories;   
   }  
   // Compare opportunities based on the opportunity amount.  
   global Integer compareTo(Object compareTo) {  
     // Cast argument to AccountHistoryWrapper  
     AccountHistoryWrapper aHW = (AccountHistoryWrapper)compareTo;  
     // The return value of 0 indicates that both elements are equal.  
     Integer returnValue = 0;  
     if ( aHW.account.CreatedDate > aHW.account.CreatedDate) {   
       // Set return value to a positive value.  
       returnValue = 1;  
     } else if ( aHW.account.CreatedDate < aHW.account.CreatedDate) {  
       // Set return value to a negative value.   
       returnValue = -1;   
     }  
     return returnValue;   
   }  
 }  

Here is the Controller Class

 /**   
 * Description : Controller class for page of Account History of Grade   
 *   
 * Created Date : 12/10/2013   
 *   
 * Created By : Abhi Tripathi   
 *   
 * Version : V1.0    
 **/   
 public class AccountHistoryController {   
   public List<AccountHistoryWrapper> accountHistoriesWrapList {get; set;}   
   //Calling cosnturctor   
   public AccountHistoryController() {   
     //Memory Allocation   
     accountHistoriesWrapList = new List<AccountHistoryWrapper>();   
     //Loop through Accounts   
     for(Account acc : [Select Id, Name, CreatedDate, (Select ID, Field, AccountId, CreatedDate, Createdby.Name, Oldvalue,    
                              Newvalue, Account.Name From Account.Histories   
                              Where Field = 'Grade__c' ORDER BY CreatedDate DESC LIMIT 200 )   
               FROM Account ORDER BY LastModifiedDate DESC]) {    
                 //Populate wrapper list with values   
                 if(acc.Histories.size() > 0)   
                   accountHistoriesWrapList.add(new AccountHistoryWrapper(acc, acc.Histories));    
               }   
     //Get List of wrapper and sort it   
     accountHistoriesWrapList.sort();    
   }   
 }   

Here is the Page

 <!--   
 /**  
 * Description : Custom VF page to add it on store page layout as an inline VF page.  
 *  
 * Created By : Abhi Tripathi  
 *  
 * Created Date : 12/10/2013  
 *  
 **/  
 -->  
 <apex:page controller="AccountHistoryController">  
   <apex:form >  
     <apex:pageBlock >  
       <table cellspacing="0" border="0" class="detailList list" cellpadding="0">  
         <thead class="rich-table-thead">  
           <tr class="headerRow">  
             <th class="headerRow" scope="col">Activity</th>  
             <th class="headerRow" scope="col">Date</th>  
             <th class="headerRow" scope="col">Change By</th>  
           </tr>  
         </thead>  
         <tbody>  
           <apex:repeat value="{!accountHistoriesWrapList}" var="accountHistoryWrap">  
             <tr class="dataRow even first" onmouseover="if (window.hiOn){hiOn(this);} " onmouseout="if (window.hiOff){hiOff(this);} " onblur="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}">  
               <td colspan="3" style="text-align:center"><B><font size="3"><em><apex:outputField value="{!accountHistoryWrap.account.Name}"/></em></font></B></td>  
             </tr>  
             <apex:repeat value="{!accountHistoryWrap.accountHistories}" var="aHW">  
               <tr class="dataRow even first" onmouseover="if (window.hiOn){hiOn(this);} " onmouseout="if (window.hiOff){hiOff(this);} " onblur="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}">  
                 <td class="dataCell">  
                   Changed Grade from <b>"{!aHW.oldvalue}"</b> to <b>"{!aHW.newvalue}"</b>   
                 </td>  
                 <td class="dataCell">  
                   <apex:outputField value="{!aHW.createddate}"/>   
                 </td>  
                 <td class="dataCell">   
                   <apex:outputField value="{!aHW.createdby.Name}"/>   
                 </td>  
               </tr>   
             </apex:repeat>  
           </apex:repeat>  
         </tbody>  
       </table>  
     </apex:pageBlock>  
   </apex:form>  
 </apex:page>  

Test class

 @isTest(seeAllData=false)  
 private class Test_AccountHistoryController {  
   //Test method  
   static testMethod void myUnitTest() {  
     //List of Account  
     List<Account> accountList = new List<Account>();  
     //Insert Account  
     Account account = new Account(Name = 'test', Grade__c = 'A');  
     insert account;  
     //Insert another account  
     Account account2 = new Account(Name = 'ATest', Grade__c = 'C');  
     insert account2;  
     //Update Account  
     account.Grade__c = 'B';  
     accountList.add(account);  
     //Update Account  
     account2.Grade__c = 'D';  
     accountList.add(account2);  
     //Update account  
     update accountList;  
     //Account History  
     List<AccountHistory> accHistory = [Select Id From AccountHistory Where AccountId =: accountList];  
     //Test starts here  
     Test.startTest();  
     //Calling wrapper class  
     AccountHistoryWrapper wrapper = new AccountHistoryWrapper(account, accHistory);   
     //Controller  
     AccountHistoryController controller = new AccountHistoryController();  
     //Test stops here  
     Test.stopTest();  
     //Assert  
     System.assertEquals(true, Controller.accountHistoriesWrapList != null);  
   }  
 }  


Happy Coding
CHEERS........!!!!!!!

Wednesday 27 November 2013

Salesforce Account Record Merging With Deleting the Duplicate Records

Whenever we are working with bulk data, its a hard thing to check the duplicate records and then delete them without loosing data.

So I thought why shouldn't we go for a bulk process that helps us in Merging the duplicate records and them clearing them from database without any loss of data.

So here I have done something for my Account object, I have written a batch, whenever we execute it, it will start processing on Account records created in "LAST 15 MINUTES", takes those Account's Name and Billing Address, and then it will check in database that is there any record having same Name AND Billing Address or not.

This batch won't work if there is any slight change in between name or billing address.

for example I have created two Account records having same Name and Billing Address only the first record Phone was an extra field filled.


1st Account created



2nd Account without Phone but same Billing Address and Name




Now when I execute my batch in console




Now my batch deletes duplicate Account that have empty phone


Now the best thing is, no matter how many field you have in Account it will save your field values in the unique record, that means your data will never loose your data.

Here is the batch


/**
* Description : Batch class to merge duplicate transaction Account records.
*
* Created Date : 11-26-2013 
* 
* Revision Logs : V1.0 - Created
*
**/
global class Batch_MergeDuplicateTransactionAccounts implements Database.Batchable<sObject>, Database.Stateful {
    
    //Set to hold the account records name with billing address field values
    global Set<String> setAccountNameWithBillingAddress;
    
    //String to hold the Account object fields names
    String accountFieldsNamesString = '';
    
    //Set to hold the Account fields API name strings
    Set<String> setAccountFieldsNameString;
    
    //Calling Constructor 
    global Batch_MergeDuplicateTransactionAccounts() {
        
        //Memory Allocation to collections
        setAccountFieldsNameString = new Set<String>();
        setAccountNameWithBillingAddress = new Set<String>();
        
        //Describe Account object and get all the fields
        Map<String, Schema.SObjectField> accountFieldsMap = Account.sObjectType.getDescribe().fields.getMap();
        
        //Loop through Account fields Names through Schema Methods
        for(String fieldName : accountFieldsMap.keySet()) {
            
            //Describe field
            Schema.DescribeFieldResult field = accountFieldsMap.get(fieldName).getDescribe();
            
            //Filtering out the fields for getting only updatable non system fields
            if(!field.isCalculated() && field.isCreateable() && field.isUpdateable()
               && !field.getLocalName().equalsIgnoreCase(Constants.PARENTID)) {
                   
                   //Populate set with the Fields Names string values
                   setAccountFieldsNameString.add(field.getLocalName()); 
                   
                   //Account object fields name string
                   if(accountFieldsNamesString == '')
                       accountFieldsNamesString = field.getLocalName(); 
                   else
                       accountFieldsNamesString += ',' + field.getLocalName();
               }
        }
    }
    
    //Start method
    global Database.QueryLocator start(Database.BatchableContext BC) {
        
        //Varibale to hold the current date time value
        DateTime currentDateTime = DateTime.now();
        
        //Varibale to hold the 15 minute ago date time value
        DateTime fifteenMinuteAgoDateTime = DateTime.now().addMinutes(-15);
        
        //String variable to hold the Account records those were created in last 15 minute and so.
        String sOQLQuery = 'SELECT ID, Name FROM Account WHERE'
            + ' CreatedDate >: fifteenMinuteAgoDateTime AND CreatedDate <=: currentDateTime'
            + ' AND Name != null ORDER By CreatedDate ASC';
        
        //Fetching all the Account records from the database
        return Database.getQueryLocator(sOQLQuery);
    }
    
    //Exectue Method having logic for duplicate accounts finding's
    global void execute(Database.BatchableContext BC, List<Account> scope) {
        
        //Set to hold the account name with billing address strings
        Set<String> setAccountNameWithBillingAddressStrings = new Set<String>();
        
        //Map to hold the Account records corresponding the account name and billing address value as key
        Map<String, List<Account>> mapNameBillingAddressKeyWithAccounts = new Map<String, List<Account>>();
        
        //This map is to hold the Parent Id and List of List of Accounts. One list will have maximum 2 child records.
        //Standard merge statement allows to merge 3 records at a time, so List of list will hold list of 2 child records
        Map<Id, List<List<Id>>> mapParentAccountWithListOfChildrenAccounts = new Map<Id, List<List<Id>>>();
        
        //This mapis to hold the updated Parent data always
        Map<Id, Account> mapParentAccount = new Map<Id, Account>();
        
        //Loop through account records in scope
        for(Account acc : scope) {
            
            //Checking value in set
            if(!(setAccountNameWithBillingAddress.contains(acc.Name.trim().toLowerCase()))) {
                
                //Populate set with values
                setAccountNameWithBillingAddress.add(acc.Name.trim().toLowerCase());
                setAccountNameWithBillingAddressStrings.add(acc.Name.trim().toLowerCase());
            }
        }
        
        //Check set for size
        if(setAccountNameWithBillingAddressStrings != null) {
            
            //Loop through eligible account records
            for(Account account : Database.query('SELECT ' + accountFieldsNamesString + ' FROM Account WHERE Name IN : setAccountNameWithBillingAddressStrings AND Name != null ORDER By CreatedDate ASC')) {
                
                //Key String
                String keyString = '';
                String billingAddressString = '';
                
                //Appending account billing address field values in key string after performing validation on them
                if(account.BillingStreet != null)
                    billingAddressString += account.BillingStreet.trim().toLowerCase(); 
                else
                    billingAddressString += null;
                if(account.BillingCity != null)
                    billingAddressString += account.BillingCity.trim().toLowerCase(); 
                else
                    billingAddressString += null;
                if(account.BillingState != null)
                    billingAddressString += account.BillingState.trim().toLowerCase(); 
                else
                    billingAddressString += null;
                if(account.BillingCountry != null)
                    billingAddressString += account.BillingCountry.trim().toLowerCase(); 
                else
                    billingAddressString += null;
                if(account.BillingPostalCode != null)
                    billingAddressString += account.BillingPostalCode.trim().toLowerCase(); 
                else
                    billingAddressString += null;
                
                //Formation of key string with the help of account name and billing address string
                keyString = account.Name.trim().toLowerCase() + Constants.SEPERATOR + billingAddressString;
                System.debug('@@@@@ keyString ' + keyString);
                
                //Check for key value in map
                if(mapNameBillingAddressKeyWithAccounts.containsKey(keyString)) {
                    
                    //Get the Values of the Map and add Id to it.
                    mapNameBillingAddressKeyWithAccounts.get(keyString).add(account);
                    
                } else {
                    
                    //Creat a new Set at values and add Id to it.
                    mapNameBillingAddressKeyWithAccounts.put(keyString, new List<Account>{account}); 
                }
                
                System.debug('@@@@@ mapNameBillingAddressKeyWithAccounts ' + mapNameBillingAddressKeyWithAccounts);
                
                //Loop through map keys
                for(String key : mapNameBillingAddressKeyWithAccounts.keySet()) {
                    
                    //Checking if we have more than one account record in the list corresponding to the account name, billingaddress combined string key
                    if(mapNameBillingAddressKeyWithAccounts.get(key) != null && mapNameBillingAddressKeyWithAccounts.get(key).size() >= 1) {
                        
                        //Account record having oldest created date stamped on it will become parent of other dup recods
                        Account parentAccount = mapNameBillingAddressKeyWithAccounts.get(key)[0];
                        
                        //Set Parent in Map with latest Values
                        mapParentAccount.put(parentAccount.Id, parentAccount);
                        
                        //Add a default list
                        List<List<Id>> lstOfLst = new List<List<Id>>();
                        lstOfLst.add(new List<Id>());
                        mapParentAccountWithListOfChildrenAccounts.put(parentAccount.Id, lstOfLst);
                        
                        //Lopp through the child records
                        //Set all the null field in Parent with child data if child have not null value
                        for(Integer i=1; i<mapNameBillingAddressKeyWithAccounts.get(key).size(); i++) {
                            
                            //Dup Child Account record
                            Account childAccount = mapNameBillingAddressKeyWithAccounts.get(key)[i];
                            
                            //Loop through set having Account object fields API Name with it
                            for(String accountFieldAPIName : setAccountFieldsNameString) {
                                
                                //Checking for value in child with respect to Parent
                                if(parentAccount.get(accountFieldAPIName) == null && childAccount.get(accountFieldAPIName) != null) {
                                    
                                    //Populating Instance with value
                                    parentAccount.put(accountFieldAPIName, childAccount.get(accountFieldAPIName)); 
                                }
                            }
                            
                            //Put the latest innstance of Parent Account in Map
                            mapParentAccount.put(parentAccount.Id, parentAccount);
                            
                            //Get List from Marging Map
                            List<List<Id>> mergingAccounts = mapParentAccountWithListOfChildrenAccounts.get(parentAccount.Id);
                            
                            //Chekcif list size has been reached to 2, add a new List and add account in that
                            if(mergingAccounts[mergingAccounts.size() - 1].size() == 2) {
                                
                                //Add a new List
                                mergingAccounts.add(new List<Id>()); 
                            }
                            
                            //Add Child record in List
                            mergingAccounts[mergingAccounts.size() - 1].add(childAccount.Id);
                            
                            //Put this list back in original map
                            mapParentAccountWithListOfChildrenAccounts.put(parentAccount.Id, mergingAccounts);
                        }
                    }
                }
            }
            
            System.debug('@@@@@@ value in mapParentAccount ' + mapParentAccount);
            System.debug('@@@@@@ value in mapParentAccountWithListOfChildrenAccounts ' + mapParentAccountWithListOfChildrenAccounts);
            
            //Start Merging Process
            for(Account pAccount : mapParentAccount.values()) {
                
                //Get merging list and start merging process
                if(mapParentAccountWithListOfChildrenAccounts.containsKey(pAccount.Id)) {
                    
                    //Loop through the merging list
                    for(List<Id> accounts : mapParentAccountWithListOfChildrenAccounts.get(pAccount.Id)) {
                        
                        if(accounts != null && accounts.size() > 0) {
                            System.debug('###### accounts ' + accounts);
                            //Merge statement for merging of the child records with respect to Parent Account record
                            merge pAccount accounts;
                        }
                    } 
                }
            }
        }
    }
    
    //Finish Method
    global void finish(Database.BatchableContext BC) {
        
    }
}


Here is the Test Class

/**
* Description : Test Class for Batch_MergeDuplicateTransactionAccounts.
*
* Created Date : 11-27-2013
*
* Revisiion Logs : V_1.0 - Created
*
* Code Coverage : 100%
**/
@isTest
private class Test_Batch_MergeDuplicateTxnAccounts {
    
    //Test method
    static testMethod void myUnitTest() {
        
        //List to hold account records
        List listAccounts = new List();
        
        //Create Account with iteration of count
        for(integer i = 1 ; i <= 100 ; i++) { //Populating the list of Account records listAccounts.add(new Account(Name = 'Test1' , BillingCity = 'TestCity' , BillingState = 'TestState' , BillingPostalCode = '85004' , BillingStreet = 'TestStreet' , BillingCountry = 'US')); } listAccounts.add(new Account(Name = 'Test2' , BillingCity = 'TestCity' , BillingState = 'TestState' , BillingPostalCode = '85005' , BillingStreet = 'TestStreet' , BillingCountry = 'US')); listAccounts.add(new Account(Name = 'Test2'));                  //Insert accounts insert listAccounts; //List to hold contact records List listContacts = new List();
            
            //Populate the list with contact records
            listContacts.add(new Contact(FirstName = 'Test' , LastName = 'Contact' , AccountId = listAccounts[1].Id));
            listContacts.add(new Contact(FirstName = 'Test1' , LastName = 'Contact1' , AccountId = listAccounts[2].Id));
            
            //Insert contacts
            insert listContacts;
            
            //Test start from here
            Test.startTest();
            
            //Batch Initializing
            Batch_MergeDuplicateTransactionAccounts controller = new Batch_MergeDuplicateTransactionAccounts();
            
            //Execute Batch
            Database.executeBatch(controller , 200);
            
            //Test stop here
            Test.stopTest();
            
            //Query to get account records
            listAccounts = [SELECT ID , (SELECT ID From Contacts) FROM Account];
            
            //Assert for results
            System.assertEquals(listAccounts.size() , 3);
            System.assertEquals(listAccounts[0].contacts.size() , 2);
        }
    }   
}


Thanks & Cheers,
Hope helped someone.


Sunday 24 November 2013

Salesforce launches the internet of Customers "Salesforce1"

Salesforce1 is meant to allow the rapid creation of apps that can work across Salesforce's sales, service, and marketing apps, as well as on top of its Force.com, Heroku, and ExactTarget Fuel platforms, all at the same time. Salesforce1 is a free, automatic upgrade for existing Salesforce customers.


Salesforce1 is a new social, mobile and cloud customer platform built to transform sales, service and marketing apps for the Internet of Customers. As the pioneer of enterprise cloud computing, salesforce.com is launching the next generation of the world’s #1 cloud platform, Salesforce1, for the new connected world. Now every company can connect with customers in a whole new way.

New Salesforce1 is the first CRM platform for developers, ISVs, end users, admins and customers moving to the new social, mobile and connected cloud.
Developers can create next generation apps. Salesforce1 was built API-first to enable developers to build the next generation of connected apps.

Built for next generation applications, Salesforce1 has 10 times more APIs and services built-in, for developers to build quickly, and easily create personalized experiences for connect smartphones and wearable smart devices. With Salesforce1, every ISV can accelerate their growth by building, selling and distributing apps for the connected customer.

ISVs such as Evernote and Kenandy are building mobile-ready apps on the platform and leveraging the power of the Salesforce1 AppExchange to market and sell these apps. A new mobile app built on the Salesforce1 Customer Platform allows users to access and experience Salesforce everywhere on any form factor.


“It’s not about thousands of new computers, it’s about 50 billion connected things,” said Benioff. “Everything is on the net. The airplane engine is on the net. The Caterpillar tractor is on the net. The Coca-Cola vending machine is on the net.”

Salesforce1 is a new home base for Salesforce mobile, providing access to core customer relationship management (CRM) features as well as custom and partner apps. It’s also a platform for developers, who have access to 10 times more Salesforce APIs than they did before.

Wednesday 20 November 2013

Salesforce Integration With Klout




Recently from the client side got a requirement of getting Twiitter Account rating in Salesforce using Klout.
Go to Klout
Here what we are doing is just creating a visualforce page which is having custom fields of Account on the click of a button on detail page, our visualforce page will open, where user will get its Twitter Account rating.
It can be easily understand by the snaps below

Write your twitter Screen Name and click on the button "Get Rating"


After clicking that button user will get its Twitter rating as shown


Now the question rises how to do this thing?

So first of all, create an Account in Klout(Link provided above) then on the dashboard Click on "REGISTER AN APP", fill all the formalities and then you'll find this, marked in red is the key which we'll use to get Twitter score.


Here is the apex class

/**     Description    :    Ths class will Integrate Salesforce with Klout. 

  *

  *    Created By     :    Abhi Tripathi

  *

  *    Created Date   :    07/30/2013

  *

  *    Revisison Log  :    v1.0 - Created

  *

  *    Version        :    V1.0

**/



public with sharing class KloutWithSalesforceTwitterRatingUpdate {



  //Wrapper Class for First Response

  public class firstResponseParsingWrapper {

   

    //Response variables

    public String id;

    public String network;

   

    //Constructor

    public firstResponseParsingWrapper(String id, String network) {

      this.id = id;

      this.network = network;

    }

  }

 

  //Wrapper for second response

  public class KloutFinalResponseWrapper {

   

    //Score from the response

    public String score;

   

    //Constructor

    public KloutFinalResponseWrapper(String score) {

      this.score = score;

    }

  }

 



  //account

  public Account account { get; set; }

  public String twitterScore { get; set; }

 

  //constructor

  public KloutWithSalesforceTwitterRatingUpdate(ApexPages.StandardController stdController){

   

    //Initiallize

    twitterScore = '';



    //account record

    this.account = (Account)stdController.getRecord(); 

  }

   

    //Method for making callout and populating values retrieved from response is going to be diplayed on Visualforce Page

    public void kloutTwitterRating() { 



    try {

      //Http

      Http http = new Http();

     

      //Request

      HttpRequest req = new HttpRequest();

      req.setEndpoint('http://api.klout.com/v2/identity.json/twitter?screenName='+ twitterScore +'&key=4j6pe8zamj4dmh2by9tzv5sc');

      req.setMethod('GET');

     

      //Send request

      HTTPResponse firstResponse = http.send(req);

      System.debug('res::::::' + firstResponse.getBody());

     

      //Body

      String body = firstResponse.getBody();

     

      //Deserializing response

      KloutWithSalesforceTwitterRatingUpdate.firstResponseParsingWrapper parsedResponse = (KloutWithSalesforceTwitterRatingUpdate.firstResponseParsingWrapper)JSON.deserialize(body, firstResponseParsingWrapper.class);

      System.debug('parsedResponse:::::::' + parsedResponse);

     

      //String for id in response

      String responseId = parsedResponse.id;

      System.debug('responseId:::::::' + responseId);

   

      //Second request for score

      HttpRequest finalReq = new HttpRequest();

      finalReq.setEndpoint('http://api.klout.com/v2/user.json/'+ responseId +'/score?key=4j6pe8zamj4dmh2by9tzv5sc'); 

      finalReq.setMethod('GET');

     

      //Send request

      HTTPResponse lastResponse = http.send(finalReq);

      System.debug('lastResponse::::::' + lastResponse.getBody());

     

      //Body

      String finalBody = lastResponse.getBody(); 

     

      //Deserializing response

      KloutWithSalesforceTwitterRatingUpdate.KloutFinalResponseWrapper parseFinalResponse = (KloutWithSalesforceTwitterRatingUpdate.KloutFinalResponseWrapper)JSON.deserialize(finalBody, KloutFinalResponseWrapper.class);

      System.debug('parseFinalResponse::::::' + parseFinalResponse);

     

      //Assigning value

      account.Twitter_Rating__c = parseFinalResponse.score;

      account.Validate_Score_Successfully__c = true;

      account.Validate_Score_Last_Attempt__c = date.today();   

   



    }catch (exception e) {

     

      //Error messages

      ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'UserName Not Found, Sorry Try Again');

            ApexPages.addMessage(errormsg);

      System.debug('e:::::::' + e); 

    }   

   

  }

}


Here is the Visualforce Page

 <apex:page standardController="Account" extensions="KloutWithSalesforceTwitterRatingUpdate">  
    <!-- heading-->    
   <apex:sectionHeader title="Screen Name" subtitle="Twitter Rating"/>  
   <!-- form -->  
   <apex:form >  
     <!-- page messages-->  
     <apex:pageMessages />  
     <!-- page block -->  
     <apex:pageBlock mode="edit">  
         <!-- button -->  
         <apex:pageBlockButtons >   
           <apex:commandButton value="Get Rating" action="{!KloutTwitterRating}" />  
           <apex:commandButton value="Cancel" action="{!cancel}" />  
         </apex:pageBlockButtons>  
       <!-- block section -->  
       <apex:pageBlockSection title="Twitter Screen Name">  
         <!--Input Text>-->  
         <apex:pageBlockSectionItem >  
           Your Twitter Screen Name  
           <apex:inputText value="{!twitterScore}"/>  
         </apex:pageBlockSectionItem>  
       </apex:pageBlockSection>  
       <apex:pageBlockSection title="Rating With Related Values">  
         <!--Score-->  
         <apex:outputField value="{!account.Twitter_Rating__c}" />  
         <apex:outputField value="{!account.Validate_Score_Successfully__c}" />  
         <apex:outputField value="{!account.Validate_Score_Last_Attempt__c}" />   
       </apex:pageBlockSection>  
     </apex:pageBlock>   
   </apex:form>  
 </apex:page>  


Now get your Twitter Rating in your salesforce.
Cheers.......!!!!!


Monday 21 October 2013

360° With Test Class for Beginners #Salesforce

Here is have provided some of the basics to write perfect test class in salesforce



Here I'll explain with short snippets for the test classes
As the first one below

1:  @isTest(seeAllData=false)  
2:    private class Test_CountContactOfAccount {   
3:      //Method  
4:      static testMethod void CountContactOfAccountUnitTest() {  
5:      }  
6:    }  

(seeAllData=false)
You can set it true or false on your own, there are two condition for setting it true or false

1. If you are querying any object and fetching records from the database in the test class then set it to true as well there are few object we can't create in our test like "OpportunityLineItem" so to test with this object records, we need to query it with "seeAllData = true".

2. But if you are inserting test records of an object an then querying it in the test class, then you need to set it false otherwise test class won't be executed.


TESTING CUSTOM CONTROLLER AND STANDARD CONTROLLERr
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

How to cover custom controller in the test class?
If you need to cover constructor in the test class then simply define it in the test class, like CountContactOfAccount is the name of the class, just define it in the test class as in below, it will cover you whole constructor.

1:  //Calling contructor  
2:  CountContactOfAccount controller = new CountContactOfAccount();  

How to call StandardController (ApexPages.StandardController stdController)?
If you are using standard controller in the class then you need to define it in the test class too

1:  //Insert you object that you are using in your class  
2:  Account acc = new Account(Name = 'test');  
3:  insert acc;  
4:  //Define standard controller and pass inserted object   
5:  ApexPages.StandardController stdController = new ApexPages.StandardController stdController(acc);  
6:  //Now define your controller then pass standard controller you've just defined  
7:  CountContactOfAccount controller = new CountContactOfAccount(stdController );  

TESTING WITH STATIC AND VOID METHODS
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

How to call methods in the test class?
As you have seen above we have called controller , by using controller we can call static and void methods easily

So if the method is void like "public void something() {"

1:    //Calling contructor  
2:    CountContactOfAccount controller = new CountContactOfAccount();  
3:    //Calling void method  
4:    controller.something();  

But if the method is static like "public static list something() {"

then you can directly call the method no need to use deifned controller

1:    //Calling static method  
2:    CountContactOfAccount.something(listOfAccount);   

TESTING ApexPages.currentPage().getParameters().get('ID')
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

In this scenario you just need to put the value in the test of the variable that you fetching from the the URL

1:  //this method will put Id in the URL in the test class  
2:  currentPageReference().getParameters().put('id', '001c384348247811');  

ASSERTS (Most Important thing for test classes)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

There as three most commonly used asserts are there shown below, use asserts to check that your method are returning the correct values or not.

1:  System.assert(pBoolean)  
2:  System.assertEquals(pANY, pANY)  
3:  System.assertNotEquals(pANY, pANY)  

TESTING ERROR MESSAGES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

If you have error message in your test class and you want to cover it in test class then use this snippet

1:  //List of Message  
2:  List msgs = ApexPages.getMessages();  
3:  //Define a boolean variable  
4:  boolean msg = false;  
5:  //Loop through the messages of the page  
6:  for(Apexpages.Message msg:msgs){  
7:  //if there is error message then set the boolean to true  
8:  if (msg.getDetail().contains(‘Search requires more characters’)) msg = true;  
9:  }  
10:  //Assert  
11:  system.assert(msg);  
12:  //Single Assert on Error Messages  
13:  System.assert(ApexPages.getMessages()[0].getDetail().contains( 'User not found with given username' ));  

I think these are the most common scenarios for the Test classes that makes some mess.

Some key points:

1. Whole orgs average code coverage should be 75%, NOT each class.
2. Each trigger must more then 0% code coverage.

Hope I've helped you
CHEERS....!!!!

Wednesday 9 October 2013

Populating Map Using Loops


Many times I faced that I need a Value with its "Group of values".
Getting a unique value in Set is a idea, but what if you have a group of values associated with it.

So that time Map is the best way to settle it down.
we can take Map in many ways but here I am using
Map<String, List<String>>

I used this map allot, because populating this map with Key and Values is a great method.
Here am showing two methods same but in different ways.

Here is the first one.

1:  //Map of Opportunity  
2:    Map<Id, Set<Id>> opportunityMap = new Map<Id, Set<Id>>();  
3:    //Loop through the Opportunity records  
4:    for(Opportunity opp : opportunities) {  
5:      //Check if map key contains System Field  
6:      if(opportunityMap.containsKey(opp.Opportunity_Field__c)) {  
7:       //Get the Values of the Map and add Id to it.  
8:       opportunityMap.get(opp.Opportunity_Field__c).add(opp.Id);  
9:      }else {  
10:       //Creat a new Set at values and add Id to it.  
11:       opportunityMap.put(opp.Opportunity_Field__c, new Set<Id>{opp.Id});   
12:      }  
13:     }  
14:    }  

Here is the same but another way to write it

1:  //Loop through list  
2:  for(Opportunity opp : opportunities) {  
3:    //List of Opportunity  
4:    List<Opportunity> opps = new List<Opportunity>();  
5:   //Check for values of the Map  
6:   &#160 if(mapOpportuinties.containsKey(opp.Fishbowl_Customer_Number__c) == false) {  
7:    //Add to list  
8:    opps.add(opp);  
9:   &#160} else {  
10:     //Add to list  
11:     opps = mapOpportuinties.get(opp.Fishbowl_Customer_Number__c);  
12:     opps.add(opp);  
13:   }  
14:     //Populating map  
15:     mapOpportuinties.put(opp.Fishbowl_Customer_Number__c, opps);  
16:   }  

Using the above snippet every unique value will have a common group of values. its like

Object1 = value1, value2 value3
Object2 = value1, value2, value3
Object3 = value2, value3, value4
Object4 = value5, value6, value7


Cheers....!!!!!
Happy Coding.