You need to sign in to do that
Don't have an account?
Sean Barczynski
Setting WhatID to Custom Object
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.
Any thoughts on what may be happening here?
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) { 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')); } } } try { Insert TasksToCreate; } catch (System.DmlException ex) { System.Debug (ex); } }
Any thoughts on what may be happening here?
Tax_Services__c doesn't have any ID yet which can be passed as a WhatID to the Task. Change the trigger to a after insert and that should help with the whatID getting populated.