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;
    }
}


}
1
  • 1
    Pro tips: (1) give collection variable names as plurals - e.g. newTasks, not newTask; (2) newTask will never be null so don't test for it; (3) DML of any empty collection will cost nothing, no DML statement is burned so skip the tests for newTask.size()>0
    – cropredy
    Mar 9 '20 at 4:19
3

You are filtering by Status, but you have not returned this Status field back in your query. Change your query to this:

LIST<Lead> leadClosedStatus = [SELECT Id, Status
                               FROM Lead 
                               WHERE Status LIKE '%Closed%'];

I noticed another bug, however:

if(updateLead.Status == '%Closed%') {
        Task t = new Task();
        t.Status = updateLead.Status;
        newTask.add(t);
    }

Because you have already filtered by %Closed% you don't need this.

Also, the wildcard syntax only works in SOQL queries, not in an if statement.

If you still did want to do some further filtering, you would do something like:

if(updateLead.Status.contains('Closed')) {  
  //or == 'Closed' depending on context
  //do something.
}
3

The logic for the update part is flawed, in your query you are not getting the updated leads but any Lead with Status LIKE '%Closed%' and you missed to include the Status field as part of the return columns —as the previous answer pointed out.

You don't even need to query for the updated leads, you can use the trigger.new List which contains all the modified records for the transaction with all its direct fields.

A final note is that in your original question earlier it seems the requirement is to change the status for existing tasks —related to the modified leads— not creating a new Task with no details other than a Status like your code here is doing.

1
  • 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 Mar 11 '20 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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