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
Sean BarczynskiSean Barczynski 

Trigger won't link task to custom object record

Greetings,

I'm having some trouble with a trigger I've written.  This is intended to create a task based upon certain fields in a custom object.  The tasks are created as expected, except WhatID is not populated in the tasks, therefore they are not linked to the custom object record.
 
/**********************************************************************

  This trigger checks if tax docs are received and/or scanned on
  Tax_Services__c object and assigns tasks accordingly.

**********************************************************************/

trigger Tax_Docs on Tax_Services__c (before insert, before update) {
    // The sets used to store the IDs of the Accounts or Contacts/Leads within an event that need to get updated
 /*

    Set <Id> IdSet = new Set <Id> ();

    for(Tax_Services__c e : trigger.new)
    {
        IdSet.add(e.Id);
    }

    // Creates two maps in case whatId is not populated
    Map<ID, Tax_Services__c> serviceMap = new Map<ID, Tax_Services__c>([select Id,  
                                                                               Tax_Docs_Received__c, 
                                                                               Tax_Docs_Scanned__c, 
                                                                               Business_Account__c,  
                                                                               Tax_Advisor__c, 
                                                                               Tax_Preparer__c
                                                                               from Tax_Services__c 
                                                                               Where Id in :IdSet]);
   
    List<Task> taskList = [select Id,  
                                  WhatID, 
                                  StartDateTime, 
                                  ActivityDateTime,
                                  FSTR__Sub_Type__c,  
                                  Status__c, 
                                  Financial_Plan_Update__c, 
                                  IPS_Updated__c, 
                                  Tax_Plan__c
                                  from Event 
                                  Where WhatId in :whatIdSet];
*/

    // The actual Accounts to save
    List <Task> TasksToCreate = new List <Task> ();

    for(Tax_Services__c e : Trigger.new)
    {
  
        if(Trigger.isUpdate)
        {
            Tax_Services__c oldService = Trigger.oldMap.get(e.ID);
            
            if(e.Tax_Docs_Received__c == TRUE && e.Tax_Docs_Scanned__c == TRUE)
            {
                if(e.Tax_Docs_Received__c != oldService.Tax_Docs_Received__c || e.Tax_Docs_Scanned__c != oldService.Tax_Docs_Scanned__c)
                {
                    TasksToCreate.add(new Task(OwnerID = e.Tax_Advisor__c,
                                               Subject = 'Tax Docs Received and Scanned',
                                               WhatID = e.Id,
                                               ActivityDate = date.today(),
                                               Status = 'Not Started',
                                               Priority = 'Normal',
                                               Hidden__c = 'Tax Docs Received & Scanned'));
                }
            }
            
            if(e.Tax_Docs_Received__c == TRUE && e.Tax_Docs_Scanned__c == FALSE)
            {
                if(e.Tax_Docs_Received__c != oldService.Tax_Docs_Received__c || e.Tax_Docs_Scanned__c != oldService.Tax_Docs_Scanned__c)
                {
                    TasksToCreate.add(new Task(OwnerID = e.Tax_Advisor__c,
                                               Subject = 'Scan Tax Docs',
                                               WhatID = e.Id,
                                               ActivityDate = date.today(),
                                               Status = 'Not Started',
                                               Priority = 'Normal',
                                               Hidden__c = 'Scan Tax Docs'));
                }
            }
        }
    
    
        if(Trigger.isInsert)
        {
            if(e.Tax_Docs_Received__c == TRUE && e.Tax_Docs_Scanned__c == TRUE)
            {
                    TasksToCreate.add(new Task(OwnerID = e.Tax_Advisor__c,
                                               Subject = 'Tax Docs Received and Scanned',
                                               WhatID = e.Id,
                                               ActivityDate = date.today(),
                                               Status = 'Not Started',
                                               Priority = 'Normal',
                                               Hidden__c = 'Tax Docs Received & Scanned'));
            }
            
            if(e.Tax_Docs_Received__c == TRUE && e.Tax_Docs_Scanned__c == FALSE)
            {
                    TasksToCreate.add(new Task(OwnerID = e.Tax_Advisor__c,
                                               Subject = 'Scan Tax Docs',
                                               WhatID = e.Id,
                                               ActivityDate = date.today(),
                                               Status = 'Not Started',
                                               Priority = 'Normal',
                                               Hidden__c = 'Scan Tax Docs'));
            }
        }
    

    
  /*      if(e.WhatID != null && accountMap.containsKey(e.whatId))
        {
        
        }
  */
    }
            try
            {
                Insert TasksToCreate;
            }
            catch (System.DmlException ex)
            {
                System.Debug (ex);
            }
}

Any thoughts on what may be happening here?
Best Answer chosen by Sean Barczynski
KapilCKapilC
Hi Sean,

You are trying to set whatid with e.Id and your trigger is written for "before Insert". At this time everything is available except record id. You should change your trigger to after insert at that time you have reecord id and e.Id will have record id too. Please try this, hope this will sort out your problem.

[If you got answer from my post please mark it as solution.]

Thanks,
Kapil

All Answers

KapilCKapilC
Hi Sean,

You are trying to set whatid with e.Id and your trigger is written for "before Insert". At this time everything is available except record id. You should change your trigger to after insert at that time you have reecord id and e.Id will have record id too. Please try this, hope this will sort out your problem.

[If you got answer from my post please mark it as solution.]

Thanks,
Kapil
This was selected as the best answer
Sean BarczynskiSean Barczynski
That worked!  Thanks.