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
Keith GayKeith Gay 

Help needed! Trying to make a trigger to update a custom field on task upon creation that reads a lookup field from the attached account.

I am new to apex so just trying my best. I know this is not right but this is what I am working with so far. Any insight would be greatly appreciated.

trigger channelpartner on Task (after insert) {
for (task t : Trigger.new) {
if(acct.channel_object != null){

Task tsk = new Task(),
accList.add(c.accountID)

t.channel_partner = acct.channel_object

tasks.add(tsk);}
}
insert tasks;
}
Best Answer chosen by Keith Gay
v varaprasadv varaprasad
Hi Keith.

As per my understanding, you are assigning task related account field(channel_object__c) in channel_partner__c .

Please use correct field API name and use following code it will help you.
 
trigger channelpartner on Task (before insert) {


set<id> accIds = new set<id>();

for (task t : Trigger.new){
if(t.whatid != null){
   accIds.add(t.whatid);
}
}
Map<id,account> mapOfAccdata = new map<id,account>([select id,name,channel_object_C from account where id in : accIds]);

for(task t : Trigger.new){    
    t.channel_partner__c = mapOfAccdata.get(t.whatid).channel_object_C;

}

}

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 

All Answers

v varaprasadv varaprasad
Hi Keith.

As per my understanding, you are assigning task related account field(channel_object__c) in channel_partner__c .

Please use correct field API name and use following code it will help you.
 
trigger channelpartner on Task (before insert) {


set<id> accIds = new set<id>();

for (task t : Trigger.new){
if(t.whatid != null){
   accIds.add(t.whatid);
}
}
Map<id,account> mapOfAccdata = new map<id,account>([select id,name,channel_object_C from account where id in : accIds]);

for(task t : Trigger.new){    
    t.channel_partner__c = mapOfAccdata.get(t.whatid).channel_object_C;

}

}

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
This was selected as the best answer
Keith GayKeith Gay
Amazing Varaprasad, thank you for the reply! I believe this is what I am looking for. My only question would be, would this still work even if the channel_object__c field on account is looking up to a custom object? definitely going to try this out in any case. Thank you again for the help, I greatly appreciate it.