Trigger to find Case assigned date and time in Salesforce

1. Create a field on Case object with Case_Assigned_Date_Time__c API name.

2. Create a trigger with below code:

trigger CaseTrigger on Case (before insert, before update) {
    for(Case c : trigger.new) {
        if(trigger.isInsert) {
            if(c.Status == 'Assigned') {
                c.Case_Assigned_Date_Time__c = System.now();
            }
        } else if(trigger.isUpdate) {
            if(c.Status == 'Assigned' && c.Case_Assigned_Date_Time__c == null) {
                c.Case_Assigned_Date_Time__c = System.now();
            }
        }
    }
}

Note:

Make sure that Case_Assigned_Date_Time__c is read only on the page layout.

Cheers!!!

No comments:

Post a Comment