function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Test class for a trigger

Hi All


Can you please help me in writing a test class for the following trigger. The scenario is whenever an opportunity is closed won or dead/lost a field should be updated in Case object.


trigger CaseUpadate on Opportunity (after update)
List <Case> caseList;
List <Id> oppIdList = new List <Id>();
for(Integer i=0; i<; i++)
    if ([i].StageName != Trigger.old[i].StageName && ([i].StageName == 'Closed Won' ||[i].StageName == 'Closed - Dead/Lost'))
caseList = [select Id, Opportunity_closed_won__c from Case where Opportunity__c in :oppIdList];
if (caseList.size() > 0)
    for(Case c : caseList)
        if (c.Opportunity_closed_won__c != true)
            c.Opportunity_closed_won__c = true;   
    update caseList;



private class TestTrigger {

    Opportunity o = new Opportunity (Name = 'Test', CloseDate=System.Today(), StageName = 'test');
    //add whatever other fields are required to save opportunity
    insert o;
     Case c = new Case (// add whatever fields are required to save it);
     insert c;

     o.stagename = 'Closed Won';
     update o;