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
calvin_nrcalvin_nr 

Trigger works as expected but doesnot work with test code

Hi All,

 

I have written a validation trigger to make sure that only one record of a custom object has a particular flag turned on for each account.

When I test this trigger directly by trying to create a record that satisfies the trigger condition, I get the error message as expected.

 

So I went ahead and wrote a test class. I am new to apex and this was my first attempt at writing a test method. I read the docmentation and some examples. When I run my test, its 75% complete which is good to move to production but 2 of the most crucial lines are not being tested. I want to understand why this is happening and how  can address it.

 

When I look at the log, the SOQL query whic is a the heart of my trigger's error mechanism does not return any rows. I confirmed that this query with the values given in the test class does return a row and therefore the error must be raised. but for some reason this is not happening.

Please help meunderstand what is wrong here. Thanks.

 

Trigger code:

 

trigger osv_check_default_flag on Collection_Rule__c (before insert, before update) 
{
	//Declarations		
	ID accountID;
	//List of Collection rule objects from the same account
	List <Collection_Rule__c> rules;
	
	for (Collection_Rule__c newRule : Trigger.new) 
	{
		//Get Account of record being inserted
		accountID = newRule.Account__c;
		//Get collection rules from the same Account where the "default" flag is checked.
		rules=[select id from Collection_Rule__c where Default__c=true and Account__c=:accountID];
				
		//If records with the default exist and the current record has its default flag checked, Do not save and raise ERROR!
		if(rules.size()>0 && newRule.Default__c == true)
		{
			newRule.addError('An Account can only have ONE Default Collection Rule');
			break;
		}	 
	  
	 }
	
	

}

 test class:

 

@isTest
private class osv_test_collection_rule_trigger {
	//Test Method
	static testMethod void CollectionRulesDefault_Test() 
	{
		//Prepare Data
		Collection_Rule__c cr = new Collection_Rule__c();
		cr.Name = 'Test Collection Rule';		
		//This is an ID and will need to be changed when moving to production.
		cr.Account__c = '001Z0000004pGIIIA2';
		cr.Default__c = true;
	
		
		//START TEST
    	//test.starttest();
    	
    	/*Try-catch is the better way to test AddError() method
    	//http://boards.developerforce.com/t5/Apex-Code-Development/How-do-I-test-for-an-exception-addError-in-a-Trigger/td-p/145443*/
    	
    	try {    	
    	//Insert record
    		insert cr;   		
    		
    	}
    	catch(Exception e){
    		Boolean expectedExceptionThrown =  e.getMessage().Equals('An Account can only have ONE Default Collection Rule')? true : false;
            System.AssertEquals(expectedExceptionThrown, true);
            } 
    	
    	//test.stoptest();
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Best practice is to create all object required for your test (thus no need to hardcode id's :)

 

so you will need to create an Account and use that ID in place of the hardcoded ID.

 

or if you want to continue the non best practices:

 

use @isTest(seeAllData = True)

All Answers

Starz26Starz26

Best practice is to create all object required for your test (thus no need to hardcode id's :)

 

so you will need to create an Account and use that ID in place of the hardcoded ID.

 

or if you want to continue the non best practices:

 

use @isTest(seeAllData = True)

This was selected as the best answer
calvin_nrcalvin_nr

So is that the reason that the test is failing? Ok I will try creating an account in the test code and see if it works. 

 

Thanks,

Calvin

calvin_nrcalvin_nr

Definiely going with best practices. Thanks for pointing this out.