I am not sure why you would need a trigger for this. I think workflow rules would work just fine.
We created date/time fields for each of our statuses for reporting on time between each status. You could create the same for each of yours.
After the fields are created, create your workflow rules to capture the change in status. When the status equals the value you want, populate the respective date/time field with Now().
Edit to include code, since I am unsure of how to format the comment to show this.
This is the part of the link in the comment below that shows how to track days, hours, and minutes.
IF(
Diff_c > 0 ,
TEXT( FLOOR( Diff_c ) ) & " days "
& TEXT( FLOOR( MOD( Diff_c * 24, 24 ) ) ) & " hours "
& TEXT( ROUND( MOD( Diff_c * 24 * 60, 60 ), 0 ) ) & " minutes",
""
)
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
)
Best Answer
In addition of already said by sfdcfox I would like to show you an example of the formula field that you should add.
Add a custom field type formula and use this:
IF( ( Now() - CreatedDate )*24 > 18 , IMAGE("/resource/RedFlag", "red" ,30, 30) , IF( ( Now() - CreatedDate )*24 < 15, IMAGE("/resource/BlueFlag", "blue" ,30, 30), IMAGE("/resource/YellowFlag", "yellow" ,30, 30)) )
Key points:
(Now() - CreatedDate) *24
== record's age in hoursIMAGE("/resource/YellowFlag", "yellow" ,30, 30))
== will show the image that setSample: