function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DemiraliDemirali 

Send email to contacts in related list of an object

Hello to all. 

I have an object called ABC. 

This ABC has one child called BOOK__c

Also there is another object Pencil__c which has a lookup to ABC and has a related list of contacts. 


Whenever BOOK__c record is checked completed (BOOK.Completed__c = True), I want to send an email to all Contacts in related list of Pencil__c of BOOK__c's parent ABC. 

How can I figure this out?

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

 

Hi,

 

Here is the sample code for your requirement. here at Last i founded set<Id> sContactID to which you have to send email.

 

List<Book__c> lstBook = [select id,ABC__c from book__c where completed__c = true];
set<Id> sABCId = new set<Id>();
for(Book__c bk: lstBook){
    if(bk.ABC__c != null){
        sABCId.add(bk.ABC__c);
    }
}

List<ABC__c> lstABC = [select id,(select id from Pencils__r) from ABC__c where id IN: sABCId];
set<Id> sPencilId = new set<Id>();
for(ABC__c ab: lstABC){    
    for(Pencil__c pn: ab.Pencils__r){        
        sPencilId.add(pn.id);        
    }
}

List<Pencil__c> lstPencil = [select id,(select id from contacts__r) from Pencil__c where id IN: sPencilId];
set<Id> sContactID = new set<ID>();
for(Pencil__c pnc: lstPencil){    
    for(Contact con: pnc.contacts__r){        
            sContactID.add(con.id);
        }
    }
}

 

 write logic for sendEmail for sContactID

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator

All Answers

hitesh90hitesh90

 

Hi,

 

Here is the sample code for your requirement. here at Last i founded set<Id> sContactID to which you have to send email.

 

List<Book__c> lstBook = [select id,ABC__c from book__c where completed__c = true];
set<Id> sABCId = new set<Id>();
for(Book__c bk: lstBook){
    if(bk.ABC__c != null){
        sABCId.add(bk.ABC__c);
    }
}

List<ABC__c> lstABC = [select id,(select id from Pencils__r) from ABC__c where id IN: sABCId];
set<Id> sPencilId = new set<Id>();
for(ABC__c ab: lstABC){    
    for(Pencil__c pn: ab.Pencils__r){        
        sPencilId.add(pn.id);        
    }
}

List<Pencil__c> lstPencil = [select id,(select id from contacts__r) from Pencil__c where id IN: sPencilId];
set<Id> sContactID = new set<ID>();
for(Pencil__c pnc: lstPencil){    
    for(Contact con: pnc.contacts__r){        
            sContactID.add(con.id);
        }
    }
}

 

 write logic for sendEmail for sContactID

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator

This was selected as the best answer
DemiraliDemirali

Thnak you for your quick reply. 

 

I don't have a knowledge of deploying codes to SF environment. Can you help me out? If you have time, we may also have a conversation over skype? 

 

Thanks, 

Simon WhightSimon Whight
Useful looking post, hoping to use this to get Salesforce to post to all Contacts on an Account when Contract Start Date passes a milestone.