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
dcgb2332dcgb2332 

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?
Best Answer chosen by dcgb2332
Abdul KhatriAbdul Khatri
Give this a last try. If this doesn't work then I will end here. Sorry
trigger UpdateAccountName on Task (before update) 
{  
    Map<Id, Task> idAccountTaskMap = new Map<Id, Task>();
    for (Task t : Trigger.new) {
        
        if(t.AccountId != null)
            idAccountTaskMap.put(t.AccountId, t);
    }

    if(idAccountTaskMap.isEmpty()) return;
    
    Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id = :idAccountTaskMap.keyset()]);
    
    for(Task a : idAccountTaskMap.values()) 
    {   
        a.Account_Name__c = accountMap.get(a.AccountId).Name;
    }
}

 

All Answers

Abdul KhatriAbdul Khatri
What is the Data Type of the Custom Field Account_Name__c?
Where you getting AccountId field on the Task object. There is WhatId instead?
Naren9Naren9
Hi Sabrina,
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
Abdul KhatriAbdul Khatri
Sorry my bad AccountId is available but get filled based on WhatId

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
Abdul KhatriAbdul Khatri
If you are looking at populating the Custom Field Account_Name__c with the Account Name based on Id, I would recommend to change your triggers accordingly.
 
trigger Account_Name on Task (before insert, before update) { 
    
	for(Task a : [SELECT Account.Name FROM Task WHERE Id IN :trigger.new]) 
       {        
               task.Account_Name__c = a.Account.Name;
       }
}


 
Abdul KhatriAbdul Khatri
Please share the the Details of Account_Name__c field.
dcgb2332dcgb2332
Account Name – Account_Name__c - Text Area (255)
Naren9Naren9
Use the below code, it is working in my Org:
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
dcgb2332dcgb2332
Naren, It’s working but it comes in as a numeric name rather than the account name: Example: Account Name 0013600000PSj3qAAD It should be: Account Name: Walmart Thanks!
Naren9Naren9
Just change the code like below:
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
dcgb2332dcgb2332
Narender, You ARE THE BEST! Thank you so much, it worked perfectly ☺ THANK YOU!
Abdul KhatriAbdul Khatri
Hi Sabrina,

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.
trigger Account_Name on Task (before insert, before update) 
{   
    for(Task a : [SELECT Account.Name FROM Task WHERE Id IN :trigger.new]) 
    {   
        if(a.Account.Name != null)
	        task.Account_Name__c = a.Account.Name;
    }
}
Thanks
dcgb2332dcgb2332
Abdul,

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
Abdul KhatriAbdul Khatri
Sorry little correction in my code. It would be very strange if this code will not work as it is doing the same thing what you are doing in the one from the best answer but following the best practise. I am just trying to help you out for being ambarrased in future.
 
trigger Account_Name on Task (before insert, before update) 
{   
    List<Task> task = [SELECT Account.Name FROM Task WHERE Id IN :trigger.new];
    for(Task a : task) 
    {   
        if(a.Account.Name != null)
	        task.Account_Name__c = a.Account.Name;
    }
}

 
dcgb2332dcgb2332
Abdul,

I appreciate your effort, but it's still not working. 
Abdul KhatriAbdul Khatri
Sorry my bad, this code should not disappoint you as it will work before insert and before update in both cases
 
trigger Account_Name on Task (before insert, before update) 
{  
    Map<Id, Task> idAccountTaskMap = new Map<Id, Task>();
    for (Task t : Trigger.new) {
        
        if(t.AccountId != null)
            idAccountTaskMap.put(t.AccountId, t);
    }

    if(idAccountList.isEmpty()) return;
    
    Map<Id, Account> accountMap = [SELECT Name FROM Account WHERE Id = :idAccountTaskMap.keyset()];
    
    for(Task a : idAccountTaskMap.values()) 
    {   
        a.Account_Name__c = accountMap.get(a.AccountId).Name;
    }
}

 
Abdul KhatriAbdul Khatri
Give this a last try. If this doesn't work then I will end here. Sorry
trigger UpdateAccountName on Task (before update) 
{  
    Map<Id, Task> idAccountTaskMap = new Map<Id, Task>();
    for (Task t : Trigger.new) {
        
        if(t.AccountId != null)
            idAccountTaskMap.put(t.AccountId, t);
    }

    if(idAccountTaskMap.isEmpty()) return;
    
    Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id = :idAccountTaskMap.keyset()]);
    
    for(Task a : idAccountTaskMap.values()) 
    {   
        a.Account_Name__c = accountMap.get(a.AccountId).Name;
    }
}

 
This was selected as the best answer
dcgb2332dcgb2332
For anyone who comes across this post, both Abdul and Naren's codes work, however, Abdul's adheres more to best practices for Salesforce.

Thank you both!
dcgb2332dcgb2332
Wow, that worked. Thank you so much for trying again. I will award you best answer. Thanks!