Tuesday, February 21, 2012

IE double click on List Select Option


I recently came across a very strange issue in IE. The requirement was adding a action on select option double click of a list. The double click is working fine for all other controls in IE but was not working in case of select option.
In Mozilla & Chrome below code working as expected --

$("#list1").find("option").unbind('dblclick');
$("#list1").find('option').dblclick(function(){
//put your code here whatever needed on double click
});

but IE not able to get the double click on select option & not performing the action as other browsers. I did some Rnd on the issue but haven;t got any solution, even in some post mentioned that this is not supported on IE.
I am thankful to my friend Kapil Choudhary who help me to fix this issue. Here is the script for IE which will work on all browsers perfectly.

$("#list1").unbind('dblclick');
$("#list1").dblclick(function () {
$("#list1").find('option:selected').each(function() {
//put your code here whatever needed on double click
});
});

Saturday, January 21, 2012

Use Apply With Linked In Salesforce Site


I am back after couple of months. Recently I started to look into the Linked in Salesforce integration. I found this very interesting & keep diging & finally implemented as web site page which is using apply with linked-on button. Here are the complete steps by which you can add “Apply with Linked In” button to your VF site.
1-go this below url & create a developer login on linked in-
https://www.linkedin.com/reg/join
2-Login with the linked-in password by below url
http://developer.linkedin.com/user/login
2-go to below url & click on “Create On API Key” link
https://developer.linkedin.com/apply-getting-started

3-You will see a page as below.


Click on “Add New Application” Link, fill all the values in the next form & in the “JavaScript API Domain:” text box enter your site url. For Eg In my case it was “http://kgblog-developer-edition.ap1.force.com/apex/ApplyWithLinkedIn”
Click on save & you will see some information on the page, something like api key, secret key etc. do not close this window, it will be used in next step.




4-Now go to below url 
https://developer.linkedin.com/plugins/apply & click on Get Code button. You will see something link as below-
<script src="http://platform.linkedin.com/in.js" type="text/javascript">
  api_key: YOUR_API_KEY
</script>
<script type="IN/Apply" data-companyid="XXXX" data-jobtitle="test" data-email="XXXXX@yahoo.com"></script>
Copy this js code & put it in your vf page. 
Replace api key in above code which we get step-3.
5-now run the site page, you will see apply with linked button on your page.
If you are building an application in which you need some input something like name, email & some other information from the linked site than you can use “apply with linked in” button .

 <script type="IN/Apply" data-companyid="XXXX" data-jobtitle="Manager" data-joblocation="Delhi" data-callback="onCallBack" ></script>
& the js method will be as below



<script>


        function onCallBack(p1,p2) {
            document.getElementById("txt1").value = p1.person.firstName;
            document.getElementById("txt2").value = p1.person.lastName;
            document.getElementById("txt3").value = p1.person.emailAddress;
            document.getElementById("txt4").value = p1.person.publicProfileUrl;
         
        }
     
</script>


take a look of the demo by below url

Friday, March 11, 2011

Play with bulk data - Apex Batch Class


Wherever you want to perform some operation on bulk data the Batch Class is the right option for that.
Here I am going to explain the steps of how a batch class can be implemented….

declare a batch class

global class TestBatchClass implements Database.Batchable <Sobject>,Database.Stateful{
   public Id accountId{get;set;}
}

The syntex will be same for all batch classes you only need to change the class name “TestBatchClass”. 

Here I am taking an example of account updation, suppose we want to perform some operation on accounts than we need to take a property accountId in the batch class. This property will be used when we create test class for this batch class.

In below lines I am declaring constructor for the batch class
public TestBatchClass(){
}
   
public TestBatchClass (Id accountId){
        this.accountId = accountId;
   }

   One noticeable point is second constructor will be used when we write test 
   class for this batch class. You can avoid the parameterize constructor if does not         
   required in your case.

  //--------------------------------------------------------------//
    //Start Method :->
    //-------------------------------------------------------------//
    global Database.Querylocator start(Database.BatchableContext ctx){
            return null;
    }

   As per the name start method will be called as the class start execution. In this method  
   you need to change the query as per you requirement. For Eg we are using a query to get  
   accounts on which we will perform some operations. 
  
  
 if(test.isrunningTest()) {
             return Database.getQueryLocator([select Id,Name from Account limit 5]);
   }
   else{
        return Database.getQueryLocator([select Id,Name from Account where Id=: accountId]);
    } 
  
   Here one noticeable point is there are two queries to get the accounts. Test.isrunninTest 
  checks and execute the first query if we are calling this batch class from test class, it 
  will useful because we should not perform the batch logic for thousands of account if it is 
  calling from Test class.

 
  //--------------------------------------------------------------//
  //Execute method
  //-------------------------------------------------------------//
  global void execute(Database.BatchableContext ctx, List<Account>){

   }  

 Execute method is the most important method of the batch class, here you need to write the  
 logic on the basis of you want to perform update/insert/delete operation. For Eg if we need 
 to change a field the fetched accounts than the code will be look like as below-


 List<Account> listOfAccounts = (List<Account>)scope;
  List<Account> listOfAccountsUpdated = new List<Account>();
  Account newAccount;
  for(Account acct :listOfAccounts) {
newAccount = new Account(Id = acct.Id);
            newAccount.checked__c = true;
listOfAccountsUpdated.add(newAccount);
  }
  If(listOfAccountsUpdated.Size()>0){
       Update listOfAccountsUpdated;
  }


  At the end of the batch class there is finish method, generally you need not to write any 
  code in this method, you can write some code if there is any specific requirement at the 
  end of batch execution. for eg. send a mail in finish.

 
   //--------------------------------------------------------//
  //Finish method
  //----------------------------------------------------------//
  global void finish(Database.BatchableContext ctx){
       
  }
   The whole class code will be look like as below-


    
  global class TestBatchClass implements     Database.Batchable <Sobject>,                                          Database.Stateful {
   public Id accountId{get;set;}
       public TestBatchClass(){
       
   }
   
   public TestBatchClass (Id accountId){
      this.accountId = accountId;
   }

    //--------------------------------------------------------//
   //Start Method :->
   //----------------------------------------------------------//
   global Database.Querylocator start(Database.BatchableContext ctx){
        if(test.isrunningTest()) {
            return Database.getQueryLocator([
select Id,Name from Account limit 5]);
        }
        else{
            return Database.getQueryLocator([
select Id,Name from Account where Id=: accountId]);
        }            
    }
   
    //--------------------------------------------------------//
    //Execute method
    //---------------------------------------------------------//
   global void execute(Database.BatchableContext ctx, List<Accountscope){
       List<Account> listOfAccounts = (List<Account>)scope;
List<Account> listOfAccountsUpdated = new List<Account>();
Account newAccount;
for(Account acct :listOfAccounts) {
newAccount = new Account(Id = acct.Id);
              newAccount.checked__c = true;
listOfAccountsUpdated.add(newAccount);
}
If(listOfAccountsUpdated.Size()>0){
              Update listOfAccountsUpdated;
}

    } 
    //-------------------------------------------------------//
    //Finish method
    //--------------------------------------------------------//
    global void finish(Database.BatchableContext ctx){
       
    }
 }



 it's done..!!

  
 If you want to test this batch class then open the system log & execute below lines of 
 code 

 TestBatchClass obj = new TestBatchClass ();
  Database.executeBatch(obj,5);

 In execute batch you can specify how many records need to take in a batch, for example we 
 are taking 5. Generally we should not take more than 200 for this.

 Now you can check the implementation flow/bugs in the system log detail & manipulate the  
 logic of the batch class.

After executing this batch class sometimes you will not able to save data in the class if 
 there any error thrown in by batch class & batch class not execute successfully. For this 
 you need to go to Setup->Monitoring->Apex Jobs & then Abort the job. In our case it will be 
 “TestBatchClass”.  


Happy Coding...!!

Monday, January 31, 2011

Access Salesforce Content on your VF Page


Recently I came across the Salesforce Content Related requirement, Salesforce only provide limited access to the content (ContentVersion object). It does not allow you to access the Content & show in your custom VF page.
Here Me & my friend Vinod did a lot of Rnd & found a easy solution. This post will save your lot of time if you are doing this kind of task with Content.

To implement its example, take a div
<div class="centerContent" id="myDiv"></div>
There are two simple JS function which will help to get salesforce content –

function OpenDoc(docId){
 
var htmlString = '<div id="chatterFileViewerPanel" class="chatterFileViewerPanel"><br/><br/> <embed height="500px" align="middle" width="500px" type="application/x-shockwave-flash" wmode="transparent" pluginspage="http://www.adobe.com/go/getflashplayer" allowfullscreen="true" allowscriptaccess="sameDomain" ' +
'name="renditionLarge" bgcolor="#f3f3f3" quality="high" id="renditionLarge" '
+'flashvars="shepherd_prefix=/sfc/servlet.shepherd&amp;v='+docId+'&amp;mode=chatterfilepreview&amp;in_tests=false" src="/static/101210/sfc/flex/DocViewer.swf" /> </div> ';
           
        document.getElementById('myDiv').innerHTML =  htmlString;
        PrepareFlexComponent(docId);
       
       
}

function PrepareFlexComponent(docId){
        insertFlexComponent('/static/102010/sfc/flex/DocViewer',
                'shepherd_prefix=/sfc/servlet.shepherd&v='+docId+'&mode=chatterfilepreview&
                amp;in_tests=false','500px', '500px', '#f3f3f3', 'chatterFileViewerPanel', 'renditionLarge', false,
                { adobeFlashVersionLbl : 'You must enable or download Adobe Flash Player version 9.0.115 or 
                   later to use this feature.',
                   downloadAdobeLbl : 'Download Adobe Flash Player', downloadAdobeLinkLbl : 'Click the 
                   link below to download the Adobe Flash Player:' closeDialogLbl : 'Cancel'});
                   
        Ninja.Flex.ContentStaticResourcesBase = '/static/102010/sfc';
        Ninja.Flex.ShepherdController = '/sfc/servlet.shepherd';
}  



Put below script as it is in your page


<script type="text/javascript" src="/static/101210/js/functions.js"></script>
<script  src="/EXT/ext-3.0.0/ext.js" type="text/javascript"></script>
<script  src="/jslibrary/1294273133000/main.js" type="text/javascript"></script>
<script  src="/jslibrary/1289945557000/Chatter.js" type="text/javascript"></script>
<script  src="/jslibrary/1289346548000/ChatterFiles.js" type="text/javascript"></script>
<script  src="/jslibrary/labels/1295420058000/en_US.js" type="text/javascript"></script>
<script type="text/javascript" src="/static/101210/desktop/desktopAjax.js"></script>
             
<script type="text/javascript" src="/static/101210/sfc/javascript/lib/AC_OETags.js"></script>
 
<script>

   function postSubDocument(url, internal) {
      var form = document.getElementById("formSubDocument");
      form.action = url;
      form.internal.value = internal;
      form.submit();
    }
             
    function hideFlex() {
        document.getElementById('IE6Confirm').style.display = "block";
    }
                    
    function showFlex() {
        document.getElementById('IE6Confirm').style.display = "none";
    }
                    
    function cancelDownload(url) {
        showFlex();
    }
                    
    function triggerDownload(url) {
        window.open(url);
    }
                    
    function downloadDocForIE6() {
        showFlex();
        triggerDownload(_url);
    }
                    
    function downloadDoc(url) {
        var isIE6 = (navigator.userAgent.indexOf("MSIE 6") != -1);
        if (isIE6) {
            _url = url;
            hideFlex();
        }
        else {
           triggerDownload(url);
        }
     }


  </script>

It’s done! Now call function OpenDoc by passing a ContentId & you can see your Content on VF page :)
It will show the content same as the salesforce standard way.
Enjoy..!!



Tuesday, January 25, 2011