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
Itay MaozItay Maoz 

Mapping From Account to related Task

I'm trying to have a date from the account map to all the tasks associated with the account. The Field on Account is Date_Accepted__c and on the Task is also Date_Accepted__c. I currently have a trigger (thanks to help from this board) That populates the Date Accepted on the task when a new task is created, but if I change the date on the account, it doesn't change the date on the task. Is this possible to do with a trigger?  Here is what I have so far, its a trigger on the task. I'm guessing it would have to be a trigger on the account instead:

trigger Triggertask on Task (before insert) {
    
        id accountid ;
  for(Task task: Trigger.new) 
{
       Schema.SObjectType token = task.whatid.getSObjectType();

   if(token.getDescribe().getname()=='Account')
        accountid= task.whatid;
}
  if(accountid!=null)
  {
    account act= [select Date_Accepted__c from account where id=:accountid];
    
    for(Task tsk:Trigger.new)
    {
        tsk.Date_Accepted__c=act.Date_Accepted__c;
    }
  }

}
Best Answer chosen by Itay Maoz
sumit singh 37sumit singh 37
trigger Triggertaskaccount on Account (before update) {
date date_accept;
set<id> accountid = new set<id>;
     for(Account act:Trigger.new)
     {
         date_accept = act.Date_Accepted__c;
         accountid.add(act.id);
     }
    
    list<Task> task= [select id,Date_Accepted__c from Task where whatid In: accountid];
    
    for(Task tsk:task)
    {
        tsk.Date_Accepted__c= date_accept;
    }
  update task;
}

All Answers

sumit singh 37sumit singh 37
trigger Triggertaskaccount on Account (before update) {
date date_accept;
set<id> accountid = new set<id>;
     for(Account act:Trigger.new)
     {
         date_accept = act.Date_Accepted__c;
         accountid.add(act.id);
     }
    
    list<Task> task= [select id,Date_Accepted__c from Task where whatid In: accountid];
    
    for(Task tsk:task)
    {
        tsk.Date_Accepted__c= date_accept;
    }
  update task;
}
This was selected as the best answer
venkat-Dvenkat-D
Yes..It has to be trigger on Account. it can be after or before update based on your requirement. 

list<task> tasks = [Select id,customfield from task where parentid = :Trigger.New];

for (task t : tasks){
 task.customfield = trigger.newmap.get(parentid).customfield;
}
update tasks;
Itay MaozItay Maoz
Didn't realize i needed both, this works. Thank you so much. 
Mahesh DMahesh D
Hi Itay Maoz,

Please follow the below code:

Trigger TaskBeforeTrigger on Task (before insert) {
    Set<Id> accIdSet = new Set<Id>();
    
    for(Task ta: Trigger.new) {
        Schema.SObjectType token = task.whatid.getSObjectType();
        if(token.getDescribe().getname()=='Account') {
            accIdSet.add(task.whatid);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        Map<Id, Account> accMap = new Map<Id, Account>([Select Id, Date_Accepted__c from Account where Id IN: accIdSet];
    
        for(Task ta: Trigger.new) {
            Schema.SObjectType token = ta.whatid.getSObjectType();
            if(token.getDescribe().getname()=='Account') {
                Account tempAcc = accMap.get(ta.whatId);
                ta.Date_Accepted__c = tempAcc.Date_Accepted__c;
            }
        }
    }
}


// Trigger on the Account object
Trigger AccountAfterTrigger on Account(after update) {
    Map<Id, Account> validAccMap = new Map<Id, Account>();
    
    for(Account acc: Trigger.new) {
        if(acc.Date_Accepted__c != Trigger.oldMap.get(acc.Id).Date_Accepted__c) {
            validAccMap.put(acc.Id, acc);
        }
    }
    
    if(!validAccMap.isEmpty()) {
        List<Task> taList = [Select Id, Date_Accepted__c, whatId from Task where whatId IN: validAccMap.ketSet()];
        
        if(taList != null && !taList.isEmpty()) {
            for(Task ta: taList) {
                Account tempAcc = validAccMap.get(ta.whatId);
                ta.Date_Accepted__c = tempAcc.Date_Accepted__c;
            }
            update taList;
        }
    }
}

Regards,
Mahesh

 
venkat-Dvenkat-D
Itay, Mahesh's code will owrk fine. Sumit's code will stamp last accounts date on to all tasks not on the tasks related to its respective account.
Mahesh DMahesh D
Hi Itay Maoz,

Please check my solution as it is appropriate to your problem.

@Sumit,

The solution may not work in all scenarios as your code is on before event of Account and also not bulkified.

Regards,
Mahesh