You need to sign in to do that
Don't have an account?
Jason Walstad 2
Apex trigger not Properly Instantiating Event object
Hi all,
I have a simple trigger on the Contract object designed to create a new Event when the contract status is updated to 'Activated'.
However, the Events do not seem to be created by my trigger. I cannot pinpoint the reason why these Events are not being properly created.
Here is the error generated by my test class: System.AssertException: Assertion Failed: Expected: 0, Actual: 4
Can anyone help?
Trigger code:
trigger StaggeredEventCreation on Contract(before update){
//All contract records which activated the trigger
List<Contract> c = Trigger.new;
//Only update those records which had a status change to 'Activated'
for (Contract temp : c){
if (temp.Status == 'Activated' && temp.Status != Trigger.oldMap.get(temp.ID).Status){
Event e1 = new Event(OwnerId = '',
StartDateTime = Datetime.Now(), EndDateTime = Datetime.Now() + 7, Subject = 'Initial Followup');
Event e2 = new Event(OwnerId = '',
StartDateTime = Datetime.Now() + 30, EndDateTime = Datetime.Now() + 37, Subject = '30-Day Followup');
Event e3 = new Event(OwnerId = '',
StartDateTime = Datetime.Now() + 60, EndDateTime = Datetime.Now() + 67, Subject = '60-Day Followup');
Event e4 = new Event(OwnerId = '',
StartDateTime = Datetime.Now() + 90, EndDateTime = Datetime.Now() + 97, Subject = '90-Day Followup');
}
}
Apex test class code:
@isTest
private class StaggeredEventCreationTest{
@isTest static void validateStaggeredEventCreation(){
Contract c = new Contract(AccountId = '', StartDate = Date.today(),
ContractTerm = 12, Status = 'Draft');
insert c;
c.Status = 'Activated';
update c;
Integer eventCount = [SELECT count() FROM Event];
System.assertEquals(eventCount, 4);
}
}
I have a simple trigger on the Contract object designed to create a new Event when the contract status is updated to 'Activated'.
However, the Events do not seem to be created by my trigger. I cannot pinpoint the reason why these Events are not being properly created.
Here is the error generated by my test class: System.AssertException: Assertion Failed: Expected: 0, Actual: 4
Can anyone help?
Trigger code:
trigger StaggeredEventCreation on Contract(before update){
//All contract records which activated the trigger
List<Contract> c = Trigger.new;
//Only update those records which had a status change to 'Activated'
for (Contract temp : c){
if (temp.Status == 'Activated' && temp.Status != Trigger.oldMap.get(temp.ID).Status){
Event e1 = new Event(OwnerId = '',
StartDateTime = Datetime.Now(), EndDateTime = Datetime.Now() + 7, Subject = 'Initial Followup');
Event e2 = new Event(OwnerId = '',
StartDateTime = Datetime.Now() + 30, EndDateTime = Datetime.Now() + 37, Subject = '30-Day Followup');
Event e3 = new Event(OwnerId = '',
StartDateTime = Datetime.Now() + 60, EndDateTime = Datetime.Now() + 67, Subject = '60-Day Followup');
Event e4 = new Event(OwnerId = '',
StartDateTime = Datetime.Now() + 90, EndDateTime = Datetime.Now() + 97, Subject = '90-Day Followup');
}
}
Apex test class code:
@isTest
private class StaggeredEventCreationTest{
@isTest static void validateStaggeredEventCreation(){
Contract c = new Contract(AccountId = '', StartDate = Date.today(),
ContractTerm = 12, Status = 'Draft');
insert c;
c.Status = 'Activated';
update c;
Integer eventCount = [SELECT count() FROM Event];
System.assertEquals(eventCount, 4);
}
}
There is a very little mistake in your code i.e. you have done everything very well but just forget to insert the events that you have created.
I have updated your code so please change your trigger with below code :
Please let me know if you still have any issue with above code or you want more help on this.
Thanks,
Abhishek Bansal.