You need to sign in to do that
Don't have an account?
Sean Barczynski
Tasks not being created
Hello!
I'm having a problem with a trigger not creating tasks that are part of a FOR loop, even though other pieces of the loop are being performed. I think it has something to do with ActivityDate field because if I do date.today(), it works fine, but if I do date.newinstance(...), it doesn't get created. In the fields list for Tasks, it shows as date/time, but when I try to do datetime.newinstance, I get an error saying it's expecting a date. The issue is from line 85 to line 115 (bold and italics).
I'm having a problem with a trigger not creating tasks that are part of a FOR loop, even though other pieces of the loop are being performed. I think it has something to do with ActivityDate field because if I do date.today(), it works fine, but if I do date.newinstance(...), it doesn't get created. In the fields list for Tasks, it shows as date/time, but when I try to do datetime.newinstance, I get an error saying it's expecting a date. The issue is from line 85 to line 115 (bold and italics).
/********************************************************************** This trigger updates the Last Meeting Date fields at the HH level **********************************************************************/ trigger LastEventDate on Event (after insert, after update) { // The sets used to store the IDs of the Accounts or Contacts/Leads within an event that need to get updated Set <Id> whatIdSet = new Set <Id> (); for(Event e : trigger.new) { if(e.WhatId != null) { whatIdSet.add(e.WhatId); } } // Creates two maps in case whatId is not populated Map<ID, Account> accountMap = new Map<ID, Account>([select Id, Last_Review_Meeting__c, Last_FP_Update__c, Last_IPS_Update__c, Last_Tax_Planning_Meeting__c, Last_Tax_Prep_Meeting__c from Account Where Id in :whatIdSet]); List<Event> eventList = [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]; List<Tax_Services__c> serviceList = [select Id, Business_Account__c, Meeting_Date__c, Client_will_send_docs__c, Tax_Docs_Received__c, Tax_Docs_Scanned__c, Tax_Preparer__c, Tax_Year__c from Tax_Services__c Where Business_Account__c in :whatIdSet]; // The actual Accounts & Tax Services to save List <Account> AccountsToUpdate = new List <Account> (); List <Tax_Services__c> ServicesToUpdate = new List <Tax_Services__c> (); List <Task> TasksToCreate = new List <Task> (); for(Event e : Trigger.new) { if(e.WhatID != null && accountMap.containsKey(e.whatId)) { Account a1 = accountMap.get(e.WhatId); Date d1 = Date.newInstance(e.ActivityDateTime.year(), e.ActivityDateTime.month(), e.ActivityDateTime.day()); //If new meeting is scheduled as planned, set this meeting date to the appropriate Last Meeting Date field if(e.Status__c != 'Canceled' && e.Status__c != 'Re-Scheduled' && Trigger.isInsert) { if(e.FSTR__Sub_Type__c == 'Tax Preparation Meeting') { if(d1 > a1.Last_Tax_Prep_Meeting__c) { a1.Last_Tax_Prep_Meeting__c = d1; } } if(e.FSTR__Sub_Type__c == 'Tax Planning Meeting' || e.Tax_Plan__c) { if(d1 > a1.Last_Tax_Planning_Meeting__c) { a1.Last_Tax_Planning_Meeting__c = d1; } for(Integer i=0; i<=serviceList.size()-1; i++) { if(serviceList[i].Business_Account__c==a1.Id && integer.valueof(serviceList[i].Tax_Year__c)==e.ActivityDateTime.year()) { //Update Tax Service Meeting Date serviceList[i].Meeting_Date__c=Date.newInstance(e.ActivityDateTime.year(), e.ActivityDateTime.month(), e.ActivityDateTime.day()); ServicesToUpdate.add(serviceList[i]); TasksToCreate.add(new Task(OwnerID = serviceList[i].Tax_Preparer__c, Subject = 'Complete Tax Plan', WhatID = serviceList[i].Id, ActivityDate = d1.addDays(-5), Description = 'Mark task complete when tax plan is received', Status = 'Not Started', Priority = 'Normal', Hidden__c = 'Complete Tax Plan')); // Create "Expect Tax Docs" task if client is sending, but have not been received if(serviceList[i].Client_will_send_docs__c==TRUE && serviceList[i].Tax_Docs_Received__c==FALSE) { TasksToCreate.add(new Task(OwnerID = serviceList[i].Tax_Preparer__c, Subject = 'Expect Tax Docs', WhatID = serviceList[i].Id, ActivityDate = d1.addDays(-5), Description = 'Mark task complete when tax docs are received', Status = 'Not Started', Priority = 'Normal', Hidden__c = 'Expect Tax Docs')); } } } } if(e.FSTR__Sub_Type__c == 'Client Review Meeting') { if(d1 > a1.Last_Review_Meeting__c) { a1.Last_Review_Meeting__c = d1; } } if(e.Financial_Plan_Update__c) { if(d1 > a1.Last_FP_Update__c) { a1.Last_FP_Update__c = d1; } } if(e.IPS_Updated__c) { if(d1 > a1.Last_IPS_Update__c) { a1.Last_IPS_Update__c = d1; } } AccountsToUpdate.add (a1); } // If meeting is canceled or rescheduled, update all Last Meeting date fields if(Trigger.isUpdate) { a1.Last_Review_Meeting__c=Date.newInstance(1900, 1, 1); a1.Last_FP_Update__c=Date.newInstance(1900, 1, 1); a1.Last_IPS_Update__c=Date.newInstance(1900, 1, 1); a1.Last_Tax_Planning_Meeting__c=Date.newInstance(1900, 1, 1); a1.Last_Tax_Prep_Meeting__c=Date.newInstance(1900, 1, 1); for(Integer i=0; i<=serviceList.size()-1; i++) { if(serviceList[i].Business_Account__c==a1.Id && integer.valueof(serviceList[i].Tax_Year__c)==e.ActivityDateTime.year() && e.FSTR__Sub_Type__c == 'Tax Planning Meeting') { if(e.Status__c == 'Canceled' || e.Status__c == 'Re-Scheduled') { serviceList[i].Meeting_Date__c=NULL; ServicesToUpdate.add(serviceList[i]); } } } for(Integer i=0; i<=eventList.size()-1; i++) { // Tax Planning Meeting if(eventList[i].FSTR__Sub_Type__c == 'Tax Preparation Meeting' && eventList[i].WhatID == a1.Id && eventList[i].ActivityDateTime > a1.Last_Tax_Prep_Meeting__c && eventList[i].Status__c == 'Scheduled') { a1.Last_Tax_Prep_Meeting__c = Date.newInstance(eventList[i].ActivityDateTime.year(), eventList[i].ActivityDateTime.month(),eventList[i].ActivityDateTime.day()); } // Tax Prep Meeting if(eventList[i].FSTR__Sub_Type__c == 'Tax Planning Meeting' || eventList[i].Tax_Plan__c) { if(eventList[i].WhatID == a1.Id && eventList[i].ActivityDateTime > a1.Last_Tax_Planning_Meeting__c && eventList[i].Status__c == 'Scheduled') { a1.Last_Tax_Planning_Meeting__c = Date.newInstance(eventList[i].ActivityDateTime.year(), eventList[i].ActivityDateTime.month(),eventList[i].ActivityDateTime.day()); } } // Client Review Meeting if(eventList[i].FSTR__Sub_Type__c == 'Client Review Meeting' && eventList[i].WhatID == a1.Id && eventList[i].ActivityDateTime > a1.Last_Review_Meeting__c && eventList[i].Status__c == 'Scheduled') { a1.Last_Review_Meeting__c = Date.newInstance(eventList[i].ActivityDateTime.year(), eventList[i].ActivityDateTime.month(),eventList[i].ActivityDateTime.day()); } // IPS Update if(eventList[i].IPS_Updated__c && eventList[i].WhatID == a1.Id && eventList[i].ActivityDateTime > a1.Last_IPS_Update__c && eventList[i].Status__c == 'Scheduled') { a1.Last_IPS_Update__c = Date.newInstance(eventList[i].ActivityDateTime.year(), eventList[i].ActivityDateTime.month(), eventList[i].ActivityDateTime.day()); } // FP Update if(eventList[i].Financial_Plan_Update__c && eventList[i].WhatID == a1.Id && eventList[i].ActivityDateTime > a1.Last_FP_Update__c && eventList[i].Status__c == 'Scheduled') { a1.Last_FP_Update__c = Date.newInstance(eventList[i].ActivityDateTime.year(), eventList[i].ActivityDateTime.month(),eventList[i].ActivityDateTime.day()); } } AccountsToUpdate.add (a1); } try { update AccountsToUpdate; update ServicesToUpdate; insert TasksToCreate; } catch (System.DmlException ex) { System.Debug (ex); } } } }
At a high level, I do not see any issue in your code. So just wanted you to check data insertion failure due to some other reasons?
In Developer Console open up the Execute Anonymous and type this code
Task t = new Task();
t.ActivityDate = Date.today();
Execute it. You'll see it works fine.
Now try
Task t = new Task();
t.ActivityDate = System.now();
Execute it. You'll see an exception.
Doesn't prove that's the only issue, but it proves that your theory is correct that you can't use datetime for ActivityDate.
Let me know your thoughts.