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 

Need a trigger for updating Account Name field in Tasks from Opportunity

Hi All,

I have a custom VF page for Tasks. Currently, I cannot figure out how to auto populate a custom field, called Account Name, with the actual account name the task is associated with, from the Opportunity. Does anyone know a trigger for this?

Thank you!
Best Answer chosen by dcgb2332
Maharajan CMaharajan C
Hi Sabrina,

Sorry for the wrong guidness sabrina: am not read the requirement carefully initially.

Create the Custom Text field in Task to store the Account Name in Task

The below trigger will only fetch the Account Name as text .

===================================
Please use the below Trigger :   

trigger TaskTrigger on Task (before insert, before update ) {
    
    List<Opportunity> oppList = new List<Opportunity>();
    Map<Id,String> accountNameMap = new Map<Id,String>();
    set<Id> OppIds = new Set<Id>();
    for(Task t : trigger.new)
    {
        if(t.WhatId != null)
        {
            String whtID = t.WhatId;
            if(whtId.startsWith('006'))
            {
                OppIds.add(t.WhatId);
            }
        }
    }
    
    if(OppIds.size() > 0)
    {
        oppList = [Select Id,Account.Name from Opportunity where Id IN: OppIds];
        for(Opportunity opp : oppList)
        {
            if(opp.AccountId != null)
            accountNameMap.put(opp.Id, opp.Account.Name);
        }
        
        if(accountNameMap.size() > 0)
        {
            for(Task t : trigger.new)
            {
                if(accountNameMap.containsKey(t.WhatId))
                t.Account_Name__c = accountNameMap.get(t.WhatId);     /// Change the Api name of the field as you have currently
            }
        }
    }
}

========================

If you want to use the process builder then use the below screen shots: 

User-added image

User-added image


User-added image


User-added image


User-added image


Please refer the next post for next steps


 

All Answers

dcgb2332dcgb2332
Hi Raj, I received the below error: Error: Syntax error. Extra Account.name The steps I took: Created new field: Formula (Text) Field Name: Account Name Simple Formula: Opportunity(Lookup).Account.name Not sure what I did incorrectly… Thanks!
RahulRahul
Please share your code, so that I can look into it. 
dcgb2332dcgb2332
Sorry, what code? I don’t have anything for this yet Thanks!
RahulRahul
The VF code, to actually understand it
dcgb2332dcgb2332
Apologies:
RahulRahul
I want to understand it correctly, do you mean you want to auto-populate the account name field present in task for the current opportunity which is associated with its Account? And is this account name a standard field or custom that you created?

 
dcgb2332dcgb2332
Hi Sumit, Yes. I want to auto-populate the account name field in each task from the associated Opportunity. Bear in mind, I had to create a new custom field, since the Related To (WhatId) field, I’m using for the numeric end. Thanks
RahulRahul
so when scroll down the detail page of the opportunity and you click on new task button, from the Related list of opportunity, have you overridden the new task button with your custom vf page?
RahulRahul
ok i found the solution. I have done it through process builder. send your email id I will Illustrate the steps.
Maharajan CMaharajan C
Hi Sabrina,

Sorry for the wrong guidness sabrina: am not read the requirement carefully initially.

Create the Custom Text field in Task to store the Account Name in Task

The below trigger will only fetch the Account Name as text .

===================================
Please use the below Trigger :   

trigger TaskTrigger on Task (before insert, before update ) {
    
    List<Opportunity> oppList = new List<Opportunity>();
    Map<Id,String> accountNameMap = new Map<Id,String>();
    set<Id> OppIds = new Set<Id>();
    for(Task t : trigger.new)
    {
        if(t.WhatId != null)
        {
            String whtID = t.WhatId;
            if(whtId.startsWith('006'))
            {
                OppIds.add(t.WhatId);
            }
        }
    }
    
    if(OppIds.size() > 0)
    {
        oppList = [Select Id,Account.Name from Opportunity where Id IN: OppIds];
        for(Opportunity opp : oppList)
        {
            if(opp.AccountId != null)
            accountNameMap.put(opp.Id, opp.Account.Name);
        }
        
        if(accountNameMap.size() > 0)
        {
            for(Task t : trigger.new)
            {
                if(accountNameMap.containsKey(t.WhatId))
                t.Account_Name__c = accountNameMap.get(t.WhatId);     /// Change the Api name of the field as you have currently
            }
        }
    }
}

========================

If you want to use the process builder then use the below screen shots: 

User-added image

User-added image


User-added image


User-added image


User-added image


Please refer the next post for next steps


 
This was selected as the best answer
Maharajan CMaharajan C
User-added image
User-added image

User-added image

In the above screen type as Account and in the next selction choose the Accoun Name

User-added image

Save and Activate .


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj
dcgb2332dcgb2332
Hi Raj,

Thank you for taking the time to help. Neither the Trigger or PB worked... I'm not sure what I did incorrectly, as I'm not receiving any errors. In the trigger, when I ran the test it says 0/20 lines passed. There's nothing to indicate that the PB is running correctly or not, just that I did a sample test in stage and nothing fired.

Thanks!
 
Maharajan CMaharajan C
Hi Sabrina,

If you want the test class please use the below one : 

It will give you the 20/20   100% coverage.

@isTest
public class Test_TaskTrigger {
    
    static testmethod void inserttestTask()
    {
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        
        Opportunity oppt = new Opportunity(Name ='New Test Deal',
                                           AccountID = testAcct.ID,
                                           StageName = 'Prospecting',  // Please use the status as per your field
                                           Amount = 3000,
                                           CloseDate = System.today());
        
        insert oppt;
        
        Task t = new Task(Subject='Test Task',Status='New',Priority='Normal',CallType='Outbound',WhatId = oppt.Id);
        insert t;
        
    }
}

Thanks,
Raj
dcgb2332dcgb2332
Hi Raj,

Sorry for the back and forth, I received an error I've never seen before:


Select ID, Name,(Select Name From Opportunity)  From Account WHERE
                                  ^
ERROR at Row:1:Column:35
Didn't understand relationship 'Opportunity' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Maharajan CMaharajan C
Hi Sabrina,

Change your as below:

Opportunities not Opportunity in Child Relationship

[Select Id,Name, (Select Id from Opportunities) from Account];

Thanks,
Raj
dcgb2332dcgb2332
Hi raj, I’ve tried everything, and it’s still not working. I’m just wondering if I can create a formula field for the Account Name field, instead of doing a trigger or PB? Thanks!