I want to record the Account name to the Task so I can use it for things like email alerts. I have found code allowing me to add the account name from the Account when the WhatId is an Account. I want to make it also work when related to an Op. Currently I can't add the Opportunity's AccountId value to my list. Please see below. The line

lstAccountIds.add([select Accountid from Opportunity where id =: obj.WhatId limit 1].id);

Produces the Opportunity id, not the AccountId. If it could get the AccountId, I would be fine. Thanks!

trigger AccountNameFiller on Task (before insert) {

List<id> lstAccountIds = new List<Id>();
String str;

    for(Task obj: trigger.new){
        if(str!=null && str.startswith('001')){
     if(str!=null && str.startswith('006')){
                lstAccountIds.add([select Accountid from Opportunity where id =: obj.WhatId limit 1].id);



map<id,String> mapAccounts  = new map<Id,String>();

    for(Account objAcnt:[select id,name from Account where id in:lstAccountIds])


    for(task objTask:trigger.new){

        if(str!=null && str.startswith('001')){

            objTask.Related_name__c = mapAccounts.get(objTask.whatid);   



  • Make sure you learn about bulkifying your code. This code, as written, will fail during large data loads/imports.
    – sfdcfox
    Commented Jul 21, 2017 at 19:47
  • I added a bulkified version below. Commented Jul 21, 2017 at 20:19

1 Answer 1


This is not bulkified. What you are describing is possible, but the current trigger will fail when more than 100 Tasks related to Opportunities are created. You are also probably better off making sure there is a single trigger per object and factoring out the Id Population logic. Try this:

trigger Task_Trigger on Task(before insert){
    if(Trigger.isBefore && Trigger.isInsert){

Then have a separate class:

public class Util_AccountNameFiller{
    public static final String ACCOUNT_PREFIX = Account.sObjectType.getDescribe().getKeyPrefix();
    public static final String OPPORTUNITY_PREFIX = Opportunity.sObjectType.getDescribe().getKeyPrefix();

    public static void fillAccountId(List<Task> taskList){
        Map<Id, Task> accToTask = new Map<Id, Task>();
        Map<Id, Task> oppToTask = new Map<Id, Task>();

        for(Task obj: taskList){
            String currentWhatId = obj.whatid;

            if(currentWhatId != null){
                    accToTask.put(obj.WhatId, obj);
                    oppToTask.put(obj.WhatId, obj);

        List<Account> relatedAccounts = [SELECT Id, Name FROM Account WHERE Id IN :accToTask.keySet()];
        Map<Id, Account> accountMap = new Map<Id, Account>(relatedAccounts);

        List<Opportunity> relatedOpps = [SELECT Id, Account.Name FROM Opportunity WHERE Id IN :oppToTask.keySet()];
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>(relatedOpps);

        for(Id accId : accountMap.keySet()){
            accToTask.get(accId).Related_Name__c = accountMap.get(accId).Name;

        for(Id oppId : oppMap.keySet()){
            oppToTask.get(oppId).Related_Name__c = oppMap.get(oppId).Account.Name;

I tested this in a Sandbox and it worked on tasks related to Accounts and Opportunities.

Good luck!

  • Wow ok thanks! I knew I wasn't bulkified but this is more elegant than how I could have done so.
    – jaw999
    Commented Jul 21, 2017 at 20:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .