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
sf ostsf ost 

Test class for before and after update trigger

Hi all,

I am unable to get the code coverage for below trigger. All lines are covered except 8 and 9 lines.

Scinario: Only once record can be Active for a User. Leader__c is in Lookup-Relationship with User. Also if I update one record "Active__c = True" remaining records of that User should be updated (Active__c = False).
trigger OneRecordCanBeActive on Leader__c(after insert, before update) {

	if(checkRecursive.runOnce()){
		List<Leader__c> lList = new List<Leader__c>();
		List<Leader__c> gAccs = [select Id, Active__c, User__c from Leader__c where Id not in :Trigger.new and Active__c = True and User__c = :UserInfo.getUserId()];
			
			for(Leader__c l : gAccs){
				l.Active__c = False;
				lList.add(l);
			}
			update lList;
	}
}

My Test Class:
@isTest
private class OneRecordCanBeActive_Test {

	private static testMethod void test() {
	    
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        User u = new User();
            u.Alias = 'SysAdmin';
            u.Email='sysAdmin@testorg.com';
            u.EmailEncodingKey='UTF-8';
            u.LastName='Testing';
            u.LanguageLocaleKey='en_US';
            u.LocaleSidKey='en_US';
            u.ProfileId = p.Id;
            u.TimeZoneSidKey='America/Los_Angeles';
            u.UserName='gaccuser@testorg.com';
        Insert u;
            
        Leader__c l1 = new Leader__c();
            l1.Active__c = True ;
            l1.User__c = u.id;
        Insert l1;
        
        Leader__c l2 = new Leader__c();
            l2.Active__c = True ;
            l2.User__c = u.id;
        Insert l2;
		
        l1.Active__c = True ;
        Update l1;
	}

}

Thankyou.
Stephen Piercey 5Stephen Piercey 5
You need to use System.runAs() OR update your query. 

'and User__c = :UserInfo.getUserId()' Will check that the Leader__c.User__c is the user is the one running the code - not a user that already exists as a Leader.

I think what you are looking for is something like this instead:
 
Set<Id> UserIds = new Set<Id>();
for(Leader__c l : trigger.new){
    UserIds.add(l.User__c);
}

List<Leader__c> gAccs = [select Id, Active__c, User__c from Leader__c where Id not in :Trigger.new and Active__c = True and User__c in :UserIds];


This would also make the test work (I'm not sure it's exactly what you want though):
 
Leader__c l1 = new Leader__c();
            l1.Active__c = True ;
            l1.User__c = u.id;
        Insert l1;
        
    System.runAs(u){
        Leader__c l2 = new Leader__c();
            l2.Active__c = True ;
            l2.User__c = u.id;
        Insert l2;
		
        l1.Active__c = True ;
        Update l1;
    }

 
sf ostsf ost
Hi Stephen Piercey,

Thanks for your reply.

I updated my test class. Still the lines 8 and 9 of trigger remian uncovered. Provide me me some solution.
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class.
@isTest
private class OneRecordCanBeActive_Test {

	private static testMethod void test() 
	{
            
        Leader__c l1 = new Leader__c();
            l1.Active__c = True ;
            l1.User__c = UserInfo.getUserId();
        Insert l1;
        
		Test.StartTest();
		
        Leader__c l2 = new Leader__c();
            l2.Active__c = True ;
            l2.User__c = UserInfo.getUserId();
        Insert l2;

		Test.StopTest();
		
	}

}

Let us know if this will help you