0

don't know why I'm getting null when I query in query editor all the values are coming!

my trigger handle code

public static void afterUpdate(List<Opportunity> newOpportunitiesList, Map<Id, Opportunity> oldOpportunitiesMap){

    Map<Id,opportunity> eligibleOpportunities = new Map<Id, Opportunity>();
    Map<Id,Opportunity> emailToOpportunities = new Map<Id,Opportunity>();
    for(Opportunity Opp: newOpportunitiesList){
        if(Opp.Probability >= OpportunityTriggerHandler.ELIGIBLE_PROBABILITY && Opp.Stagename != oldOpportunitiesMap.get(opp.Id).Stagename){
             eligibleOpportunities.put(opp.Id, Opp);
             emailToOpportunities.put(opp.Id, Opp);
        }
    }
    if(!eligibleOpportunities.isEmpty()){
        for(Job__c job:[SELECT Id,Opportunity__c FROM Job__c WHERE Opportunity__c=:eligibleOpportunities.keyset()]){
            if(eligibleOpportunities.containskey(job.Opportunity__c)){
                eligibleOpportunities.remove(job.Opportunity__c);
            }   
        }
        if(!eligibleOpportunities.isEmpty()){
            //OpportunityTriggerHandler.createJobRecords(eligibleOpportunities.values()); 
        }
          **OpportunityTriggerHandler.employer(emailToOpportunities)**;
    }
}

here is my my method

 public static void employer(Map<Id, Opportunity> opportunities){

    Map<String,String> nameWithEmail = new Map<String, String>();
    for(Employers__c us: [SELECT id,Name,Email__c FROm Employers__c where Email__C != null]){
        nameWithEmail.put(us.Name,us.Email__c);  
    }

    for(Opportunity opp:[SELECT Id,Name,Stagename,Account.Name,Amount,Description, Probability,Owner.Name,(SELECT Id,Name,Email__C FROM Employers__r) FROM Opportunity WHERE Id=:opportunities.keyset()]){

        list<String> toEmails = new list<String>();



        if(nameWithEmail.containskey(opp.Owner.name)){
            toEmails.add(nameWithEmail.get(opp.Owner.name));    
        }

       for(Employers__c Emp : opp.Employers__r){
            if(nameWithEmail != null){
                toEmails.add(nameWithEmail.get(Emp.Email__C));  
            }
        }
        String Recordlink = URL.getSalesforceBaseUrl().toExternalForm()+'/'+opp.Id;
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();             
        email.setToAddresses(toEmails);
        email.setSubject('Opportunity - '+opp.Name +' is Updated ');
        String body = 'Hi Team , <br/><br/>The opportunity <b>'+ opp.Name +'</b> status has been chnaged to <b>'+ opp.StageName+ ' </b>and please find the details below. <br/><br/> Account Name: '+ opp.Account.Name +'<br/> Amount: '+opp.Amount+'<br/> Description:'+opp.Description +'<br/><br/>';
        body +='Please <a href='+Recordlink+'>click here</a>'+ ' to view the details and reach out to '+opp.Owner.Name +' for further clarification.<br/><br/>Thanks,<br/>'+opp.Owner.Name+'<br/>';
        email.setHtmlBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
    }
}

error

19:27:39:958 EXCEPTION_THROWN [165]|System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null]

1 Answer 1

0

You've neglected to check for null emails in the sub-query:

for(Opportunity opp:[
    SELECT Id,Name,Stagename,Account.Name,Amount,Description, 
           Probability,Owner.Name,
           (SELECT Id,Name,Email__C FROM Employers__r WHERE Email__c != null) 
     FROM Opportunity 
     WHERE Id=:opportunities.keyset()]){

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .