Search Here

Whenever a case is created with the origin as email then set the status as new and Priority as Medium trigger in Salesforce

 To implement the requirement of setting the Status to New and Priority to Medium whenever a Case is created with the origin as Email, you can write an Apex Trigger in Salesforce. Here's how you can do it:


trigger SetCaseFieldsOnEmailOrigin on Case (before insert) {

    for (Case cs : Trigger.new) {

        if (cs.Origin == 'Email') {

            cs.Status = 'New';

            cs.Priority = 'Medium';

        }

    }

}

Key Points of the Trigger

  1. Trigger Type:

    • The trigger is defined for the before insert event because we need to modify the values of the Status and Priority fields before the record is saved to the database.
  2. Condition:

    • The if statement checks if the Origin of the case is Email.
  3. Field Updates:

    • The Status and Priority fields are updated directly on the Case records in the Trigger.new list.

Best Practices

  1. Bulkification:

    • The trigger is inherently bulkified as it iterates over the Trigger.new list to handle multiple case records.
  2. Testing:

    • Write test classes to cover the functionality of this trigger.
@isTest
private class SetCaseFieldsOnEmailOriginTest {
    @isTest
    static void testSetCaseFieldsOnEmailOrigin() {
        // Create a case with origin as 'Email'
        Case testCase = new Case(
            Origin = 'Email',
            Subject = 'Test Case',
            Description = 'Testing email origin trigger'
        );
        insert testCase;

        // Query the inserted case
        Case insertedCase = [SELECT Status, Priority FROM Case WHERE Id = :testCase.Id];
        
        // Assert the Status and Priority
        System.assertEquals('New', insertedCase.Status, 'Status should be set to New');
        System.assertEquals('Medium', insertedCase.Priority, 'Priority should be set to Medium');
    }
}


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.