You need to sign in to do that
Don't have an account?
dcgb2332
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!
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!
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:
Please refer the next post for next steps
All Answers
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:
Please refer the next post for next steps
In the above screen type as Account and in the next selction choose the Accoun Name
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
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!
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
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.
Change your as below:
Opportunities not Opportunity in Child Relationship
[Select Id,Name, (Select Id from Opportunities) from Account];
Thanks,
Raj