0

everyone!

I just started learning APEX last week, and I'm now learning APEX Triggers. I need help for the task below.

Overview:

  1. I created a new field in Contact Object: "Details Completed" (checkbox)
  2. I also created an APEX trigger to make a new task: follow up contact if details are not completed (Details Completed is uncheck) // this trigger works fine

Problem: Now, I need to make a new trigger if follow up task (from above) is completed (status=completed), but "Details Completed" in Contact Object is still unchecked: New task is to make a second follow up reminder to contact to provide details

I'm trying to access the field Details_Completed__c from Contacts but it seems like I'm unable to, is there a way I can do this? Here's the code that I made:

trigger statusCompletedTrigger on Task (after update) {
    if(trigger.isUpdate){
        List<Task> taskList = new List<Task>();
        List<Task> task = [select id, subject, whoid, status from task where id in :trigger.new order by createdDate desc];
        List<Contact> con = [Select Id, LastName, Document_Verified__c from Contact];            
        
        for (task updateTask: task){
            if (updateTask.status == 'completed'){
                if (con.Document_Verified__c == false){
                    taskList.add(new Task(
                        Subject = 'Follow up again with ' + updateTask.whoId,
                        ActivityDate = system.today().addDays(7),
                        Priority = 'High', whatId=updateTask.whoId)
                    );
                }
                if (taskList.size()>0) {
                    insert taskList;
                }
            }
        }
    }
}

Kindly help me with my concern above, also, please feel free to comment on the points where I can improve, I know the code I've created above have many flaws. Thank you in advance everyone!

1 Answer 1

0

Use map instead of List<Contact> con like Map<ID, Contact> con = new Map<ID, Contact>([Select Id, LastName, Document_Verified__c from Contact]);

And in for loop check the contact id as

for (task updateTask: task){
    if (updateTask.status == 'completed'){
        if(con.containsKey(whoId)){
            if (con.get(whoId).Document_Verified__c == false){
                taskList.add(new Task(
                    Subject = 'Follow up again with ' + updateTask.whoId,
                    ActivityDate = system.today().addDays(7),
                    Priority = 'High', whatId=updateTask.whoId)
                            );
            }
        }
    }
}
if (taskList.size()>0) {
    insert taskList;
}
2
  • Hi Rohit, thank you for your response. I actually tried map() method, but got the same error because I don't know how to use it. HAHAHA. I will try the code that you have provided, I'll update you if this works! Thank you once again! :) Commented Aug 2, 2022 at 9:55
  • Hi, Rohit. i have deleted the error that I encountered. haha. I was able to fix the issue. Thank you once again for your help! the trigger is finally working! :) Commented Aug 2, 2022 at 10:52

You must log in to answer this question.

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