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
Vidya H 4Vidya H 4 

hi can anyone help me to write test class for this code

trigger AccountStage on Account (After Update) {
    
    Set<Id> accountIds = new Set<Id>();

    for(Account ac : Trigger.new)
    {
         if(ac.Stage__c =='Closed')
              accountIds.add(ac.Id);
    }

     List<Opportunity> oppsToUpdate = new List<Opportunity>();

     for(Opportunity opp : [select id, StageName from Opportunity where AccountId in: accountIds])
     {
          opp.StageName='Closed - won';
          opp.closeDate=Date.today();
          oppsToUpdate.add(opp);
     }

     update oppsToUpdate;

}
Best Answer chosen by Vidya H 4
CharuDuttCharuDutt
Hii Vidya
Try Below Code
@isTest
public class AccountStage Test{
    @isTest 
    public Static Void UnitTest(){
        Account Ac =new Account();
        Acc.Name = 'Account test';
        Insert Acc;
        
        
        Opportunity opp = new Opportunity();
        opp.Name = 'test opp';
        opp.Stagename = 'Prospect';
        opp.CloseDate = System.toda()-5;
        opp.AccountId = Acc.Id;
        insert Opp;
        Acc.Stage__c = 'Closed';
        update Acc;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!