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
iswarya sekar 7iswarya sekar 7 

Hiii everyone!! I need to write test class for the apex trigger. Anyone Please Help me!!

trigger UpdateStage on Account (after insert, after update, before insert, before Update) {   // Performs Custom actions after Record gets saved.
    DisableTriggers__c ab = DisableTriggers__c.getValues('UpdateStage');
  if(ab.activate_Trigger__c ==TRUE){
    List<Id> accountId = new List<Id>(); // get the list of all Account's ID to be Updated.
    
    for(Account acc : Trigger.new)
    {
        if(acc.All_Opportunities_Won__c==True)   // Checks if the Condition is True. 
            accountId.add(acc.Id);
    }
    
    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    
    for(Opportunity opp : [select id, StageName, Amount from Opportunity where AccountId in: accountId AND Amount != 0]) // used to get all records from above ID.
    {
        opp.StageName='Closed-Won';   //Update StageName in Opportunity
        oppsToUpdate.add(opp); 
    }
    
    update oppsToUpdate;   // Update all Opportunity in List.
}
}

 
Best Answer chosen by iswarya sekar 7
v varaprasadv varaprasad
Hi Iswarya,

Please check once below code : 
 
@istest
Public class UpdateStage_Test {

 public static testmethod Testtrigger() {
  Account Acc = new Account(
   Phone = '(014) 427-4427', // Phone
   AccountNumber = 'CC213425', // Account Number
   Website = 'http://www.pyramid.com', // Website
   All_Opportunities_Won__c = true
  );
  insert Acc;

  Opportunity Opp = new Opportunity(
   Amount = 100000.00, // Amount 
   Name = 'Pyramid Emergency Generators', // Opportunity Name  
   Account = acc.id, // Account Name
   StageName = 'Prospecting' // Stage
  );
  insert Opp;
 }
}

Hope it helps you.

Thanks
Varaprasad




 

All Answers

v varaprasadv varaprasad
Hi Iswarya,

Please check once below code : 
 
@istest
Public class UpdateStage_Test {

 public static testmethod Testtrigger() {
  Account Acc = new Account(
   Phone = '(014) 427-4427', // Phone
   AccountNumber = 'CC213425', // Account Number
   Website = 'http://www.pyramid.com', // Website
   All_Opportunities_Won__c = true
  );
  insert Acc;

  Opportunity Opp = new Opportunity(
   Amount = 100000.00, // Amount 
   Name = 'Pyramid Emergency Generators', // Opportunity Name  
   Account = acc.id, // Account Name
   StageName = 'Prospecting' // Stage
  );
  insert Opp;
 }
}

Hope it helps you.

Thanks
Varaprasad




 
This was selected as the best answer
v varaprasadv varaprasad
 Use  public static testmethod void Testtrigger(){ instead of  public static testmethod Testtrigger() {


 
iswarya sekar 7iswarya sekar 7
Hii Varaprasad,

Im getting the error as "  Illegal assignment from Id to Account "
v varaprasadv varaprasad
Use below one Instead of Account = acc.id, // Account Name
Accountid = acc.id,
iswarya sekar 7iswarya sekar 7
Its worked fine !! Thank you !!