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
sfdcianpsfdcianp 

Trigger on task

i have a relation with account and contact.Account record is having the contact name as a field.

Whenever a new task is created with related to an account i need to equate the contact field on account to the new custom field in the task obj.

 

COuld any one help me in this scenario..

 

 

Thanks in advance

souvik9086souvik9086

Try this

 

trigger PopulateContactOnTask on Task(before insert){

List<Id> lstid = new List<Id>()[
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
lstid.add(t.WhatId);
}
}
Map<id,account> aMap = new Map<id,account>([Select Id,Contactname__c from Account where id in :lstid]);
for(task t : Trigger.new){
if(t.WhatId.startsWith('001') != NULL){
t.taskcontact__c = aMap.get(t.WhatId).Contactname__c;
}
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks