You need to sign in to do that
Don't have an account?
dcgb2332
Trigger to Update Custom Field in Task
I need to update the Account Name (Account_Name__c) custom field in the Task tab, with the Account Name (AccountId). My current code is:
trigger Account_Name on Task (before insert, before update) {
for(Task a : trigger.new) {
task.Account_Name__c = a.AccountId;
}
}
I keep receiving an error that says: A value cannot be stored to Account_Name__c in type task. What am I doing wrong?
trigger Account_Name on Task (before insert, before update) {
for(Task a : trigger.new) {
task.Account_Name__c = a.AccountId;
}
}
I keep receiving an error that says: A value cannot be stored to Account_Name__c in type task. What am I doing wrong?
All Answers
Where you getting AccountId field on the Task object. There is WhatId instead?
Make sure Account_Name__c data type is same as AccountId on Task.
Also, you are using the Trigger as Before insert,so a.AccountId will be null as Task is not yet created with any Account.
Make sure Task is created and assigned to Account, then A.AccountId will not be null.
Thanks,
Naren
The below link might help you troubleshoot your issue as it gives you detail perspective of how to look for AccountId in different perspective.
https://help.salesforce.com/articleView?id=000194177&language=en_US&type=1
trigger TaskAccountNameUpdate on Task (before update) {
for (Task a : Trigger.new)
{
system.debug('AccountId'+a.AccountId);
if(a.AccountId !=null)
{
a.Account_Name__c=a.AccountId;
}
}
}
Thanks,
Naren
trigger TaskAccountNameUpdate on Task (before update) {
for (Task a : Trigger.new)
{
system.debug('AccountId'+a.AccountId);
if(a.AccountId !=null)
{
List<Account> a1 =[Select Name from Account where Id=:a.AccountId];
a.Account_Name__c=a1.get(0).Name;
}
}
}
Also, for this scenario we don't need to Write Trigger. We can do it through Process Builder or just configuration (Point and click).
Also, in Task Tab, Salesforce Already given the Account Name as Related To. You can use that instead of creating a Custom Field.
If it solved your issue, please mark as Best Answer.
Thanks,
Naren
Please please please do not use the code which you have made the best answer that code is total violation of best practices Salesforce recommend. That code is high changes of blowing off, instead please use the below code, not sure what made you not to use my code above. Anyway instead use the below one.
Please guys do not take it offense, it just a suggestion when you provide solution to anyone please make sure you provide the quality code and not just a solution. I like Narender suggestion of using point n click solution vs trigger. Anyway if you are leaning using trigger, here is the code. Thanks
As I stated before, your code does not work. It is saying a value cannot be stored to the account. The reason I cannot use a process builder or flow, is because I am already using one for another functionality on the Task. In addition, the Related To field was repurposed by another admin, and I cannot undo it without unraveling a lot of the code.
Thanks
I appreciate your effort, but it's still not working.
Thank you both!