+ Start a Discussion
Steve CairneySteve Cairney 

Code Coverage Help

Hi all, I wrote a trigger and test class bit it's only hitting 50% can anyone help with what's missing?

 

Trigger:

 

trigger changeLeadOwner on Lead (before insert, before update) {
 	  
 	      id ProfileId = userinfo.getProfileId();
 	      profile StandardProfile = [select id from profile where name='Admin User'];
 	      
 	      if (ProfileId == StandardProfile.Id) {
 	  
 	        // no bulk processing; will only run from the UI
 	          if (Trigger.new.size() == 1) {
 	              for (Lead lead :trigger.new) {
 	                  lead.Ownerid =lead.Phone_Lead_Owner__c;
 	              }
 	          }
 	  
 	      }
 	  
 	  }

 Here's the Class:

 

@isTest
public class TestchangeLeadOwner {
     // Methods for testing 
   @isTest static void test1() {
   
         profile StandardProfile = [select id from profile where name='Admin User'];
         
         User usrObj = [select id from User LIMIT 1];

         Lead ld = new Lead(Lastname='testLead', Company='companyTest' );

         ld.Phone_Lead_Owner__c = usrObj.id;

         insert ld;

         update ld;

    }

}

 Here's that the test result is highlighting red

 

if (Trigger.new.size() == 1) {  10    for (Lead lead :trigger.new) {  11    lead.Ownerid =lead.Phone_Lead_Owner__c;

 

 

Any help and tips would be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi Steve,

 

If you see your test code, you'll notice that you have queried the Admin User profileId, however the user you're querying is any random user. 

 

profile StandardProfile = [select id from profile where name='Admin User'];
         
         User usrObj = [select id from User LIMIT 1];

 And as per your trigger code,

id ProfileId = userinfo.getProfileId();
 	      profile StandardProfile = [select id from profile where name='Admin User'];
 	      
 	      if (ProfileId == StandardProfile.Id) {

 the user has to be someone with that profile.

 

So change your test code query and add a clause: WHERE ProfileId = :StandardProfile.Id

 Then use a system.runAs(usrObj) so that the code runs as the Admin User. This should do the trick.

 

 

profile StandardProfile = [select id from profile where name='Admin User'];
         
// Make sure there's a Profile with name "Admin User", else the below query will throw an exception         
User usrObj = [select id from User WHERE ProfileId = :StandardProfile.Id LIMIT 1];

// Now you have a user who's the ADMIN USER. Run the code as that user. Again ensure there's a user //with this profile

system.runAs(usrObj){
Lead ld = new Lead(Lastname='testLead', Company='companyTest' );
ld.Phone_Lead_Owner__c = usrObj.id;
insert ld;
}

 This should work. Let me know if any questions :-)

All Answers

hitesh90hitesh90

Hi Steve,

 

It's because of current user's profile is not 'Admin User' it's ''System Administrator'.

 

Try to use below soql query.

profile StandardProfile = [select id from profile where name='System Administrator'];

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

Steve CairneySteve Cairney

Hi, that hasn't improved the coverage I'm afraid.

 

I cleared the code coverage and removed the test results prior to re-testing...

vishal@forcevishal@force

Hi Steve,

 

If you see your test code, you'll notice that you have queried the Admin User profileId, however the user you're querying is any random user. 

 

profile StandardProfile = [select id from profile where name='Admin User'];
         
         User usrObj = [select id from User LIMIT 1];

 And as per your trigger code,

id ProfileId = userinfo.getProfileId();
 	      profile StandardProfile = [select id from profile where name='Admin User'];
 	      
 	      if (ProfileId == StandardProfile.Id) {

 the user has to be someone with that profile.

 

So change your test code query and add a clause: WHERE ProfileId = :StandardProfile.Id

 Then use a system.runAs(usrObj) so that the code runs as the Admin User. This should do the trick.

 

 

profile StandardProfile = [select id from profile where name='Admin User'];
         
// Make sure there's a Profile with name "Admin User", else the below query will throw an exception         
User usrObj = [select id from User WHERE ProfileId = :StandardProfile.Id LIMIT 1];

// Now you have a user who's the ADMIN USER. Run the code as that user. Again ensure there's a user //with this profile

system.runAs(usrObj){
Lead ld = new Lead(Lastname='testLead', Company='companyTest' );
ld.Phone_Lead_Owner__c = usrObj.id;
insert ld;
}

 This should work. Let me know if any questions :-)

This was selected as the best answer
Steve CairneySteve Cairney

Thanks, can you help me with the syntax?

 

@isTest
public class TestchangeLeadOwner {
     // Methods for testing 
   @isTest static void test1() {
   
         profile StandardProfile = [select id from profile where name='Admin User'];
         
         User usrObj = [select id from User LIMIT 1];
         
         WHERE ProfileId = StandardProfile.Id
         
         Lead ld = new Lead(Lastname='testLead', Company='companyTest' );

         ld.Phone_Lead_Owner__c = usrObj.id;

         insert ld;

         update ld;

    }
    
}

 

Steve CairneySteve Cairney

Hey, just seen your updated code, thanks that works great with 100% coverage

 

HOWEVER!

 

Seems it's knocked one of my other triggers coverage down to 45% I don't suppose you know how that could be happening do you?

 


Edit: Nevermind! User error ;)

vishal@forcevishal@force

Happens! :D

 

Glad it helped!