1

I have the following trigger

trigger caseTrigger on Case (before insert, before update) {
    if(Trigger.isInsert && Trigger.isUpdate){
        UpdateFieldsForApproval updateEmailField = new UpdateFieldsForApproval();
        updateEmailField.updateEmailOnCase(trigger.new);
    }

and I have the following apex class

public class UpdateFieldsForApproval{

    public void updateEmailOnCase(List<Case> newCases){
       List<ID> contractorIds = new List<ID>();
        for(Case obj : newCases)
        {
            contractorIds.add(obj.Vendor__c);
        }
        Map<ID, Vendor__c> caseContractorMap = new Map<ID, Vendor__c>([select Email__c from Vendor__c where ID in :contractorIds]);
        for (Case obj : newCases)
        {
            if (obj.Vendor__c != null)
            {
                obj.Email_Contractor__c = caseContractorMap.get(obj.Vendor__c).Email__c;
                System.debug('In Class--------'+obj.Email_Contractor__c);
            }
         }
    }
}

The Email field of the case which I am trying to update is not getting updated. As far as I know I do not have to use a DML operation for the before trigger. Can anyone tell me where am I going wrong? I am fairly new in Salesforce and any help would be appreciated.

3
  • Trigger.isInsert && Trigger.isUpdate is not needed
    – d_k
    Apr 20, 2017 at 7:56
  • @d_k Perhaps, but I don't personally think it's something worth calling out. If you adhere to 'one trigger per object', then this trigger could later expand to cover other trigger events. Preemptively cordoning off sections of code like this can reduce the number of changes that need to be made when the trigger expands (thus reducing the risk of introducing bugs). I also think it improves readability (ah, this bit of code runs on both insert and update), and can be pretty DRY.
    – Derek F
    Apr 20, 2017 at 13:19
  • @DerekF yeah that works too, thanks for pointing that out!
    – d_k
    Apr 20, 2017 at 13:21

1 Answer 1

2

Issue in trigger code

this should be || not && condition. You trigger will fire on insert or update case

if(Trigger.isInsert || Trigger.isUpdate){
   //rest of your code
}
1
  • @Rimii no worries, happens some time.
    – Ratan Paul
    Apr 20, 2017 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .