-4

I have changed my trigger as advised above but I am getting following error:

LeadClosedStatus: execution of AfterUpdate caused by:

System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []:

Trigger.LeadClosedStatus: line 36, column 1

I am getting following error:

execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Lead.Status: Trigger.LeadClosedStatus: line 25, column 1

My trigger is as follows:-

trigger LeadClosedStatus on Lead (after insert, after update) {
     SET<ID> listNewLead = new SET<ID>();
    if(Trigger.isInsert) {
        if(Trigger.isafter) {
            LIST<Task> newTask = new LIST<Task>();

        for(Lead newLead : Trigger.new) {
            listNewLead.add(newLead.Id);
            Task t = new Task();
            t.Status = newLead.Status;
            t.WhoId  = newLead.Id;
            newTask.add(t);
    }
        if(newTask.size()>0) {
            insert(newTask);
        }            
}     
    }
LIST<Lead> leadClosedStatus = [SELECT ID
                                FROM Lead 
                                WHERE Status LIKE '%Closed%'];
LIST<Task> newTask = new LIST<Task>();
if(Trigger.isUpdate) {
    for(Lead updateLead : leadClosedStatus) {
        if(updateLead.Status == '%Closed%') {
            Task t = new Task();
            t.Status = updateLead.Status;
            newTask.add(t);
        }
    }
    if(newTask != NULL && newTask.size() > 0) {
        update newtask;
    }
}


}
3
  • 4
    There is no above advisory, nor do I see a prior question on your account. Please edit your question to include the essential details, including any relevant code. If you have another account, you may want to merge them by following these directions.
    – sfdcfox
    Mar 12 '20 at 14:05
  • This appears to be your previous account. Please do have your accounts merged. Do this by clicking the Contact link at the bottom of every page.
    – David Reed
    Mar 12 '20 at 15:08
  • 4
    Please bear in mind that you generally need to do some work to integrate the suggestions and answers you get on SFSE into your code. Reading the error carefully should tell you exactly what is wrong here and what you need to fix.
    – David Reed
    Mar 12 '20 at 15:10
4

You need to read your error messages more carefully. You are not passing an id but you are attempting an update.

You want to modify your code this way. Keep in mind that for my code to work you will first have to query the tasks that you want to update, or somehow obtain the Id of those records.

for(Lead updateLead : leadClosedStatus) {
        if(updateLead.Status == '%Closed%') {
            Task t = new Task();
            t.Id = ''; //GET THE ID OF THE RECORD TO UPDATE HERE
            t.Status = updateLead.Status;
            newTask.add(t);
        }
    }

alternatively, change the DML line to

upsert newtask;

keep in mind that this will insert new tasks for records without Ids, not update existing ones.

NOTE: There are at least two more defects in your code.

  1. the if statement if(updateLead.Status == '%Closed%') is never going to work since the equal operator does not accept wildcards (%). Furthermore, you're already doing the filtering in the SOQL above, so the entire if statement is redundant. I'd take it out.

  2. Your select statement does not narrow down the list of leads you're getting (for example, by doing id IN :someIds). That means that if you have more than 10k leads this will fail due to gov limits. Furthermore, it may close tasks for all leads, not those that are processed within the trigger context.

Not the answer you're looking for? Browse other questions tagged or ask your own question.