+ Start a Discussion

Help with trigger to update custom object based on task completion


I am hoping to get a trigger that will update a custom object checkbox and date field when a task is marked as complete.

The task is generated by a detail object and I want the master object to be updated when the task is complete.

Basically, there is an object called Position Posting which is the master object. The detail object, Job Application, is triggering the task. I would love for the trigger to update the master record, Position Posting, when the task is marked as complete. If that doesn't work, I could make it work for it to update the detail object, Job Application, instead.

Could anyone help with a trigger? I am totally new to coding so if someone could bold the areas where I need to plug in our own object/field names, that would be helpful.

Thank you for your help!!!

need the details about the fileds that sholud be update  and the values they should be updated with
The fields that should be updated would be a checkbox indicating the task is complete, such as "Network Access Created" and the date field "Date Complete." The checkbox would be checked when the corresponding task is marked as complete and the date field would be the date the task was marked as complete (if possible).

I am hoping to do this for a few different tasks if the code is able to be customized accordingly. 
Gajanan D BhadGajanan D Bhad

Following code might be usefull for you. Modify it as per your requirement.

String position_prefix = Schema.SObjectType.Position__c.getKeyPrefix();
Set<Id> positionId = new Set<Id>();
for(Task tsk : Trigger.new){
    if(((String)tsk.WhoId).startsWith(position_prefix) && tsk.Status == 'Completed'){
List<Position__c> positionList = [Select Id From Position__c];  //add rest of field on which you want to work on.
for(Position__c pos : positionList){
    pos.tsk_completed__c = true;// what ever field you have created on Position object
    pos.Completed_Date__c = System.now(); //what ever field you have created if it is data time type. if it is of type date then use System.today()

update positonList;

Hope this code will helps you.