You need to sign in to do that
Don't have an account?

Email to all primary contact using batch
Hello Experts,
I have to use batch class to send email to primary_contact about the sum of all opportunity amount assosciated with that contact.
I have written batch class that is sending mail to given email id.
How to send mail to email id of all primary contact assosciated with that opportunity.
Any help will be appreciated.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
global class OpportunityAggregate implements Database.Batchable<SObject>, Database.Stateful {
Map<Contact, Map<String, Double>> mapConOpps = new Map<Contact, Map<String, Double>>();
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([
SELECT
Id,
(SELECT Id, Name FROM Contacts WHERE Primary__c=true),
(SELECT Id, Name, Amount FROM Opportunities WHERE StageName='Closed Won')
FROM
Account
WHERE
Id
IN
(SELECT AccountId FROM Contact WHERE Primary__c=true)]);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Account acc : scope) {
Map<String, Double> mapOppAmo = new Map<String, Double>();
for(Opportunity objOpp : acc.Opportunities) {
mapOppAmo.put(objOpp.Name, objOpp.Amount);
}
mapConOpps.put(acc.Contacts, mapOppAmo);
}
}
global void finish(Database.BatchableContext BC) {
Map<Contact, Double> mapCon_Amount = new Map<Contact, Double>();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<String> sendTo = new List<String>();
sendTo.add('ayish@gmail.com');
mail.setToAddresses(sendTo);
mail.setReplyTo('hello@world.com');
mail.setSenderDisplayName('Apex batches');
mail.setSubject('URGENT BUSINESS PROPOSAL');
String body = '';
for(Contact con : mapConOpps.keySet()) {
Double sumAmount = 0;
body+='Contact : ' + con.Name + '<br>------------------------------------<br>';
for(String opp : mapConOpps.get(con).keySet()) {
body+='Opportunity : ' + opp + ' and amount = ' + mapConOpps.get(con).get(opp) +
'<br>';
sumAmount+=mapConOpps.get(con).get(opp);
}
body+='The aggregate opportunity amount = ' + sumAmount + '<br><br>';
}
mail.setHtmlBody(body);
mails.add(mail);
Messaging.sendEmail(mails);
}
}
I have to use batch class to send email to primary_contact about the sum of all opportunity amount assosciated with that contact.
I have written batch class that is sending mail to given email id.
How to send mail to email id of all primary contact assosciated with that opportunity.
Any help will be appreciated.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
global class OpportunityAggregate implements Database.Batchable<SObject>, Database.Stateful {
Map<Contact, Map<String, Double>> mapConOpps = new Map<Contact, Map<String, Double>>();
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([
SELECT
Id,
(SELECT Id, Name FROM Contacts WHERE Primary__c=true),
(SELECT Id, Name, Amount FROM Opportunities WHERE StageName='Closed Won')
FROM
Account
WHERE
Id
IN
(SELECT AccountId FROM Contact WHERE Primary__c=true)]);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Account acc : scope) {
Map<String, Double> mapOppAmo = new Map<String, Double>();
for(Opportunity objOpp : acc.Opportunities) {
mapOppAmo.put(objOpp.Name, objOpp.Amount);
}
mapConOpps.put(acc.Contacts, mapOppAmo);
}
}
global void finish(Database.BatchableContext BC) {
Map<Contact, Double> mapCon_Amount = new Map<Contact, Double>();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<String> sendTo = new List<String>();
sendTo.add('ayish@gmail.com');
mail.setToAddresses(sendTo);
mail.setReplyTo('hello@world.com');
mail.setSenderDisplayName('Apex batches');
mail.setSubject('URGENT BUSINESS PROPOSAL');
String body = '';
for(Contact con : mapConOpps.keySet()) {
Double sumAmount = 0;
body+='Contact : ' + con.Name + '<br>------------------------------------<br>';
for(String opp : mapConOpps.get(con).keySet()) {
body+='Opportunity : ' + opp + ' and amount = ' + mapConOpps.get(con).get(opp) +
'<br>';
sumAmount+=mapConOpps.get(con).get(opp);
}
body+='The aggregate opportunity amount = ' + sumAmount + '<br><br>';
}
mail.setHtmlBody(body);
mails.add(mail);
Messaging.sendEmail(mails);
}
}
Try this