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
ahmadkhanahmadkhan 

hi guys need help stuck from last 4 days.:(

i have two objects which have parent child relationship

 

i have a field person Responsible in parent object which is a lookup field to user

and child object i have a list..

 

i have to send email to all the person responible records of child object that are related to parent object. and i am stuck in logic

Scenario:

 

parent records with person responsible = Rameez whose email is rameez.sheraz@gmail.com


Person Responsible RAMEEZ have 3 child records a,b,c

i have to send email to rameez some information of child records lets say Name of each record.

Please help its very very very urgent.:/ i am just a beginner in apex

souvik9086souvik9086

Can you please make sure that in Parent object is there only one record with person responsible as Rameez or list and also what is the lookup field name in child object?

 

thanks

ahmadkhanahmadkhan
hey thank for a quick reply..

Yes there will be only one record....
no lookup field in child just a parentID which has a masterdeial relationship with parent object
souvik9086souvik9086

Ok I'm trying to give you the solution.

 

Thanks

souvik9086souvik9086

Try this

 

public class SendEmail{
ParentObject__c pObj = new ParentObject__c();
String body = 'The Child Object Names are';
pObj = [SELECT ID,Name,PersonResponsible__r.Email,(SELECT ID,Name FROM ChildObjects__r) FROM ParentObject__c WHERE PersonResponsible__c = 'Rameez'];
for(ChildObject__c cObj : pObj.ChildObjects__r){
if(body == 'The Child Object Names are'){
body = cObj.name;
}
else{
body = ','+cObj.name;
}
}
String email = pObj.PersonResponsible__r.Email;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;
String[] toAddresses = new String[] {email} ;
mail.setToAddresses(toAddresses) ;
mail.setSubject('Your subject');
mail.setHtmlBody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

ahmadkhanahmadkhan
Thanks Souvik it helped me alot!!!

another question what if i want to send an email containing three type of records from child object.

1. records for current month
2. records for next month
3. records for after 2 months

so email sent to RAMEEZ will looks like this

records for current month a,b,c
records for next month d
records for after 2 months ef
souvik9086souvik9086

See your post. I gave answer to that previously.

http://boards.developerforce.com/t5/Apex-Code-Development/NEED-URGENT-HELP-PLEASE/m-p/655647#M121617

 

To fit this in you email body, I'm trying to incorporate that here.

 

Thanks

souvik9086souvik9086

Try something like this.

 

public class SendEmail{
ParentObject__c pObj = new ParentObject__c();
List<ChildObject__c> cObjListThisMonth = new list<ChildObject__c>();
List<ChildObject__c> cObjListNextMonth = new list<ChildObject__c>();
List<ChildObject__c> cObjListLaterMonths = new list<ChildObject__c>();
String body = 'The records for current month are';
pObj = [SELECT ID,Name,PersonResponsible__r.Email,(SELECT ID,Name FROM ChildObjects__r) FROM ParentObject__c WHERE PersonResponsible__c = 'Rameez'];
cObjListThisMonth = [SELECT ID,Name FROM ChildObject__c WHERE LookUpField__c = :pObj.id AND CreatedDate = THIS_MONTH];
cObjListNextMonth = [SELECT ID,Name FROM ChildObject__c WHERE LookUpField__c = :pObj.id AND CreatedDate = THIS_MONTH];
Datetime myDate = System.Today();
Integer mon = myDate.addmonths(2).month();
cObjListLaterMonths = [SELECT ID,Name FROM ChildObject__c WHERE LookUpField__c = :pObj.id AND CALENDAR_MONTH(CreatedDate) = :mon];

for(ChildObject__c cObj : cObjListThisMonth){
if(body == 'The records for current month are'){
body = cObj.name;
}
else{
body += ','+cObj.name;
}
}
body += '\n The records for next month are';
for(ChildObject__c cObj : cObjListNextMonth){
body += ','+cObj.name;
}
body += '\n The records for later months are';
for(ChildObject__c cObj : cObjListLaterMonths){
body += ','+cObj.name;
}
String email = pObj.PersonResponsible__r.Email;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;
String[] toAddresses = new String[] {email} ;
mail.setToAddresses(toAddresses) ;
mail.setSubject('Your subject');
mail.setHtmlBody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks