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
Aaron HillAaron Hill 

Trigger to Update checkbox based on new activity type

The title says it all. We have an activity type on leads called "First Time Meeting (Phone or In-Person)". So when someone in the org logs a call on a lead with this status we want to have a checkbox called "Had First Time Meeting" Update to "true". This is what I have so far:
 
trigger hadFirstMeeting on Task (before update) {
    for (task t : Trigger.new) {
        if (t.Type === "First Time Meeting (Phone or In-Person)") {
        
        }
    }
}

This produces an error: Error: Compile Error: line 3:23 no viable alternative at character '"' at line 3 column 23

I'm a JS programmer, fairly new to Apex. I originally planned for it to look like this: 
 
trigger hadFirstMeeting on Task on Lead (before update) {
    for (task t, Lead L : Trigger.new) {
        if (t.Type === "First Time Meeting (Phone or In-Person)") {
            L.Had_First_Time_Meeting__c = true;
        }
    }
}

But I have no idea if this is the correct syntax. I'm having a hard time finding resources for apex that talk about activities. I'm not sure how a lead should relate to a task within the code. And first I need to solve the error anyway. Can anyone offer some help on this? Thanks!

 
@Karanraj@Karanraj
You have to use single quotation mark for string values in apex language. 
trigger hadFirstMeeting on Task (before update) {
    for (task t : Trigger.new) {
        if (t.Type == 'First Time Meeting (Phone or In-Person)') {
            L.Had_First_Time_Meeting__c = true;
        }
    }
}

 
Aaron HillAaron Hill
Great, thanks. But how do I relate the lead with the task?
@Karanraj@Karanraj
if the task record is created manually from the related list of lead record then the task will be linked to lead object. The trigger.new contains all the values entered by user for the task record.
Aaron HillAaron Hill
So this is what I have:
 
trigger hadFirstMeeting on Task (before update) {
    for (task t : Trigger.new) {
        if (t.Type == 'First Time Meeting (Phone or In-Person)') {
            t.Had_First_Time_Meeting__c = true;
        }
    }
}

But I don't think I'm referencing the correct object. Because even though task and and lead are linked, my custom field "Had_First_Time_Meeting__c" is a child of the lead specifically. I get the following error: Error: Compile Error: Invalid field Had_First_Time_Meeting__c for SObject Task at line 4 column 13

So I tried it referencing the lead instead of task t. Like so: 
 
trigger hadFirstMeeting on Task (before update) {
    for (task t : Trigger.new) {
        if (t.Type == 'First Time Meeting (Phone or In-Person)') {
            Lead.Had_First_Time_Meeting__c = true;
        }
    }
}

And I got the error: Error: Compile Error: Expression cannot be assigned at line -1 column -1

Apologies if this is really basic. Appreciate your help. How do I reference the linked lead within the code?
Pankaj_GanwaniPankaj_Ganwani
Hi All,

By going through the problem statement and above comments, it seems the checkbox field on lead should be checked if a task record is created with subject "First Time Meeting (Phone or In-Person)". In this case, we will have to create a trigger on Task object with After insert event and then update the related Lead record.
 
trigger hadFirstMeeting on Task (after insert) {
    List<Lead> updatelead = new List<Lead>();
	for (task t : Trigger.new) {
        if (t.Type == 'First Time Meeting (Phone or In-Person)' && t.whatId!=null && t.whatId.getsobjectType() == Lead.sobjectType) {
            updatelead.add(new Lead(Id = t.whatId, Had_First_Time_Meeting__c  = true));
        }
    }
	
	update updatelead;
}

 
Aaron HillAaron Hill
Thanks Pankaj, I updated the rule with your code. It saves fine but it still doesn't update the checkbox after I create a new task on a lead. I double checked the merge field. And the picklist value. I also tried swapping the merge field (Had_First_Time_Meeting__c) with a different one (Title) and just had it update the text. Again, it saved fine but when I created a new task nothing changed. Was I supposed to customize the values in your code somehow?

Thanks!