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
mac adminmac admin 

How to update the custom field when a task field is completed

Hi all,
I want to update custom field when an task is closed in that particular custom field. Can any one help me over here.

Thanks in advance.
regards,
mac.
Best Answer chosen by mac admin
Nitish TalekarNitish Talekar
Hi Mac,

I have updated if condition and its works for me. Task subect copied on account record.

trigger TaskTrigger on Task (After update) {
    Set<ID> setWhatId = new Set<ID>();
    Map<Id,Task> mapWhatIdWithTask = new Map<Id,Task>();
    
    for (Task objTask : Trigger.new) {
        if(objTask.status.equalsIgnoreCase('Completed')) {
            setWhatId.add(objTask.whatId);
            mapWhatIdWithTask.put(objTask.whatId,objTask);
        }
    }
    
    List<Account> lstAccount = new List<Account>();
    
    for(Account objAccount: [SELECT Task_Subject__c FROM Account WHERE ID IN :setWhatId]) {
        Task objTask = mapWhatIdWithTask.get(objAccount.id);
        objAccount.Task_Subject__c = objTask.subject;
        lstAccount.add(objAccount);
    }
    if(lstAccount.size()>0)
        update lstAccount;
}

And about the auto populate the look up value, now we can not achive this. You can create lookup field on Task but can not auto populate it.

Please mark this as best answer if it works for you.

Thanks,
Nitish

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hi Mac, 

You requirement is not clear, if you want to update a field, if a task is closed, then you can use workflow or process builder. 


Thanks, 
Sumit Kumar Singh
mac adminmac admin
Hi Sumit,
When I create a task in custom object and when task is completed then I want to update my custom field with task subject in my custom object. Can you please help me over here.

Thanks in advance.

Regards,
mac.
Nitish TalekarNitish Talekar
Hi Mac,

Once task completed then in After update event of task trigger with the help of WhatId field (task standard field) which contains the custom object id and then query to custom object and update the custom field with task subject.

Please mark this as best answer if it works for you.

Thanks,
Nitish
mac adminmac admin
Hi Nitish can you give me a sample example . It will be helpfull for me.

Regards,
mac
Nitish TalekarNitish Talekar
Hi Mac,

Here is the example:
Suppose taskSubject__c is the custom field which is on Account which you want to update once task is closed.


trigger TaskTrigger on Task (After update) {
    Set<ID> setWhatId = new Set<ID>();
    Map<Id,Task> mapWhatIdWithTask = new Map<Id,Task>();
    
    for (Task objTask : Trigger.new) {
        if(objTask.status == 'Closed') {
            setWhatId.add(objTask.whatId);
            mapWhatIdWithTask.put(objTask.whatId,objTask);
        }
    }
    
    List<Account> lstAccount = new List<Account>();
    
    for(Account objAccount: [SELECT taskSubject__c FROM Account WHERE ID IN :setWhatId]) {
        Task objTask = mapWhatIdWithTask.get(objAccount.id);
        objAccount.taskSubject__c = objTask.subject;
        lstAccount.add(objAccount);
    }
    
    if(lstAccount.size()>0)
        update lstAccount;
}

Please mark this as best answer if it works for you.

Thanks,
Nitish
mac adminmac admin
Hi Nitish,
Thanks for the reply. I have changed your code as below but the Status is not changing in the custom object. can please let me know why i'm not able to update the Status field.
trigger updtaestatuscon on Task (After update) {
    Set<ID> Contact = new Set<ID>();
    Map<Id,Task> mapWhatIdWithTask = new Map<Id,Task>();
    
    for (Task objTask : Trigger.new) {
        if(objTask.status == 'Closed') {
            Contact.add(objTask.whatId);
            mapWhatIdWithTask.put(objTask.whatId,objTask);
        }
    }
    
    List<Sales_order__c> lstAccount = new List<Sales_order__c>();
    
    for(Sales_order__c objAccount: [SELECT Status__c FROM Sales_order__c WHERE ID IN :Contact]) {
        Task objTask = mapWhatIdWithTask.get(objAccount.id);
        objAccount.Status__c = objTask.subject;
        lstAccount.add(objAccount);
    }
    
    if(lstAccount.size()>0)
        update lstAccount;
}

Regards,
mac.
mac adminmac admin
Hi Nitish,
Can we auto populate the look up value for Name(Contact / Lead) field in the Task page which will open when we click on New Task button in the related list. In Name field we have Contact and Lead in my scenario I'm choosing contact. I wna t to auto populate the contact name when the Task page is opening. Can we achive this...?
Nitish TalekarNitish Talekar
Hi Mac,

I have updated if condition and its works for me. Task subect copied on account record.

trigger TaskTrigger on Task (After update) {
    Set<ID> setWhatId = new Set<ID>();
    Map<Id,Task> mapWhatIdWithTask = new Map<Id,Task>();
    
    for (Task objTask : Trigger.new) {
        if(objTask.status.equalsIgnoreCase('Completed')) {
            setWhatId.add(objTask.whatId);
            mapWhatIdWithTask.put(objTask.whatId,objTask);
        }
    }
    
    List<Account> lstAccount = new List<Account>();
    
    for(Account objAccount: [SELECT Task_Subject__c FROM Account WHERE ID IN :setWhatId]) {
        Task objTask = mapWhatIdWithTask.get(objAccount.id);
        objAccount.Task_Subject__c = objTask.subject;
        lstAccount.add(objAccount);
    }
    if(lstAccount.size()>0)
        update lstAccount;
}

And about the auto populate the look up value, now we can not achive this. You can create lookup field on Task but can not auto populate it.

Please mark this as best answer if it works for you.

Thanks,
Nitish
This was selected as the best answer
mac adminmac admin
Hi Nitish,
Thanks for reply thats worked.
Can we auto populate the look up value for Name(Contact / Lead) field in the Task page which will open when we click on New Task button in the related list. In Name field we have Contact and Lead in my scenario I'm choosing contact. I wna t to auto populate the contact name when the Task page is opening. Can we achive this...?