Create a trigger to update related records #inSalesforce

Create a trigger to update related records #inSalesforce


Create a trigger on contact, if the contact's email id is present then the related Account's Sample Description field is updated

trigger updateContact on Contact(after insert, after update){
    List<Id> accountsTOUpdate = new List<Id>();
    for(Contact con : Trigger.new){
        if(con.email != null){
            accountsTOUpdate.add(con.AccountId);
        }
    }
    if(!accountsTOUpdate.isEmpty()){
        List<Account> accountsTOUpdate = [Select id, sampleDescription__c from Account where id in :accountsTOUpdate];
        for(Account acct:accountsTOUpdate){
            acct.sampleDescription__c = 'Contacted via Email';
        }
        update accountsTOUpdate;
    }
}

Comments