+ Start a Discussion
Muhammad Jawwad 16Muhammad Jawwad 16 

Getting opportunity data in a table using convertedopportunityId

Please review my code and tell me where i'm wrong to getting data from oppty map and populate into table?
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>
        //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>
         List<String> convertedOppId = new List<String>();
         for(Lead l: allLeads){

         Map<Id, Opportunity> opptymap = new Map<Id, Opportunity>();  
         for(Opportunity o : [SELECT Id FROM Opportunity WHERE Id IN: convertedOppId]){
	      opptymap.put(o.Id, o);
        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){
                    leadMap.put(l.Loan_Officer_1a__c, new List<lead>());
        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);
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(new String[]{con.EMail});
                    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>'+
                    leadsTable += '</table>';
                    String opptyTable = '<table cellpadding="3" cellspacing="3" width="100%" align="center" border="1" style="border-collapse:collapse;">'+
                        '<tr style="font-weight:bold;"><td>Name</td><td>Starting Credit Score</td><td>StageNmae</td><td>Enrolled On</td><td>Est. Re Pull Date</td><td>Realtor Name</td></tr>';
                    for(Opportunity o: opptymap.get(i)){
                        //JMA:: for each lead you can get the associated Opportunity from map <Id, Opportunity>
                        opptyTable += '<tr><td>'+o.Name+'</td>'+
                    leadsTable += '</table>';
                    html = html.replace('||Leads||',leadsTable);
                    html = html.replace('null',' ');
        if(mails.size() > 0){

    global void finish(Database.BatchableContext BC) {


for(Opportunity o: opptymap.get(i)) - at line 64, the variable i will not give you the opportunity I think...

Ensure, opptyMap.get(i) will give you opportunity record,

Thank You,
Rajesh Adiga P.

Muhammad Jawwad 16Muhammad Jawwad 16
i have got an error: Loop must iterate over collection: Opportunity
See, for(Opportunity o: opptymap.get(i)) will give you one Opportunity record, but the salesforce expects a list of Opportunity record.

This should be like for(Opportunity o: opptymap.values())
Muhammad Jawwad 16Muhammad Jawwad 16
error resolved but not getting the results did you review my code?