function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Poorna DeveloperPoorna Developer 

Apex Trigger to track the Picklist value.

Hi Everyone,

I have a object named  'Application__c' and Picklist field named 'Stage__c'['Red', 'Blue','Pink','Black']

Whenever the 'Stage__c'  picklist value is changed I need to track that field value.
ie. When picklist value changed from Red -> Blue I need to track this field in my trigger. Is this possible?
Any help.
Thankyou all.
Best Answer chosen by Poorna Developer
Suraj Tripathi 47Suraj Tripathi 47
Hi Poorna Developer,

You can try this approch to solve your problem.
trigger TestTrigger on Application__c (before update) {
    Map<Id, Application__c> newMap = Trigger.newMap;
    Map<Id, Application__c> oldMap = Trigger.oldMap;
    for(Id key : newMap.keySet()){
        If(oldMap.get(key).Stage__c.equals(newMap.get(key).Stage__c)){
            //logic for new & old is same.
        }else{
            //logic for new & old is  not same.
        }
    }
    
}
Trigger.new : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

Trigger.old : Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.


If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Poorna Developer,

You can try this approch to solve your problem.
trigger TestTrigger on Application__c (before update) {
    Map<Id, Application__c> newMap = Trigger.newMap;
    Map<Id, Application__c> oldMap = Trigger.oldMap;
    for(Id key : newMap.keySet()){
        If(oldMap.get(key).Stage__c.equals(newMap.get(key).Stage__c)){
            //logic for new & old is same.
        }else{
            //logic for new & old is  not same.
        }
    }
    
}
Trigger.new : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

Trigger.old : Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.


If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.
This was selected as the best answer
ShivankurShivankur (Salesforce Developers) 
Hi Poorna,

Its always better to use configuration stuff instead of code, if its really not necessary to perform any additional logic.

To do it via configuration:
1. Create a Workflow Rule on Application__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 ( Application__c.Stage__c )
 
4. Under Immediate Workflow Actions, you can define to perform any of the available actions such as Email Alert, Field Update, Outbound message, create a task.

To do it via Trigger to track changes to the picklist field:
trigger TriggerOnApplication on Application__c (before insert,before update){    
    for (Application__c obj : Trigger.new){
        if (obj.Stage__c.equals('Blue')){
            /code to perform additional logic since the field change is tracked           
        }           
    }
}

While using triggers,you can make use of before insert,before update context or Trigger Context Variables.

Reference: 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.