+ Start a Discussion
Muhammad Jawwad 16Muhammad Jawwad 16 

Batch Apex - how to populate convertedopportunityID and sending opportunity records below leads

global class LoanOfficerBatch implements Database.Batchable<sObject> {
    public String query = 'SELECT Loan_Officer_1a__c,Loan_Officer_1a__r.Email, ConvertedOpportunityId, Name, Phone, Starting_Credit_Score__c, ' 
                          +  'Status, Enrolled_On__c, Est_Re_Pull_Date__c, Realtor_Name__c ' 
                          +   ' FROM Lead'; /*JMA:: add  ConvertedOpportunityId field*/ 
    public EmailTemplate templateId = [Select Id,HtmlValue,Subject from EmailTemplate where name = 'LoanOfficerRecord' LIMIT 1];

    global Database.QueryLocator start(Database.BatchableContext bc) {

        query += ' WHERE CreatedDate >= LAST_MONTH AND CreatedDate <= THIS_MONTH AND Loan_Officer_1a__c != null';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Lead> allLeads) {
        
        //JMA:: Create a map of <Id, Opportunity>
        Map<Id,List<Opportunity>> OpptyMap = new Map<Id,List<Opportunity>>();
        //JMA:: Query Opportunities by using Lead.ConvertedOpportunityId. You need to first create a set of Id from Lead.ConvertedOpportunityId
        //JMA:: populate the map <Id, Opportunity>
        
        Map<Id,List<Lead>> leadMap = new Map<Id,List<Lead>>();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMEssage>();
        if(allLeads != null && allLeads.size() > 0){
            for(Lead l: allLeads){
                if(!leadMap.containsKey(l.Loan_Officer_1a__c)){
                    leadMap.put(l.Loan_Officer_1a__c, new List<lead>());
                }
                leadMap.get(l.Loan_Officer_1a__c).add(l);
            }
        }
        if(leadMap.keySet().size() > 0){
            Map<Id,Contact> officers = new Map<Id,Contact>([SELECT Id,Email,Name FROM Contact WHERE Id IN: leadMap.keySet()]);
            for(Id i: leadMap.keySet()){
                Contact con = officers.get(i);
                System.debug(con);
                if(String.isnOtBlank(con.Email)){
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(new String[]{con.EMail});
                    mail.setSubject(templateId.Subject);
                    String html = templateId.HtmlValue;
                    html = html.replace('||OfficerName||',con.Name);
                    String leadsTable = '<table cellpadding="3" cellspacing="3" width="100%" align="center" border="1" style="border-collapse:collapse;">'+
                        '<tr style="font-weight:bold;"><td>Name</td><td>Phone</td><td>Starting Credit Score</td><td>Status</td><td>Enrolled On</td><td>Est. Re Pull Date</td><td>Realtor Name</td></tr>';
                    for(Lead l: leadMap.get(i)){
                        //JMA:: for each lead you can get the associated Opportunity from map <Id, Opportunity>
                        leadsTable += '<tr><td>'+l.Name+'</td>'+
                            '<td>'+l.Phone+'</td><td>'+l.Starting_Credit_Score__c+'</td><td>'+l.Status+'</td><td>'+l.Enrolled_On__c+'</td>'+
                            '<td>'+l.Est_Re_Pull_Date__c+'</td><td>'+l.Realtor_Name__c+'</td></tr>';
                    }
                    leadsTable += '</table>';
                    html = html.replace('||Leads||',leadsTable);
                    html = html.replace('null',' ');
                    mail.setHTMLBody(html);
                    mails.add(mail);
                }
            }
        }
        if(mails.size() > 0){
            //Messaging.sendEmail(mails);
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
   
}

 
Best Answer chosen by Muhammad Jawwad 16
mukesh guptamukesh gupta
Hi

Please use this code:- 
 
List<String> convertedOppId = new List<String>();
for(Lead l: allLeads){
	convertedOppId.add(l.ConvertedOpportunityId); 
}
map <Id, Opportunity> oppMap = map <Id, Opportunity>();
for(Opportunity opp : [SELECT Id FROM Opportunity WHERE Id IN: convertedOppId]){
	oppMap.put(opp.id, opp );
    
}

   String oppTable = '<table cellpadding="3" cellspacing="3" width="100%" align="center" border="1" style="border-collapse:collapse;">'+
                        '<tr style="font-weight:bold;"><td>Name</td><td>Phone</td><td>Starting Credit Score</td><td>Status</td><td>Enrolled On</td><td>Est. Re Pull Date</td><td>Realtor Name</td></tr>';
                    for(Id idKey: oppMap.getKeySet()){
                        Opportunity o = oppMap.get(idKey)
                        
                        //JMA:: for each lead you can get the associated Opportunity from map <Id, Opportunity>
                        oppTable += '<tr><td>'+o.Name+'</td>'+
                            '<td>'+o.StageName+'</td></tr>';
                    }
                    oppTable += '</table>';
                    html = html.replace('||Opportunities||',oppTable);
                    html = html.replace('null',' ');
                    mail.setHTMLBody(html);
                    mails.add(mail);
                }

Kindly MARK AS A BEST ANSWER!!if the reply was helpful.


Regards
Mukesh

All Answers

mukesh guptamukesh gupta
Hi Muhammed,

Please use below code :-
 
List<String> convertedOppId = new List<String>();
for(Lead l: allLeads){
	convertedOppId.add(l.ConvertedOpportunityId); 
}

map <Id, Opportunity> opp = map <Id, Opportunity>();
for(Opportunity opp : [SELECT Id FROM Opportunity WHERE Id IN: convertedOppId]){
	opp.put(opp.id, opp )
}
Kindly MARK AS A BEST ANSWER!!if the reply was helpful.


Regards
Mukesh

 
Muhammad Jawwad 16Muhammad Jawwad 16

telling me one thing please 

for(Lead l: leadMap.get(i)){
44                        //JMA:: for each lead you can get the associated Opportunity from map <Id, Opportunity>
45                        leadsTable += '<tr><td>'+l.Name+'</td>'+
46                            '<td>'+l.Phone+'</td><td>'+l.Starting_Credit_Score__c+'</td><td>'+l.Status+'</td><td>'+l.Enrolled_On__c+'</td>'+
47                            '<td>'+l.Est_Re_Pull_Date__c+'</td><td>'+l.Realtor_Name__c+'</td></tr>';
48                    }
49                    leadsTable += '</table>';
50                    html = html.replace('||Leads||',leadsTable);
51                    html = html.replace('null',' ');
52                    mail.setHTMLBody(html);
53                    mails.add(mail);
54                }

how to fetch opportunity in different table?

mukesh guptamukesh gupta
Hi

Please use this code:- 
 
List<String> convertedOppId = new List<String>();
for(Lead l: allLeads){
	convertedOppId.add(l.ConvertedOpportunityId); 
}
map <Id, Opportunity> oppMap = map <Id, Opportunity>();
for(Opportunity opp : [SELECT Id FROM Opportunity WHERE Id IN: convertedOppId]){
	oppMap.put(opp.id, opp );
    
}

   String oppTable = '<table cellpadding="3" cellspacing="3" width="100%" align="center" border="1" style="border-collapse:collapse;">'+
                        '<tr style="font-weight:bold;"><td>Name</td><td>Phone</td><td>Starting Credit Score</td><td>Status</td><td>Enrolled On</td><td>Est. Re Pull Date</td><td>Realtor Name</td></tr>';
                    for(Id idKey: oppMap.getKeySet()){
                        Opportunity o = oppMap.get(idKey)
                        
                        //JMA:: for each lead you can get the associated Opportunity from map <Id, Opportunity>
                        oppTable += '<tr><td>'+o.Name+'</td>'+
                            '<td>'+o.StageName+'</td></tr>';
                    }
                    oppTable += '</table>';
                    html = html.replace('||Opportunities||',oppTable);
                    html = html.replace('null',' ');
                    mail.setHTMLBody(html);
                    mails.add(mail);
                }

Kindly MARK AS A BEST ANSWER!!if the reply was helpful.


Regards
Mukesh
This was selected as the best answer
Muhammad Jawwad 16Muhammad Jawwad 16
Everything else perfect but after execution an error occurred: 
First error: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Name 
mukesh guptamukesh gupta
please add 

Name field in 

for(Opportunity opp : [SELECT Id, Name FROM Opportunity WHERE Id IN: convertedOppId]){ oppMap.put(opp.id, opp ); }

Kindly MARK AS A BEST ANSWER!!if the reply was helpful.


Regards
Mukesh
Muhammad Jawwad 16Muhammad Jawwad 16
thank a lot dear it's working.
Muhammad Jawwad 16Muhammad Jawwad 16
This code works but not getting desired result.How i get asscoiated opportunity through this code and populate it in a table?please help