Saturday, June 30, 2012


Well for developing a shiny  Visualforce  page or Image formula, sometimes you may need Icons.
If you look into standard pages, Salesforce uses a very large set of Icons in the standard pages.When you open a standard Case page you can see a "Brief Case" icon at the top of page or when you open a Account you can see a "Folder" icon at the top of the page.

To use these icons I generally look into the standard pages source to extract the exact URL of the icons and then reference them from Visualforce pages. Well that is really hectic. So ease the process I have included them all in Visualforce page and hosted the same.

http://blogforce9dev-developer-edition.ap1.force.com/salesforceicons


To use a icon just click on the icon and the image URL along the image tag will be populated in the footer text box.



Wednesday, June 13, 2012


[Update : Added Live Demo Links]

In the first part I already described you how to create your own wait/loading screen by blocking the full page.(In case you have missed the first part, you can quickly catch up by reading this blog )

Well what if you want to block specific part of the page ?? Like a pageblock or certain set of fields?? Well jQuery is again here to rescue! The good part of jQuery is it mixes with visualforce very readily to give out awesome results!

So lets see how to make this work using the good old jQuery UI Block Plugin.


 How to block a certain portion of page?

 For demo let us assume that we want to block a pageblock while a ajax request is in progress. To achieve this just give page block a unique id say "pb2Block" and use the same from the jquery ui block. Pass in the id of the pageblock which you want to block to the function, probably from a <apex:actionStatus> like this

Action Status
<apex:actionStatus onstart="blockElement('pb2Block')" onstop="unblockElement('pb2Block')" id="blockElement"/>

Command Button

<apex:commandButton value="Send Request" action="{!processAjaxRequest}" reRender="pb" status="blockElement"/>  

so whenever a ajax request is invoked by a command button the onstart event of the actionStatus is fired which blocks the the pageblock with id "pb2Block" and when the ajax request ends the ontstop event is fired which unblocks the pageblock.


 The JavaScript Functions : The demo visualforce page includes two Javascript function to block and unblock the element

              a) blockElement - takes the Salesforce id of the component to block.
              b)unblockElement - takes the Salesforce id of the component to unblock.



How the Page will look like?


Where is the Code?

Visualforce Page
 <apex:page controller="jQueryUIElementBlockDemo_Con">  
   <!-- Import Necessary Jquery js File and StyleSheets-->  
   <apex:includeScript value="{!URLFOR($Resource.jQuery, 'js/jquery-1.6.2.min.js')}"/>  
   <apex:includeScript value="{!URLFOR($Resource.jQuery, 'js/jquery-ui-1.8.16.custom.min.js')}"/>  
   <apex:includeScript value="{!URLFOR($Resource.jqPlugin, '/jquery.blockUI.js')}"/>  
   <apex:stylesheet value="{!URLFOR($Resource.jQuery, 'css/ui-lightness/jquery-ui-1.8.16.custom.css')}"/>  
   <script>  
     $j = jQuery.noConflict();  
     /*  
     *Pass the Id of the element to block   
     **/  
     function blockElement(sfId){   
       $j('[id$='+sfId+']').block({ message: '<img src="/img/loading32.gif" /><h1> Loading...</h1>',   
         css: {   
            border: 'none',   
            padding: '15px',   
            '-webkit-border-radius': '10px',   
            '-moz-border-radius': '10px',   
            opacity: .9  
         }   
       });   
       return false;  
     }  
     /*  
     *Pass the Id of the element to unblock   
     **/  
     function unblockElement(sfId){  
       $j('[id$='+sfId+']').unblock();  
     }  
   </script>  
   <apex:form >  
     <!--In the on start event of action status just call the blockPage method and onstop call the unblockPage js method-->  
     <!--Calling the blockElement js function blocks the page until unblockElement method is called-->  
     <!--Please note that the "Id" of element to black is passed to the js functions--->  
     <apex:actionStatus onstart="blockElement('pb2Block')" onstop="unblockElement('pb2Block')" id="blockElement"/>  
     <apex:sectionHeader title="jQuery UI Element Block Demo" subtitle="This is a demo of jQuery UI block for Blocking Certain portion of a page."/>  
     <apex:pageBlock title="Block This Section" id="pb2Block">  
       <apex:pageBlockSection title="Press the send request button to send a ajax request to the controller/server." collapsible="false">  
         <apex:pageBlockSectionItem >      
           <apex:outputLabel >Name</apex:outputLabel>  
           <apex:inputText />   
         </apex:pageBlockSectionItem>  
         <apex:pageBlockSectionItem >      
           <apex:outputLabel >Phone</apex:outputLabel>  
           <apex:inputText />   
         </apex:pageBlockSectionItem>   
         <apex:pageBlockSectionItem >      
           <apex:outputLabel >Mobile</apex:outputLabel>  
           <apex:inputText />   
         </apex:pageBlockSectionItem>  
         <apex:pageBlockSectionItem >      
           <apex:outputLabel >Zipcode</apex:outputLabel>  
           <apex:inputText />   
         </apex:pageBlockSectionItem>     
       </apex:pageBlockSection>  
       <apex:pageBlockButtons id="pb">  
         <!--Note here in the status attribute of command button the id of action status is provided-->  
         <apex:commandButton value="Send Request" action="{!processAjaxRequest}" reRender="pb" status="blockElement"/>  
       </apex:pageBlockButtons>  
     </apex:pageBlock>  
     <apex:pageBlock title="Unblocked Section">  
       <apex:pageBlockSection title="This Page block will remain unblocked" collapsible="false">  
         <apex:pageBlockSectionItem >      
           <apex:outputLabel >Name</apex:outputLabel>  
           <apex:inputText />   
         </apex:pageBlockSectionItem>  
         <apex:pageBlockSectionItem >      
           <apex:outputLabel >Phone</apex:outputLabel>  
           <apex:inputText />   
         </apex:pageBlockSectionItem>   
         <apex:pageBlockSectionItem >      
           <apex:outputLabel >Mobile</apex:outputLabel>  
           <apex:inputText />   
         </apex:pageBlockSectionItem>  
         <apex:pageBlockSectionItem >      
           <apex:outputLabel >Zipcode</apex:outputLabel>  
           <apex:inputText />   
         </apex:pageBlockSectionItem>     
       </apex:pageBlockSection>  
     </apex:pageBlock>  
   </apex:form>  
 </apex:page>  


Apex Controller
public class jQueryUIElementBlockDemo_Con {  
   public void processAjaxRequest(){  
     //Do some processing here  
     for(Integer i =0 ; i < 150000; i++){  
     }  
   }  
   //test method  
   static testMethod void test_ProcessAjaxRequest(){  
     new jQueryUIElementBlockDemo_Con().processAjaxRequest();  
   }  
 }  

Demo

Need more help to implement?

Don't worry packaged a demo visualforce page and all the necessary static resources like jQuery files, jQuery UI Block Plugin. Install the demo page from the here http://blogforce9dev-developer-edition.ap1.force.com/ProjectDetail?id=a0290000007rfwN




Saturday, June 9, 2012



[Update : Added Live Demo Links]

Ever wondered how to show user a wait or loading message while a ajax call from a visualforce page is in progress?
Well the answer is yes! You can always show a message to user using action status.
What if you want to block the page or some predefined area? Well the solution is jQuery UI block. Using this plugin you can actually block portions of visualforce page while the ajax action is processing.Well the part 1 I am demonstrating how to block the full page while a ajax request is in progress.

How it works?

Very simple from <apex:commandButton/> or <apex:actionFunction/> simply relate a <apex:actionstatus/> component which has a "onstart" and "onstop" event. Call the "blockPage" js function from the "onstart" and call "unblockPage" function from "onstop" attribute.

How the Page will look like?




Visualforce Page


<apex:page controller="jQueryUIBlockDemo_Con">  
   <!-- Import Necessary Jquery js File and StyleSheets-->  
   <apex:includeScript value="{!URLFOR($Resource.jQuery, 'js/jquery-1.6.2.min.js')}"/>  
   <apex:includeScript value="{!URLFOR($Resource.jQuery, 'js/jquery-ui-1.8.16.custom.min.js')}"/>  
   <apex:includeScript value="{!URLFOR($Resource.jqPlugin, '/jquery.blockUI.js')}"/>  
   <apex:stylesheet value="{!URLFOR($Resource.jQuery, 'css/ui-lightness/jquery-ui-1.8.16.custom.css')}"/>  
   <script>  
     $j = jQuery.noConflict();  
     //function to block the whole page  
     function blockPage(){   
       $j.blockUI({ message: '<img src="/img/loading32.gif" /><h1> Loading...</h1>',   
         css: {   
          border: 'none',   
          padding: '15px',   
          '-webkit-border-radius': '10px',   
          '-moz-border-radius': '10px',   
          opacity: .9  
         }   
       });   
       return false;  
     }  
     //function to unblock the page  
     function unblockPage(){  
       $j.unblockUI();  
     }  
   </script>  
   <apex:form >  
     <apex:sectionHeader title="Jquery UI Block Demo" subtitle="This is a demo of jQuery UI block for Blocking the whole page when a ajax request is in progress!"/>  
     <!--In the on start event of action status just call the blockPage method and onstop call the unblockPage js method-->  
     <!--Calling the blockPage js function blocks the page until unblockPage method is called-->  
     <apex:actionStatus onstart="blockPage()" onstop="unblockPage()" id="blockUI"/>  
     <apex:pageBlock title="Jquery UI Block Demo">  
       <p>Press the send request button to send a ajax request to the controller/server.</p>  
       <apex:pageBlockButtons id="pb">  
         <!--Note here in the status attribute of command button the id of action status is provided-->  
         <apex:commandButton value="Send Request" action="{!processAjaxRequest}" reRender="pb" status="blockUI"/>  
       </apex:pageBlockButtons>  
     </apex:pageBlock>  
   </apex:form>  
 </apex:page>  


Apex Controller


 public class jQueryUIBlockDemo_Con{  
   public void processAjaxRequest(){  
     //Do some processing here  
     for(Integer i =0 ; i < 10000; i++){  
     }  
   }  
   //test method  
   static testMethod void test_ProcessAjaxRequest(){  
     new jQueryUIBlockDemo_Con().processAjaxRequest();  
   }  
 }  

Demo
You can check the live demo from http://blogforce9dev-developer-edition.ap1.force.com/jQueryUIBlockDemo

Need more help to implement?

Don't worry packaged a demo visualforce page and all the necessary static resources like jQuery files, jQuery UI Block Plugin. Install the demo page from the the Project Detail Page http://blogforce9dev-developer-edition.ap1.force.com/ProjectDetail?id=a0290000007rfwI and go to "jQueryUIBlockDemo". The code is well commented and ready to use.

Thursday, May 24, 2012



[You can see this blog for a more elegant solution ]

This was unknown to me and in past months I tried hard to use Datetime field in Dynamic SOQL query, well after doing lots of permutations and combinations I finally figured it out! which I though was not possible.

Well I guess this is worth sharing so here it is.

You can compare any Datetime field in Dynamic SOQL where query! Just keep in mind the format should be correct. You can use the format method to format datetime field like this where System.Now() is my Datetime field.

System.Now().format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\''));


OR




DateTime dt = System.Now();//initialize datetime with current datetime

String formatedDt = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');//format the datetime to make it Dynamic Soql ready


So a sample query to extract all the Accounts where the CreatedDate is less than Today will be


DateTime dt = System.Now();//initialize datetime with current datetime
String formatedDt = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');//format the datetime to make it Dynamic Soql ready
Database.query('Select Id FROM Account WHERE CreatedDate<'+formatedDt);



Wednesday, May 23, 2012


Well Summer 12 is approaching with lots of new enhancements including fieldset describing feature from within apex.This was most awaited feature for me because have done a lot work around to achieve the same result.
Thanks to Abhinav Gupta from whose blog(http://www.tgerm.com/2012/01/recordtype-specific-picklist-values.html) I actually got Idea how to develop these feature. Sometime back Abhinav wrote about describing picklist based on recordtype, I took the code from there and made some changes in the classes and Eureka! a fieldset describer is at your service.

Well since Summer 12 is approaching very fast and after that this will not be of any use,So this for those user who are still stuck in Spring 12 and need a work round to get a List of field names that a field set contains. Well you can install the package from this link https://login.salesforce.com/packaging/installPackage.apexp?p0=04t900000009eGV

It mainly contains two classes. A visulaforce controller to generate a page using the fieldset and parsing the response and another main class to which we send Field Set name and Object name.

How to describe a fieldset?
Its very easy just follow the below code.

List<String> fieldNameList = FieldSetDescriber.describeFieldSet('<Your Object Name>','<Your Field Set Name>');



Tuesday, May 22, 2012


Well few days ago I came across a requirement where on button click user should be asked for confirmation about the action in a modal window. Well we don't have such component in Visualforce!. But hey! Wait! I guess the jquery has it! Why dont we use that one ?

So started mixing Visualforce with Jquery and Result? A modal dialog box!


Well Don get scared its very easy to implement!

Just go to apex/showDialog page for demo and press the showDialog button.

To summarize you can just open a modal window by just calling the openDialog method included in the showDialog page,make sure that you include necessary files while you implement the same in your VF page.

Exp : openDialog('This is a custom dialog','Alert')" ;
Where the first argument is the dialog body and the second is the Dialog title.

Thursday, February 2, 2012


[Update:Added Live Demo Link And Unmanaged package Link]

Talking about the Visualforce component , It has all the component that can make a decent UI but its only limited to the word "DECENT", you can create very rich UI using visualforce.What if  a simple slider needs to be implemented in a Visualforce page? Alas! there are no such component in the standard Visualforce Library.

Well you can create one for yourself using jQuery Library. Talking about jQuery its very stable javascript library with lots of feature! It Readily mixes with visualforce to create very rich UI.

So lets have look on the code to create a simple slider using jQuery.

Visualforce Page

<apex:page controller="jqSlider_Con">  
      <!--Inculding the required jQuery Files and CSS--->  
      <apex:includeScript value="{!URLFOR($Resource.jQuery, 'js/jquery-1.6.2.min.js')}"/>  
      <apex:includeScript value="{!URLFOR($Resource.jQuery, 'js/jquery-ui-1.8.16.custom.min.js')}"/>  
      <apex:stylesheet value="{!URLFOR($Resource.jQuery, 'css/ui-lightness/jquery-ui-1.8.16.custom.css')}"/>  
      <script>  
        $j = jQuery.noConflict();   
        //this function will be called as soon as page load is complete  
        $j(function(){  
          //calling the createSlider Method to create the slider in designated location  
          createSlider('slider','disp','idInputHidden',0,0,1000);  
       });  
        /*  ----------------------------  
        *  Method to create the Slider  
        *  ----------------------------  
        *  PARAMETERS : "destination" = The Id of the div where the slider needs to be created  
        *         "dispOutput" = The Id of the div where the slider value needs to be displayed  
        *         "idInputHidden" = The Id of the apex inputHidden component  
        *         "startVal" = initial position of slider  
        *         "minVal", "maxVal" = minimum and maximum value of slider  
        **/  
         function createSlider(destination,dispOutput,idInputHidden,startVal,minVal,maxVal){  
           $j("#"+destination).slider({   
             range: false,   
             min: minVal,  
             max: maxVal,  
             values: [startVal],  
             slide: function(event, ui){   
             //This function executes every time slider is moved and applies the slider values   
             //to the input fields as well as the output below the slider  
               $j("[id$="+idInputHidden+"]").val(ui.values[0]);  
               $j("#"+dispOutput).html('$' + ui.values[0]);  
             }  
           });  
           //write the initial value in the display div  
           $j("#"+dispOutput).html('$' + startVal);  
         }   
     </script>  
     <apex:form >  
       <apex:pageBlock title="jQuery Slider">  
         <!--Slider will be created Here-->  
         <div id="slider"/><br/>  
         <!--Slider Output here-->  
         <div id="disp"/>  
         <apex:inputHidden value="{!sSliderField}" id="idInputHidden"/>  
         <apex:pageBlockButtons >  
           <apex:commandButton value="Submit" action="{!checkSliderVal}" reRender="idInputHidden"/>  
         </apex:pageBlockButtons>  
       </apex:pageBlock>  
     </apex:form>  
  </apex:page>  

Controller Class

 public class jqSlider_Con {  
     public String sSliderField { get; set; }  
     public void checkSliderVal(){  
       System.debug('###-------------+'+sSliderField );  
     }  
  }  


About the method "createSlider": In this example I have created a function "createSlider". Which takes in the id of div where the slider neds to be rendered, the id of div where the the values should be displayed, the id of the apex inputHidden(so that values can be passed to controller), the initial value of the slider and range of the slider(max and minimum value).This method can be called multiple times for creating slider in different location of page.




Passing Values to Controller: The values are passed to controller using apex inputHiddent element which assigns the value to a public variable. You can check the communication by clicking the submit button, the value of the slider will assigned to the sSliderField and can be seen in debug logs.


Demo

You can check the live demo by going to 
http://blogforce9dev-developer-edition.ap1.force.com/jQSlider


Package For Installation

Install the demo in your developer Org by using this url : 
https://login.salesforce.com/packaging/installPackage.apexp?p0=04t90000000LxdC