Increase size of an image formula field

formulaformula-fieldimage-formula

enter image description here

Hi all I have written a formula field Flagstatus which updates each time a choose a picklist value from the picklist field flag.The red flag is displayed when I choose red in the picklist and so on..
Formula field:

IMAGE(
CASE( Flags__c ,
"Green", "/img/samples/flag_green.gif",
"Yellow", "/img/samples/flag_yellow.gif",
"Red", "/img/samples/flag_red.gif",
"/s.gif"),
"status color")

My requirement is to somehow increase the size of the flag.When I searched I saw a method where I can provide the dimensions for the flag and I modified..
Modified formula:

IMAGE(
CASE( Flags__c ,
("Green", "/img/samples/flag_green.gif","green",30,30)
("Yellow", "/img/samples/flag_yellow.gif","red", 30, 30)
("Red", "/img/samples/flag_red.gif","yellow", 30, 30)
("/s.gif"),
"status color"))

BUt is not working.Syntax issue..

enter image description here

enter image description here

CASE(
Flags__c,

"Green",
IMAGE(
"/img/samples/flag_green.gif",
"green",
40,
40
),

"Yellow",
IMAGE(
"/img/samples/flag_yellow.gif",
"yellow",
40,
40
),

"Red",
IMAGE(
"img/samples/flag_red.gif",
"red",
40,
40
),
IMAGE("/s.gif", "status color")
)

Best Answer

Indenting and formatting your formulas is extremely helpful in finding syntax mistakes or cross-checking with the expected format. In this case, you'll note that CASE expects

CASE(expression,value1, result1, value2, result2,..., else_result)

and IMAGE expects

IMAGE(image_url, alternate_text, height, width)

Likewise, what you're trying to accomplish with the above formula could be put in the following terms to match with the parameters above

  • For a given field (expression), depending on its data (value) - return a format (result) to be used within IMAGE

If I format your existing formula and add a comment to match with the expected format above, you can see your parenthesis are bunching the value used in CASE together with the format you'd want to return for IMAGE.

IMAGE(
    CASE( 
        Flags__c,                                                /* expression */
        
        ("Green", "/img/samples/flag_green.gif","green",30,30)   /* value1 */
        /* Missing comma from above expression */
        
        /* Wrong alternate text for color */
        ("Yellow", "/img/samples/flag_yellow.gif","red", 30, 30) /* result1 */

...

You'll need to separate them out like so

IMAGE(
    CASE( 
        Flags__c ,                                         /* expression */
       
        "Green",                                           /* value 1 */
        ("/img/samples/flag_green.gif","green",30,30),     /* result 1 */

        "Yellow",                                          /* value 2 */
        ("/img/samples/flag_yellow.gif","yellow", 30, 30), /* result 2 */

        "Red",                                             /* value 3 */
        ("/img/samples/flag_red.gif","red", 30, 30),       /* result 3 */

        ("/s.gif", "status color")                         /* else result */
    )
)

Note: you had mismatched labeling your image colors (red/yellow) as well

Testing quickly the above, you'll still get errors. If you look closer at it, you'll see that the value being returned to IMAGE() from CASE is ("url", "alternatetext",30,30). However, that's treated as all one input/parameter (string) in IMAGE instead of 4 separate parameters.

You'd have to duplicate your CASE for each parameter to achieve it the way you were doing

IMAGE(
    CASE(
        Flags__c,

        "Green",
        "img/samples/flag_green.gif

        ..rest of case for just url
    ),

    CASE(
        Flags__c

        "Green",
        "green",

        ..rest of case for just alternate text

...and so on

As you can see, it's not very efficient. Instead, you can flip your CASE and IMAGE so you can return the IMAGE() with all its properties set

CASE(
    Flags__c,                               /* expression */

    "Green",                                /* value 1 */
        IMAGE(                              /* result 1 */
            "/img/samples/flag_green.gif",
            "green",
            30,
            30
        ),

    "Red",
        IMAGE(
            "img/samples/flag_red.gif",
            "red",
            30,
            30
        ),

    ...rest of your colors/images

)
Related Topic