+ Start a Discussion
StevevStevev 

Trigger Update action if picklist value changes

I have an object Foo that stores the date a picklist status is changed. Right now it changes on any object update (in other words it reflect the "last modified" date of the object) but I want to narrow the condition to only write the date if the specific picklist changes value to enable me to generate formula results based on the length of time in a specific status.

 

I tried using PRIORVALUE to detect that the picklist changed but since it is a picklist it won't compile.

 

Has anyone got an example where a trigger performs an action based a change to a picklist value on an object? Any ideas?

 

 

trigger FooStatusChange on Foo__c (before update) {
  
  if (Trigger.isUpdate) {
      for (Foo__c a: Trigger.new) {
        a.date_status_changed__c = System.today();
        }
  }

}

 

 

ahab1372ahab1372

you can compare to the old value by using the oldMap

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_triggers_context_variables.htm

 

 

if( a.picklist__c != trigger.oldMap.get(a.id).picklist__c)
{
   your code
}

 

 

 

Pinnacle-AaronPinnacle-Aaron

Assuming Foo__c.date_status_changed__c is a DATETIME field, another option would be:

 

1. Create a Workflow Rule on Foo__c object

2. Set Evaluation Criteria to "Every time a record is created or edited"

2. Change Rule Criteria to "formula evaluates to true"

3. Enter the following formula:

 

ISCHANGED ( Foo__c.picklist__c )

 

4. Under Immediate Workflow Actions, create a Field Update for Foo__c.date_status_changed__c

5. Enter the following formula to update the field:

 

NOW()

 

6. Save and done