+ Start a Discussion
Akshay ShrivastavaAkshay Shrivastava 

i want to send email to grandparent if child field condition is true

Object “Y” => Detail to “Contact” MD Relationship
Object “Z” => Detail to “Y” Lookup
if amount field on “Z” is more than 10000 send an email to its grandparent Contact.
Best Answer chosen by Akshay Shrivastava
Malika Pathak 9Malika Pathak 9

Hi Akshay Shrivastava ,

Please find the solution. "i want to send email to grandparent if child field condition is true"

You can write trigger Like this to send email from child to grandparent.

trigger sendEmail on Z__c (After insert,After Update) {
    
    if(trigger.isAfter){
        if(trigger.isInsert || trigger.isUpdate){
            system.debug('Inside');
            set<Id> zSetId=new set<Id>();
            for(Z__c obj:trigger.new){
                if(obj.Amount__c>10000){
                    zSetId.add(obj.id);
                }
            }
            List<Z__c> childList=new List<Z__c>();
            childList=[select id,Amount__c,Y__r.Name,Y__r.contact__r.Email from Z__c where id in: zSetId];
            system.debug('childList:: '+childList[0].Y__r.contact__r.Email);
            for(Z__c zObj: childList){
                if(zObj.Y__r.contact__r.Email !=null){
                    EmailManagers.sendMessage(zObj.Y__r.contact__r.Email,'Amount is greater than 10000','Hi ,Your amount is grater than 10000');
                }
            }
        }
    }    
}


Please let me know it is working or not. If you find this solution is helpful for you Please mark Best Answer so that other people would take reference from it.

Thanks