function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Claire Sunderland 1Claire Sunderland 1 

Exclude converted leads from trigger

I am a Salesforce admin working to finish a Trigger in our Sandbox for tesitng. I have an Apex Trigger that is being used to rollup certain activities onto the lead record. When I try to convert the lead I'm hitting an error that I'm interpreting is because it's pushing an update to the activities on the record and then looking for the Lead.Id but this no longer exists. Can someone show me how to trigger only if the Lead is not converted?

Error message:

Error: There was an error converting the lead. Please resolve the following error and try again: ActivityRollups: execution of AfterUpdate caused by: System.SObjectException: Invalid Id for Lead 
Best Answer chosen by Claire Sunderland 1
Andrew GAndrew G
Hi Claire

Since we are working from the Task, to minimise doing a query to get the associated contacts/leads, just test the WhoId as it is contained on the task. 
Since contacts records start with a prefix of '003', let us test the what the Id starts with and then use the same remove function for those records before we pass the list to the Rollup helper class
Note that leads are prefixed by 00Q
 
trigger ActivityRollups on Task (after insert, after update, after delete, after undelete)

{
    String contactPrefix = '003';

    Task[] objects = new Task[]{};
    if (Trigger.isDelete) {
        objects.addAll(Trigger.old);
    } else {
        objects.addAll(Trigger.new);
    }

    for (Integer i = objects.size()-1 ; i>=0 ; i--) {
        if (objects[i].WhoId==null) {
            objects.remove(i);
        } else if (String.valueOf(objects[i].WhoId ).startsWith(contactPrefix)) {
            objects.remove(i);
        }
    }

    if (objects.isEmpty()) {
        return;
    }

    ActivityRollups.doRollups(objects);

}

Hope this helps

Andrew

All Answers

Anthony McDougaldAnthony McDougald
Hello Claire,
Hope that your day is off to an amazing start. I would use an IF statement to look for the IsConverted field equals false before calling a class's method in your trigger. Example is below and hope this helps. May God bless you abundantly.
If(Lead.IsConverted == false){
do xyz
}
Best Regards,
Anthony McDougald
Claire Sunderland 1Claire Sunderland 1
Thanks Anthony! Hope you are also having a great day. I thought I'd be able to figure this out but I don't know where to put the If(Lead.IsConverted == false) criteria. Here is the trigger do you have any more insight? Thank you so much for your help!
trigger ActivityRollups on Task (after insert, after update, after delete, after undelete) 

{
    Task[] objects = new Task[]{};
    if (Trigger.isDelete) {
        objects.addAll(Trigger.old);
    } else {
        objects.addAll(Trigger.new);
    }

    for (Integer i = objects.size()-1 ; i>=0 ; i--) {
        if (objects[i].WhoID==null) {
            objects.remove(i);
        }
    }

    if (objects.isEmpty()) {
        return;
    }


    ActivityRollups.doRollups(objects);

}
Andrew GAndrew G
Hi Claire

Since we are working from the Task, to minimise doing a query to get the associated contacts/leads, just test the WhoId as it is contained on the task. 
Since contacts records start with a prefix of '003', let us test the what the Id starts with and then use the same remove function for those records before we pass the list to the Rollup helper class
Note that leads are prefixed by 00Q
 
trigger ActivityRollups on Task (after insert, after update, after delete, after undelete)

{
    String contactPrefix = '003';

    Task[] objects = new Task[]{};
    if (Trigger.isDelete) {
        objects.addAll(Trigger.old);
    } else {
        objects.addAll(Trigger.new);
    }

    for (Integer i = objects.size()-1 ; i>=0 ; i--) {
        if (objects[i].WhoId==null) {
            objects.remove(i);
        } else if (String.valueOf(objects[i].WhoId ).startsWith(contactPrefix)) {
            objects.remove(i);
        }
    }

    if (objects.isEmpty()) {
        return;
    }

    ActivityRollups.doRollups(objects);

}

Hope this helps

Andrew
This was selected as the best answer
Claire Sunderland 1Claire Sunderland 1
Thanks Andrew! That worked great