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 

Test Class to get 80% code Coverage.. Anyone help me!!

trigger UpdateStage on Account (after insert, after update, before insert, before Update) {   // Performs Custom actions after Record gets saved.
   
    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
Amit Chaudhary 8Amit Chaudhary 8
Try to update your Trigger like below
trigger UpdateStage on Account (after insert, after update, before insert, before Update) {   // Performs Custom actions after Record gets saved.
   
    if(Trigger.isAfter){
		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';
			oppsToUpdate.add(opp); 
		}
		
		if(oppsToUpdate.size() > 0 ){
			update oppsToUpdate;
		}	
	}
}

Then try below test class.
@isTest
Public Class UpdateStageTest{

	@isTest
	static void myunitTest()
	{
	
		Account acc = new Account();
		acc.name = 'test';
		acc.Phone = '1111';
		acc.All_Opportunities_Won__c = false;
		// Add all required field
		insert acc;
		
		
		Opportunity opp = new Opportunity();
		opp.name = 'test opp';
		opp.stagename = 'Lead';
		opp.amount = 12;
		opp.closedate = system.today();
		opp.accountid = acc.id
		//Include all the required field values.
		insert opp;

		
		Test.StartTest();
		
			acc.All_Opportunities_Won__c = true;
			update acc;
		
		Test.StopTest();
	}
}

Let us know if this will help you
 

All Answers

Gokula KrishnanGokula Krishnan
.Hi Iswarya,

Code coverage for Trigger is to insert/update/delete test records in the test class.  Trigger will be fired whenever an Insert/Update/Delete event occurs. So, to test the trigger code, need to perform insert/update/delete operations.

In your Trigger, you had written only for Insert and Update event. So you need to perform Insert and Update operations.

Try this,
@isTest
Public Class UpdateStageTest{

	@isTest
	Static void Meth(){
		Test.StartTest();
		
		Account acc = new Account();
		acc.name = 'test';
		acc.Phone = '1111';
		//Include all the required fields values.
		insert acc;
		
		Opportunity opp = new Opportunity();
		opp.name = 'test opp';
		opp.stagename = 'Lead';
		opp.amount = 12;
		opp.closedate = system.today();
		//Include all the required field values.
		insert opp;
		
		acc.All_Opportunities_Won__c = true;
		update acc;
		
		Test.StopTest();
		
	}

}

Thanks.

If it helps you, please mark is as best answer, so it will be helpful for other developers.
iswarya sekar 7iswarya sekar 7
Hii Gokul,
 This is the error i'm getting when running the test,
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateStage: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.UpdateStage: line 3, column 1: []
YogeshMoreYogeshMore
Hi,

Can you try following code.
 
@isTest
Public Class UpdateStage_Test{

	@isTest
	Static void TestMethod1(){
		Test.StartTest();
		
		Account acc = new Account();
		acc.name = 'TestAcc';
		acc.Phone = '1234';
		acc.All_Opportunities_Won__c = true;
		insert acc;
		
		Opportunity opp = new Opportunity();
		opp.name = 'test opp';
		opp.stagename = 'Lead';
		opp.amount = 55;
		opp.closedate = system.today();
		insert opp;		
		Test.StopTest();		
	}
}

Please mark above answer as SOLVED and BEST ANSWER if it helps you.

Regards,
Yogesh More
Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || yogeshsfdc11@gmail.com
www.yogeshmore.com || Skype:-yogesh.more44
Gokula KrishnanGokula Krishnan
Hi Iswarya,

You need to change your trigger code:

Line 1 as : trigger UpdateStage on Account (after insert, after update) { 

(or)
 
trigger UpdateStage on Account (after insert, after update, before insert, before Update) {   // Performs Custom actions after Record gets saved.
   
    if(Trigger.isAfter){
		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.

Thanks,
iswarya sekar 7iswarya sekar 7
Still. i'm getting the same error..
Amit Chaudhary 8Amit Chaudhary 8
Try to update your Trigger like below
trigger UpdateStage on Account (after insert, after update, before insert, before Update) {   // Performs Custom actions after Record gets saved.
   
    if(Trigger.isAfter){
		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';
			oppsToUpdate.add(opp); 
		}
		
		if(oppsToUpdate.size() > 0 ){
			update oppsToUpdate;
		}	
	}
}

Then try below test class.
@isTest
Public Class UpdateStageTest{

	@isTest
	static void myunitTest()
	{
	
		Account acc = new Account();
		acc.name = 'test';
		acc.Phone = '1111';
		acc.All_Opportunities_Won__c = false;
		// Add all required field
		insert acc;
		
		
		Opportunity opp = new Opportunity();
		opp.name = 'test opp';
		opp.stagename = 'Lead';
		opp.amount = 12;
		opp.closedate = system.today();
		opp.accountid = acc.id
		//Include all the required field values.
		insert opp;

		
		Test.StartTest();
		
			acc.All_Opportunities_Won__c = true;
			update acc;
		
		Test.StopTest();
	}
}

Let us know if this will help you
 
This was selected as the best answer
iswarya sekar 7iswarya sekar 7
Thank you Amit !! Thats worked fine for me. I got 100% code Coverage.