Friday, March 30, 2012

Criteria bases Salesforce Standard Action Overriding


Sometime it is required to override the standard salesforce action for tabs or view pages with a custom visualforce page and that too for some of the profiles OR if the record meets certain criteria, but at the same time we want to keep the standard page for rest of the profiles or records which do not meet the criteria.

Let's take an example which would help you to understand this. Suppose we need to show user a custom visualforce page with some details on it when Account tab is clicked and the only exception is System Administrator who should rather see the standard tab page.

Though the logic can be defined in an apex class which will redirect user to the desired page. But from what I know from my experience is we should avoid writing the code untill it is possible to have an effective solution using configuration only.

Here is the simple solution:

  1. Write the page logic like the one given below :
    <apex:page action="{!if($Profile.Name !='System Administrator', null, URLFOR($Action.Account.Tab, $ObjectType.Account,  null, true))}" standardController="Account" recordSetVar="accounts" tabStyle="Account">
        This page replaces your Account home page for   all users except Administrators.
    </apex:page>
    
  2. Change the security setting for the vf page so that to allow all the profiles to use it.
  3. Override Account Tab with "<OverrideExceptFromAdmin>" (Name of your visualforce page) page.
and you are done with it :)

If you want to have an idea about the function URLFOR and each parameter of it, please go through the below salesforce documentation link:

Wednesday, March 28, 2012

Create PDF Attachment Dynamically using Apex Trigger

Ever wondered how we could create a PDF dynamically and store it as an attachment using a trigger in Salesforce. I know, this is quite easy if it would have been the case of New/Edit button override or if it would have been the case of a new custom visualforce page. But, to achieve this using a trigger is a little bit tricky. This is  because getContent() and getContentasPDF() methods are not available for use in a trigger or @future method due to the salesforce governor limits. So is the case with Batch and scheduled apex class.

After a bit research I was able to get a couple of workarounds to achieve the above mentioned requirement:

1. Using the webservice method: 
  1. Expose a method as a webservice, which dynamically creates & stores a PDF as an attachment record.
  2. Generate the wsdl  for this webservice and consume it in the same Org.
  3. Now call the webservice method in the generated stub class from the trigger via @future method by passing the sessionID.
  4. This will generate the desired pdf.
2. You can also try generating the PDF using Email Service class. Process is as follow:  
  1. Create an Email Service class as well as an Visualforce page template.
  2. Send an inbound email to Salesforce through workflow with parent Salesforce id in subject line.
  3. Email service class will render the Visualforce page and PDF can be saved in attachment object.
Hope the solutions will help you. Please write to me if you have any suggestion or a better solution on the same.

Sunday, March 25, 2012

Dynamic Apex to retrieve Picklist Values

Today I came across a requirement where I need to get all the picklist values for a field and to show them on a visualforce. Thogh it is not a very complex requirement and I have implemented it couple of times before as well, I spent some time on it because I implemented it this time also.

I could have saved my time if I had a reusable code for this. Thus I finally decided to develope a reusable method for the same and to post it here on the blog so that the other techies :) can use it without wasting their time as I did.

Below is the method i wrote:


// method to get the available picklist values for the Sobject Field
public static List<selectoption> getPicklistValues(String objectName, String picklistField, Boolean addNoneVal) {

 List<selectoption> options = new List<selectoption>();

 // Add none value if defined in parameter
 if(addNoneVal) {
     options.add(new SelectOption('', '--None--'));
 }

 // add the picklist values to the list of select options
 for(Schema.PicklistEntry ple: Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(picklistField).getDescribe().getPicklistValues()) {
     options.add(new SelectOption(ple.getValue(), ple.getLabel()));
 }

 // return the prepared list.
 return options;
}

You just need to add this in your apex class and call it like below:
public List<selectoption> getAccountTypePicklistValues() {
    return getPicklistValues('Account', 'Type', true);
}

Now you can use this getter method in your visaulforce page which will look like below when executed:
P.S. : I have used the "Type" field from "Account" object for the example. You can change it per your requirements.

Monday, May 23, 2011

"Syntax Highlighter" to add code snippet in blogger

Hello Everyone.

This is my first post on this blog. I am writing this blog to provide some quick salesforce developement recipes which will help others.

Before posting any content on the blog, first question for me was, how would I post a code snippet which help other people to understand the logic and implementation. After a lot of research I found "syntax highlighter" which is pretty easy to use. Just use the step mentioned below:

  1. Go to http://syntaxhighlighter.googlecode.com/svn/trunk/Styles/SyntaxHighlighter.css, then perform a "select all" and "copy". The css information is now in the clipboard.
  2. Paste the css information at the end of the css section of your blogger html template (i.e., after <b:skin><!--[CDATA[/* and before ]]--></b:skin>).).
  3. Before the </head> tag, paste the following:
       













  4. Before the </body> tag, insert the following:

       



  5. Use the "Preview" button to make sure your website is correct, then click "Save Template".

  6. When composing a blog entry that contains source code, click the "Edit Html" tab and put your source code (with html-escaped characters) between these tags:


    <pre name="code" class="cpp">
    Your html-escaped code goes here...
    </pre>
  7. Substitute "cpp" with whatever language you're using (full list). (Choices: cpp, c, c++, c#, c-sharp, csharp, css, delphi, pascal, java, js, jscript, javascript, php, py, python, rb, ruby, rails, ror, sql, vb, vb.net, xml, html, xhtml, xslt)
See full documentation here for Syntaxhighlighter.

For performing the HTML escaping, you can get a good list of tools by searching for 'html esaper' or a similar term. Here's the one I used while writing this post.

Let me know how it works!