I recently put an Apex Trigger into production.
Here is the code:
trigger FlagMLSTasks on Task (after insert) {
//if mls alert subject
Task t = [SELECT Id, Subject, CreatedDate, WhoId FROM Task WHERE Id IN: Trigger.newMap.keySet()].get(0);
if (t.Subject.contains('New MLS Listing Alert For')) {
MtgPlanner_CRM__Transaction_Property__c loan = [SELECT MtgPlanner_CRM__Closing_Date__c FROM MtgPlanner_CRM__Transaction_Property__c WHERE MtgPlanner_CRM__Borrower_Name__c =: t.WhoId ORDER BY MtgPlanner_CRM__Closing_Date__c DESC limit 1];
if (loan.MtgPlanner_CRM__Closing_Date__c == null){
return;
}
Date taskCreateDate = date.newinstance(t.CreatedDate.year(), t.CreatedDate.month(), t.CreatedDate.day());
Date loanCloseDate = date.newinstance(loan.MtgPlanner_CRM__Closing_Date__c.year(), loan.MtgPlanner_CRM__Closing_Date__c.month(), loan.MtgPlanner_CRM__Closing_Date__c.day());
if (Math.abs(taskCreateDate.daysBetween(loanCloseDate)) < 5){
t.Status = 'Completed';
t.False_MLS_Task__c = True;
update t;
}
}
}
I was getting the following error:
FlagMLSTasks: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject
I understand the error, so I fix it in the sandbox env.:
trigger FlagMLSTasks on Task (after insert) {
//if mls alert subject
Task t = [SELECT Id, Subject, CreatedDate, WhoId FROM Task WHERE Id IN: Trigger.newMap.keySet()].get(0);
if (t.Subject.contains('New MLS Listing Alert For')) {
List<MtgPlanner_CRM__Transaction_Property__c> loans = [SELECT MtgPlanner_CRM__Closing_Date__c FROM MtgPlanner_CRM__Transaction_Property__c WHERE MtgPlanner_CRM__Borrower_Name__c =: t.WhoId ORDER BY MtgPlanner_CRM__Closing_Date__c DESC];
if (loans.size() == 0 || loans.get(0).MtgPlanner_CRM__Closing_Date__c == null){
return;
}
MtgPlanner_CRM__Transaction_Property__c mostRecentLoan = loans.get(0);
Date taskCreateDate = date.newinstance(t.CreatedDate.year(), t.CreatedDate.month(), t.CreatedDate.day());
Date loanCloseDate = date.newinstance(mostRecentLoan.MtgPlanner_CRM__Closing_Date__c.year(), mostRecentLoan.MtgPlanner_CRM__Closing_Date__c.month(), mostRecentLoan.MtgPlanner_CRM__Closing_Date__c.day());
if (Math.abs(taskCreateDate.daysBetween(loanCloseDate)) < 5){
t.Status = 'Completed';
t.False_MLS_Task__c = True;
update t;
}
}
}
However, I'm having trouble trying to migrate the updated trigger into production ( a problem I did not get before when first migrating the trigger).
Here is the error I get when attempting to deploy:
System.UnexpectedException: No more than one executeBatch can be called from within a test method. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.
Stack Trace: External entry point
It seems to me that both the trigger in live is triggering the same time my new trigger is being deployed (when you deploy you have to run tests to ensure proper coverage before it is actually released into live).
My problem with this error is that I never got this before. Anytime I want to make an update to a trigger, i just make the necessary changes and then on deployment it should replace the old trigger of the same name. Why is this happening with this particular trigger? How do I go about fixing it? I'm weary of deleting the old trigger (as I think this maybe what is causing me not to be able to deploy the updated version), is there any quick way of addressing this?
Here is the test that goes along with both the old and new trigger (I used the same test).
@isTest(SeeAllData=true)
public class TestFlagMLSTasks {
@isTest static void testConfirmedFalseMLS(){
Contact c = [SELECT Id FROM Contact WHERE Id = '0031U00000R8yKmQAJ'];
Task t = new Task(Subject = 'New MLS Listing Alert For', WhoId = c.Id, Status = 'Not Started');
insert t;
System.assert(!t.False_MLS_Task__c);
}
}