You need to sign in to do that
Don't have an account?

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;
}
}
}
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;
}
}
}
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
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;
}
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;
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
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