0

I am getting following error when I update existing lead

LeadClosedStatus: execution of AfterUpdate caused by: System.StringException: Invalid id: [email protected]: External entry point

My trigger is as follows:

trigger LeadClosedStatus on Lead (after insert, after update) {
    Integer i;
    SET<ID> listNewLead = new SET<ID>();
    LIST<Task> newTasks = new LIST<Task>();
    if(Trigger.isInsert) {
        if(Trigger.isafter) {
                for(Lead newLead : Trigger.new) {
                listNewLead.add(newLead.Id);
                Task t = new Task();
                t.Status = newLead.Status;
                t.WhoId  = newLead.Id;
                newTasks.add(t);
        }
               insert newTasks;
     }     
 }
    if(trigger.isUpdate){
        if(trigger.isAfter){
            for(Lead newLead : Trigger.new) {
                if(newLead.status == 'Closed - Converted'){
                    listNewLead.add(newLead.Id);
                }
            }

        LIST<Task> relatedTasks = [SELECT ID, Status
                                   FROM Task 
                                   WHERE WhoId IN :listNewLead];

            for(Task t : relatedTasks){            
                t.status = 'Completed';
            }
            //if(relatedTasks.size() >0) {
            update relatedTasks;
            //create a master list to hold the emails to send
            LIST<Messaging.SingleEmailMessage> newMails = new LIST<Messaging.SingleEmailMessage>();
            //Have a SOQL query for all closed Leads
            LIST<Lead> leadClosedStatus = [SELECT ID, Status, Email, FirstName
                                           FROM Lead
                                           WHERE Status LIKE '%Closed%'];
            for(Lead emailLeads : leadClosedStatus) {
                //create a new email
                Messaging.SingleEmailMessage singleEmail = new Messaging.SingleEmailMessage();
                //set list of people who should get email
                LIST<ID> sendTo = new LIST<ID>();
                sendTo.add(emailLeads.Email);
                singleEmail.setToAddresses(sendTo);
                //set who the email is sent from
                singleEmail.setReplyTo('[email protected]');
                singleEmail.setSenderDisplayName('Vijay Zutshi');
                //set email contents
                singleEmail.setSubject('Task Completion Notification to Lead Owners');
                String body = 'Dear ' + emailLeads.FirstName + ' , ';
                       body += 'This is to advise the Lead has been closed successfully';
                       body += 'as the related Task is completed';
                singleEmail.setHtmlBody(body);
                //add your email to the master list
                newMails.add(singleEmail);

                //send all emails in the master list
               // Messaging.sendEmail(newMails);

            }
            //for(i=0; i<=9; i++) {

            Messaging.sendEmail(newMails);
            //}
                 //}
        }

     }
}

1 Answer 1

1
            LIST<ID> sendTo = new LIST<ID>();

You defined this list using the wrong type. It should be List<String>.

4
  • I made that change as mentioned by you but still getting following error now LeadClosedStatus: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 6; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null]: Trigger.LeadClosedStatus: line 65, column 1 Mar 19, 2020 at 18:27
  • @VijayZutshi As it says, you can't send to a null email address. Change your query to WHERE Status LIKE '%Closed%' and Email <> null];
    – sfdcfox
    Mar 19, 2020 at 18:52
  • So I made the change and then it worked. But now when I add another lead and a task and then update the lead it gives me the following error LeadClosedStatus: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 1; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded: []: Trigger.LeadClosedStatus: line 68, column 1. I am assuming this is to do with the governor limits. If that is the case is there a work around for that. Mar 19, 2020 at 19:35
  • @VijayZutshi You've reached your daily limit. Presumably you've been doing a lot of testing. You can use Messaging.reserveSingleEmailCapacity before attempting to send, or you can use Messaging.sendEmail(emails, false); to allow partial sends (and optionally parse which emails were queued, or not).
    – sfdcfox
    Mar 19, 2020 at 19:57

You must log in to answer this question.

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