Saturday, May 18, 2013

Synchronising Apex Triggers with Workflow Time Triggers

Few days back I came across a requirement which where user needed a scheduled job, that invokes after x days of record creation and creates a clone record.

So my initial thought was Scheduled Apex job but that doesn't seem to fit for this case because of the limit on number of scheduled jobs that can be scheduled. So started thinking of another approach and solution was pretty simple

WorkFlow Time Trigger + Apex Trigger = Invoke Apex @ Future Date.

Wasn't the equation clear enough? 
Well let me explain the same in steps.

  • Define Workflow
    • Create a new workflow 
      • Specify a entry criteria that fits your requirement. In my case it needs to go inside the  workflow as soon as it is created. So I just used formula editor and entered formula as "TRUE" as Rule Criteria and Evaluation Criteria as "Created". 
    • Add Time Trigger
      • Add time trigger to the apex and provide appropriate time duration. In my case I chose 30 days from "Rule Trigger Date"(Which will be same as created date). 
    • Add a field Update to the time trigger
      • Now add a field update that will be used by Apex Trigger to execute a class. So In my case the custom object had a custom field with the name "invokeApex__c" which was by default set to "FALSE".
      • Create a field update that will set this to "TRUE"

Well now you are done with the configuration part. Lets write some code
  • Creating a APEX Trigger.
    • Sample trigger will look like
       trigger invokeApexCloning on Demo__c (after update__c) {  
         for(Demo__c demo : trigger.New){  
           if(demo.invokeApex__c != trigger.Old.get(demo.Id).invokeApex__c && demo.invokeApex__c == TRUE){  
             //invoke your action here  
             CloningUtil.clone(demo);  
           }  
         }  
       }  
      
       
    • Modified version if you want to bulkify the same
       trigger invokeApexCloning on Demo__c (after update__c) {  
         List<Demo__c> demoList = new List<Demo__c>();  
         for(Demo__c demo : trigger.New){  
           if(demo.invokeApex__c != trigger.Old.get(demo.Id).invokeApex__c && demo.invokeApex__c == TRUE){  
             demoList.add(demo);  
           }  
         }  
         //invoke bulk action from here  
         CloningUtil.clone(demoList);  
       }  
      
Well thats all we are set now. This code setup will let you clone the records after 30 days. Now you know the secret to mix Time Triggers with Apex Triggers ;)

Thursday, May 9, 2013

Easy Jquery Tabs for Visualforce Pages

[Live Demo]

This is another blog regarding so called Easy Jquery Component.Now this component helps you to generate  the Jquery Tabs very easily in a Visualforce pages. Follows the same syntax as the standard component, with added Jquery goodness. Pretty straight forward and can include standard components inside the Jquery tabs.

How it looks like ?



How to use the components?

  • <c:Tabpanel> : A wrapper component that holds all the tab together inside a panel.
  • <c:Tab> : Represents a single Tab inside a tabpanel.
Below is a sample code to generate a Jquery Tab

<!--Tab Panel -->  
   <c:Tabpanel >  
     <!--First Tab-->  
     <c:Tab title="Tab 1">  
       This is a tab 1. You can add standard component inside also.  
       <apex:pageBlock title="Standard page block">  
         This is a pageblock component.  
         <br/>  
         <apex:pageBlockSection title="This is a section" collapsible="false">  
         </apex:pageBlockSection>  
       </apex:pageBlock>  
     </c:Tab>  
     <!--Second Tab -->  
     <c:Tab title="Tab 2">  
       This is a tab 2  
     </c:Tab>  
   </c:Tabpanel>  

Codebase
Demo : http://blogforce9dev-developer-edition.ap1.force.com/jQueryTabDemo
Unmanaged Package : https://login.salesforce.com/packaging/installPackage.apexp?p0=04t90000000MPs1
Github : N/A

Tuesday, April 23, 2013

Visualforce Accordion Component - An Easy Way to Implement jQuery Accordion in a VF Page

[Live Demo]

[Updated To 1.1]

Well while working with Visualforce pages, you will feel like the UI is kinda old and not that catchy. But believe me VF is not that bad at all!  You just need to add the right ingredients.

And guess what the most stable js library JQUERY and JQUERY UI mixes very well with Visualforce pages, to give awesome results. JQUERY has very long list of widgets and plugin and they are pretty easy to implement. One of the commonly used widget is the Accordion. Implementing this in a VF page is not that difficult. But for quick implementation a vf component will be really handy. So here is Visuaforce Components that automatically does all the JQUERY stuff for you and creates a awesome looking Accordion. No javascript coding just include the tags/components and enjoy the JQUERY love. 


How to use the component?
The accordion is generated by the help of two components :
  • <c:Accordion> : This is the wrapper which holds all the accordion section together
  • <c:AccordionSection> : This element actually gets converted into a accordion section.
Below is the code to generate a accordion similar to the attached screen.


<c:Accordion >  
     <c:AccordionSection title="Section 1">  
       Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque.   
       Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh  
       Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada.   
       Vestibulum a velit eu ante scelerisque vulputate.  
     </c:AccordionSection>  
     <c:AccordionSection title="Section 2">  
       Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus.   
       Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit,   
       faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.  
     </c:AccordionSection>  
   </c:Accordion>  

The plugin is updated to 1.1. The new plugin features a script manager that imports only necessary plugins,script and stylesheet. This mainly helps when multiple Accordion component are used on the same page. Instead of importing the resources multiple times they are imported only once by the script manager.


Sunday, March 31, 2013

PageBlockTable Enhancer v2.0 - And the Awesomeness Continues

[Live Demo]
Well I have been working on this new component "PageblockTable Enhancer" for a while and am trying to merge all the functionality that were featured in my earlier components " Sortable Pageblock table" and "Enhanced PageBlock Table".

The version 2.0b brings in some exciting changes like inclusion of Pagination along with the Sorting capability.

Check out the demo of the newest version here
http://blogforce9dev-developer-edition.ap1.force.com/PageBlockTableEnhancerDemo



How to use?

Parameter :

  • targetPbTableIds : comma-separated Ids of the target Pageblock tables




 <c:PageBlockTableEnhancer targetPbTableIds="mid,mid2"/>    
   <apex:pageBlock >  
       <apex:pageBlockTable value="{!accounts}" var="acc" id="mid">  
         <apex:column value="{!acc.Name}"/>  
       </apex:pageBlockTable>   
        <apex:pageBlockTable value="{!accounts}" var="acc" id="mid2">  
         <apex:column value="{!acc.Name}"/>  
       </apex:pageBlockTable>     
   </apex:pageBlock>



Codebase:
Github: NA



PS: This is a beta version of the plugin, some bug may exists, If you find one, I will be more than happy to resolve them.

Sunday, March 17, 2013

Working with Fieldsets

Fieldsets are one of the feature that you may use to make your visualforce dynamic. They are dynamic and allows display of component on the VF page dynamically. We  can add, remove, or reorder fields in a field set to modify the fields presented on the Visualforce page without modifying any code. Fieldsets are not very new to Force.com, but I have a habit searching for syntax before writing one, so writing this one for my reference and all others like me ;) .

Below are some of the example to show how fieldsets can be used in VF page.


  • Simplest example
<apex:page controller="Account_Con">   
   <apex:repeat value="{!$ObjectType.Account.FieldSets.AccountDetails}" var="f">    
    <apex:outputText value="{!myAccount[f]}" /><br/>   
   </apex:repeat>   
  </apex:page>   


  • To render a PageBlocksection
 <apex:page controller="Account_Con">  
   <apex:pageBlockSection>  
        <apex:repeat value="{!$ObjectType.Account.FieldSets.AccountDetails}" var="f">    
            <apex:outputField value="{!myAccount[f]}" />   
        </apex:repeat>   
   </apex:pageBlockSection>  
  </apex:page>   

  • To render a PageBlockTable
 <apex:page controller="Account_Con">  
   <apex:pageBlockSection>  
        <apex:pageBlockTable>  
             <apex:repeat value="{!$ObjectType.Account.FieldSets.AccountDetails}" var="f">    
                 <apex:column value="{!myAccount.Contacts[f]}" />  
             </apex:repeat>  
     </apex:pageBlockTable>         
   </apex:pageBlockSection>  
  </apex:page>   

All the above listed example dynamically display the records from the fieldset but what about the controller?
we can't just hardcode the queries here.To further extend the solution grab the code for the AVCommonUtil Class that generates query from fieldset/s.

Solution
You can use the util class to generate query dynamically from the fieldsets.

public class Account_Con(){   
  public Account myAccount{get;set;}   
  public AccountCon(){   
      AVCommonUtils util = new AVCommonUtils();  
      String query = util.generateQueryFromFieldSet('Account','AccountDetails',null,'LIMIT 1');  
       accObj = database.query(query);    
  }   
}   

Saturday, March 2, 2013

PageBlockTable Enhancer - A Awesome New Visualforce Component

[This VF Component is Updated to version 2.0. Please use click on this link to read about the latest component]
If you are going through my blog, few days back I pushed a new version of a Enchanced Pageblock Table. The main draw back of this component is to you have to modify your existing controller to provide the FieldList to the component.

But what If I give a new plugin that can work on existing Page block tables and that to without any code changes in the controller ?

So here is the initial version of The PageblockTableEnhancer, that can be included into existing page to enhance your existing PageBlockTables. Just include the component into the page and specify Ids of the target PageblockTable in csv format.

Features
Currently PageblockTableEnhancer  only supports sorting. You can apply sorting feature to any number of tables in a page by using a single PageblockTableEnhancer  component.

How to use?

Parameter :

  • targetPbTableIds : comma-separated Ids of the target Pageblock tables




 <c:PageBlockTableEnhancer targetPbTableIds="mid,mid2"/>    
   <apex:pageBlock >  
       <apex:pageBlockTable value="{!accounts}" var="acc" id="mid">  
         <apex:column value="{!acc.Name}"/>  
       </apex:pageBlockTable>   
        <apex:pageBlockTable value="{!accounts}" var="acc" id="mid2">  
         <apex:column value="{!acc.Name}"/>  
       </apex:pageBlockTable>     
   </apex:pageBlock>  


Enhanced Page Block Tables



Codebase:




Thursday, February 28, 2013

Enhanced Pableblock Table For Visualforce Pages

[Update : Have a look at awesome new PageblockTable Enhancer Component here]

Well I am really happy that finally my work on Enhanced Pageblocktable is almost complete.The Sortable Pageblocktable component is now Enhanced Pageblock table with added functionality of pagination , athough the Sortable Pageblocktable component still exists.

Now you can add functionality like pagination, sorting without any extra coding.The component uses the jQuery table sorter plugin to add the sorting and pagination features to the page block tables.

Features of Enhanced Pageblock Table

  • Sorting : Supports sorting of columns by clicking at the column header.



  • Pagination : Supports client side pagination of the table.
  • Native Look And Feel : Enhanced Pageblock table component uses the similar look and feel of a standar pageblock table and mixes with the existing UI easily.

How to use? 
Pretty Simple!, just use the below code.

Parameters
sObjList : List of sObject To display
fieldList : List of field API names to display in the table as column.


<c:EnhancedPageBlockTable sObjList="{!accounts}" fieldList="{!fields}"/>