You need to sign in to do that
Don't have an account?
Update Account data from event (@isTest)
Hello,
I am trying to write the test class for my trigger. But I can't get further then 60%.
Can someone help me with this?
trigger updateAcct onEvent (afterupdate) {
Set<Id> acctIds = new Set<Id>();
for(Event e: [SELECT Subject,WhatId, Sales_Reps__c FROM EVENT WHERE ID IN:trigger.new]){
String wId = e.WhatId;
if(wId!=null && wId.startsWith('001') && !acctIds.contains(e.WhatId) && e.Sales_Reps__c == '1-5'){
acctIds.add(e.WhatId);
}
}
for(Account a:[SELECT Sales_Reps__c FROM Account WHERE ID IN:acctIds]){
a.Sales_Reps__c = '1-5';
update a;
}
}
Test class:
public class TestClass {
static testMethod void myUnitTest() {
//Create new Account
Account acc = new Account(Name ='test');
Set<Id> acctIds = new Set<Id>();
insert acc;
// Create new Event with fiction data
Event c = new Event(WhatId=acc.ID, Subject='test',Sales_Reps__c='1-5',durationinminutes = 1440,isalldayevent = true,activitydate = System.today());
Set<Id> ownerIds = new Set<Id>();
String wId = c.WhatId;
if(c.Update_Event__c ==true){
acctIds.add(c.WhatId);
}
insert c;
Test.startTest();
try {
Account a = [SELECT Name, Sales_Reps__c,Sales_LY__c, Description FROM Account WHERE ID =:acc.Id];
a.Name='test';
a.Sales_Reps__c ='1-5';
a.Sales_LY__c = '1.750.000 - 2.000.000';
a.Description='test';
update a;
}
catch (System.DmlException e) {
System.debug('Event record could not be updated.');
}
Test.stopTest();
}
}
Looking forward to the answer!
Kind regards,
Hi,
With in your test method just insert one Account and one Event record. and pass that Account id into the Event insert operation.
for eg:
Account a = new Account(Name = 'Test');
insert a;
Event e = new Event(WhatId=a.ID, Subject='test', Sales_Reps__c='1-5', durationinminutes = 1440, isalldayevent = true, activitydate = System.today());
insert a;
the event records need to satiesfty your IF condition in the trigger.
Thanks for your help but it I still stuck on 60%.
Can someone help me?