Reply
Regular Contributor
Irish@acc
Posts: 60
0

Trigger to send email alert on creation of child object to the parent object user field

Hi,

I have written one trigger which sent email notification to the user who's in parent object...

this is my code:

 

Trigger mailSend on Quality_of_Delivery__c(after insert){

List<Project__c> lstPrj = new List<Project__c>();//parent obj
set<Id> setId=new set<Id>();
lstprj=[select id,Dev_Specialist__c from Project__c];

for(Project__c proj : lstprj){

setId.add(proj.Dev_Lead_User__c);//I'm sending the email to the Dev_Lead_User__c and this guy is in parent object"Project__c"

}
//List<User> lstuser=[select Id,email from User where ID In:setId];

 

for(Quality_of_Delivery__c Qod : Trigger.new)

{

        EmailTemplate et=[Select id from EmailTemplate where name=:'Update QOD Fields'];

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        string[] toAddress = new string[]{}; 

        mail.setTargetObjectId(Qod.Project__r.Dev_Lead_User__r.ID);

        toAddress.add(Qod.Project__r.Dev_Lead_User__r.email); 

        system.debug('toAddress :'+toAddress);

        mail.setSaveAsActivity(false);

        mail.setToAddresses(toAddress);

        //System.debug(  mail.setTargetObjectId);

         mail.setTemplateId(et.id);    

       mail.setToAddresses(toAddress);       

        

        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});

}

 

}  

 

need help on this since this is not working

Super Contributor
Vinit_Kumar
Posts: 645
0

Re: Trigger to send email alert on creation of child object to the parent object user field

Hi Irish,

 

Please be specific as what is not working here.

Don't forget to give KUDOS if post helped you.

If this answers your query,please mark this as solution so that it would be useful for others.


Regular Contributor
Irish@acc
Posts: 60
0

Re: Trigger to send email alert on creation of child object to the parent object user field

I'm not able to send the email to the Dev_Lead_user__c

Super Contributor
Vinit_Kumar
Posts: 645
0

Re: Trigger to send email alert on creation of child object to the parent object user field

Irish,

 

the way you are populating toAddress that does not look good to me.You need to query the field using SOQL and then populate the toAddress to it.

Don't forget to give KUDOS if post helped you.

If this answers your query,please mark this as solution so that it would be useful for others.