Saturday, October 26, 2013

Well this is very early release, was a bit excited when I found that it actually works! and I couldn't wait to till completion of app. So I just created a bare minimum version of this component to Post the same.

Well talking about the component it is pretty simple and uses  JSZip (can be found here) to create zip files inside a Visualforce page. I will be posting the  details about how VFZip actually works in subsequent blogs.
For now I am just posting a small unmanaged package that can create Zip files, I will be working on this further to expand the same.

Features of VFZip
  • Can zip multiple files.
  • You can provide your own name for the generated zip files.
  • Can zip upto 5mb(I know its kinda discouraging but I guess I can take it further around 15mb)

How to use ?
The syntax is pretty simple 

<c:VFZip attachmentIds="00P90000004wnj8" generatedFileName="myzip" mode="button"/>  

[Please make sure you replace the id with one from your org]

  • attachmentIds : Comma separated IDs of attachment to zip
  • generatedFileName : File name of the generated zip.
  • mode : Defines the launch mode. If set to "button" the component will display a button to launch the zip window. If set to "auto" the component will launch the zip window as soon as Visualforce page is opened.

Friday, October 11, 2013

Well I know this is nothing new, I just wanted to jot it down quickly! supports triggers @Attachments but as of now you can't create them from standard UI, but hey wait we can create them with the NEW Developer Console. Yes the Developer console(v29.0) is more like Eclipse now and allows you to create a triggers on Attachments.

So here is how you can create a trigger @ Attachment

  • Click at your Name(left top corner) and Select "Developer Console"

    Developer Console Selection

  • Now when you are inside Developer Console . Go to File >>  New >> Apex Trigger

    New Apex Trigger
  • Select "Attachment" in sObject and and give a good Name to your trigger.  

Thats it ! you are ready to go!.

Sample Attachment Trigger

Description : Say you want to create a trigger on Attachment which will update a custom field "Last_Attachment_Added_Date__c" on "Account" with the date when the Last Attachment was added.

Prerequisite : "Last_Attachment_Added_Date__c" custom  Date/Time field on Account Standard Object.

 trigger AttachmentTrigger on Attachment (before insert) {  
   Boolean isAccountAttachment = FALSE;    
      List<Account> accounts = new List<Account>();  
        Set<Id> accIds = new Set<Id>();  
        for(Attachment att : trigger.New){  
             /*Check if uploaded attachment is related to Account Attachment and same account is already added*/  
             if(att.ParentId.getSobjectType() == Account.SobjectType && (!accIds.contains(att.ParentId)){  
                  //prepare a account object for update  
                                      new Account(  
                                                          Last_Attachment_Added_Date__c =  
                     //add the accountid in set to eliminate dupe updates                                     
        //finally update accounts  
        update accounts;  

The trigger is pretty simple and the trick is in detecting on which object the attachment is being added.

In the above code we are comparing "SobjectType" of the ParentId(for attachment which is added to account SobjectType will be same as of Account Object) and the "Account" to check whether the attachment is added to account.

By using the above code as base you can do many more things like rolling up number of Attachments to its parent object, Some custom validation using trigger on attachment etc.

Sunday, October 6, 2013

Well this one of the most required iteration of my Visualforce Autocomplete Component, which worked well with small data set but starts showing sluggishness with large number of records. After doing some debugging and I felt like the underlying architecture is the problem and needs to be rewritten. And finally while searching for a good JQuery component I came across the Select2 project which looked promising but was a lil but different and was more to do with Picklists. Hmm well gradually I started going deeper into the API and guess what ? I found a way to make it to a Autocomplete component!.

The newer component looked pretty much good and was way faster than the earlier Jquery Autocomplete that I used in V1.

AutocompleteV2 in Action

Summing up few features of the V2

  • The component now uses Select2 instead of Jquery Autocomplete
  • Much more faster than earlier version and can handle large dataset
  • Complete new UI
  • The component no more depends on the Jquery from the package and can use jquery from Visualforce page(if already referred in page).
  •  Configurable : The search field can be configured to search fields other than "Name" field. Even the value that is returned to controller can be configured return fields other than record Id.
How to Use ?

<c:AutoCompleteV2 allowClear="true" importJquery="true" labelField="Name" SObject="Account" valueField="Id" targetField="{!targetField}" style="width:200px"/>  

  • labelField : The field which is displayed in the component  and against which matching is done with the entered text. In above code snippet Name is displayed in the autocomplete component.
  • valueField : The field value which is passed back to targetfield when a record is selected.
  • targetField : The field from controller where the selected values is set.
  • SObject : The sobject Api Name against which matching/query is done.
  • importJquery : Assign false if you dont want to jquery files from package.
  • syncManualEntry : Allow manual entry of data from autocomplete component. So if a matching value is not found for the entered query string, on page submit the same value will be sibmitted to targefield
  • allowClear : Set true to give user a option to clear existing value. If this value is set true user will see a small cross Icon to clear the existing value from the field
In addition to this a Visualforce Page "AutocompleteV2Demo" is included in package. This page searches through different accounts that are available in the Org. Have a look at the same for syntax and implementation



Where can this be used ?
  • It can be used for replacing Lookups and Master Detail fields. Instead of pressing the lookup button user can directly type in the autocomplete field to search and link the right record.
  • It can not only refer Lookups and Master Details but also can search through any object. It can be used as replacement for picklist. Create a object to store the Values available in picklist and use the V2 component to refer the same.(Recommended only when you have large number picklist values to show)
  • Can be used in quick search pages to search for records and swiftly navigate to the record once user submits