Hiii Madan, For writing test class we have to perform the actions that fire the trigger and verify expected results. check out this Example
trigger DmlTriggerNotBulk on Account(after update) {
// Get the related opportunities for the accounts in this trigger.
List<Opportunity> relatedOpps = [SELECT Id,Name,Probability FROM Opportunity
WHERE AccountId IN :Trigger.New];
// Iterate over the related opportunities
for(Opportunity opp : relatedOpps) {
// Update the description when probability is greater
// than 50% but less than 100%
if ((opp.Probability >= 50) && (opp.Probability < 100)) {
opp.Description = 'New description for opportunity.';
// Update once for each opportunity -- not efficient!
update opp;
}
}
}
Test class for this trigger
trigger DmlTriggerBulk on Account(after update) {
// Get the related opportunities for the accounts in this trigger.
List<Opportunity> relatedOpps = [SELECT Id,Name,Probability FROM Opportunity
WHERE AccountId IN :Trigger.New];
List<Opportunity> oppsToUpdate = new List<Opportunity>();
// Iterate over the related opportunities
for(Opportunity opp : relatedOpps) {
// Update the description when probability is greater
// than 50% but less than 100%
if ((opp.Probability >= 50) && (opp.Probability < 100)) {
opp.Description = 'New description for opportunity.';
oppsToUpdate.add(opp);
}
}
// Perform DML on a collection
update oppsToUpdate;
}
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
For writing test class we have to perform the actions that fire the trigger and verify expected results.
check out this Example
Test class for this trigger
Greetings to you!
Please refer to the below links which might help you further with the above requirement.
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers
https://mindmajix.com/test-class-with-example-in-salesforce
https://salesforce.stackexchange.com/questions/10988/how-to-write-a-unit-test-test-class-for-trigger
http://www.sfdcpoint.com/salesforce/test-class-with-example-salesforce/
https://www.salesforcetutorial.com/write-test-class-for-trigger-simpleexample/
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas