You need to sign in to do that
Don't have an account?
sapnasolanki
Test Class Coverage for Trigger
This my Trigger code. What should the test class be for this for coverage?
trigger closedWonOppt on Opportunity (after Update)
{
Opportunity opp = trigger.new[0];
String oppId = opp.id;
System.debug('oppId'+oppId);
System.debug('in trigger');
if(opp.StageName == 'Closed Won')
{
System.debug('in if');
ACL__c acl = new ACL__c();
Datetime today = datetime.now();
Integer year = today.year();
Opportunity opp1=[SELECT Name, Account.Name,Account.Id, RecordType.Name FROM Opportunity
where id = :oppId];
acl.Name = opp1.Account.Name + ' - ' + opp1.RecordType.Name + ' - ' + String.valueOf(year);
acl.Opportunity__c = opp1.Id;
acl.account__C = opp1.Account.Id;
// acl.Opportunity__c = opp1.Id;
insert acl;
}
}
//Typically test class is to test boundaries henceforth these should be accountable
// Create an Opportunity instance
// change the opportunity field (opp.stagename ='closed won' from the default)
// once you update and save (commited data will invoke this trigger)
// now we have to check to see if the updates have been made to related account fields
// Check 1 - pull up account for that oppotunities - then pull up field of Account (ac1.Name (system.Assert) ) check if not empty and if not the check the value you assigned
//Code below is the roughly estimate
// this should end test at local level
// Step 2- Test for bulk updates (since production can encounter bulk data more than 1000 hence your test should cover bulk data both trigger logic and test logic
//Find how to write bulk trigger in my website and how write test class for bulk Trigger
Hope this helps at certain level
}
}
All Answers
//Typically test class is to test boundaries henceforth these should be accountable
// Create an Opportunity instance
// change the opportunity field (opp.stagename ='closed won' from the default)
// once you update and save (commited data will invoke this trigger)
// now we have to check to see if the updates have been made to related account fields
// Check 1 - pull up account for that oppotunities - then pull up field of Account (ac1.Name (system.Assert) ) check if not empty and if not the check the value you assigned
//Code below is the roughly estimate
// this should end test at local level
// Step 2- Test for bulk updates (since production can encounter bulk data more than 1000 hence your test should cover bulk data both trigger logic and test logic
//Find how to write bulk trigger in my website and how write test class for bulk Trigger
Hope this helps at certain level
}
}
Find a code example I added here
Usually using for-loop when you create 1000 opportunities and try populating - you gradually have to make 1000 check and henceforth lots of SOQL calls. To test the bulk data, you need to avoid 100 + SOQL calls, so couple of gimmicks(tricks) i posted using standard practices in Salesforce Apex code
Check out one sample at http://www.forcelabs.net/2012/01/salesforce-trigger-to-count-contact.html
(This covers the logic of how to write trigger to handle bulk data (1000 +) and not reaching SOQL governer limit) smooth deploy in production
Thank you! that worked
Nice to hear that, I appreciate if you mark the my reply as solution.