Monday, August 16, 2010

Using IMAGE function in formula fields

Things that are "visible" are more easy to understand than things that are meant to "read".

In salesforce standard pages, IMAGE function is the answer to above line. You can show images to the user instead of just having TEXT. And your images can be very dynamic, the way you want them to be. This is the very core objective of salesforce IMAGE formula.

Let us take an example of a very simple IMAGE field. Suppose we want to create an IMAGE field which shows a RED flag when your Lead score is less than 2, yellow when score is between 2 and 4 and green when Lead score is 5.

Create a formula field by going through Setup --> Customize -->Lead --> Fields --> Create  New Custom field and the return data type as TEXT.
Select the field type as formula field. in the formula editor type in the following:

IMAGE(IF(Lead_Score__c <= 2, "/img/samples/flag_red.gif", IF(AND(Lead_Score__c > 2,Lead_Score__c <= 4), "/img/samples/flag_yellow.gif",IF(Lead_Score__c = 5, "/img/samples/flag_green.gif"))))

Now you can put this field in your page layout and try changing your Lead Score field (I am assuming you have Lead Score field on your Lead object, else create one) and see the flag changing. The images that you see here are standard icons provided by salesforce. You can get more icons at mgsmith's blog post here.

One more formula field that I used in one of my project implementation, is below. It just illustrates how you can "concatenate" your image formula field the same way you do it with TEXT.

IF(OR(NumberofLocations__c=1,NumberofLocations__c=2,NumberofLocations__c=3,NumberofLocations__c=4),IMAGE("/img/samples/color_green.gif", "green", 15, 49),IMAGE("/img/samples/color_red.gif", "red", 15, 49))
& IMAGE("/s.gif", "green", 15, 1)
& IF(OR(NumberofLocations__c=2,NumberofLocations__c=3,NumberofLocations__c=4),IMAGE("/img/samples/color_green.gif", "green", 15, 49),IMAGE("/img/samples/color_red.gif", "red", 15, 49))
& IMAGE("/s.gif", "green", 15, 1)
& IF(OR(NumberofLocations__c=3,NumberofLocations__c=4),IMAGE("/img/samples/color_green.gif", "green", 15, 49),IMAGE("/img/samples/color_red.gif", "red", 15, 49))
& IMAGE("/s.gif", "green", 15, 1)
& IF(OR(NumberofLocations__c=4),IMAGE("/img/samples/color_green.gif", "green", 15, 49),IMAGE("/img/samples/color_red.gif", "red", 15, 49)).

Here I have taken refrence of a field NumberofLocations__c on say Account object. It displays a series of images concatenated. So this is kind of Progress Bar that you can show to your user.

Did you note the '&', the numbers 15 and 49, and /s.gif location?
 The '&'  is used to concatenate two strings or texts. In salesforce image fields are treated as string so you can 'add' them.
The number 15 is the height of your image and 49 is the width or span.
One very good image provided by salesforce is "blank" image i.e. '/s.gif'. So when you do not want to show any image in your condition, simply put the url of image as '/s.gif'. It shows a blank. In case you do not want to show an image and leave the url part as blank, it shows a red cross instead of blank. Which definetely doesn't look good.

You can find more about image formulas at salesforce documentation here.

Hope this was useful.

Happy Clouding :)

No comments:

Post a Comment