You need to sign in to do that
Don't have an account?
Steve 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.
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.
And as per your trigger code,
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.
This should work. Let me know if any questions :-)
All Answers
Hi Steve,
It's because of current user's profile is not 'Admin User' it's ''System Administrator'.
Try to use below soql query.
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
Hi, that hasn't improved the coverage I'm afraid.
I cleared the code coverage and removed the test results prior to re-testing...
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.
And as per your trigger code,
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.
This should work. Let me know if any questions :-)
Thanks, can you help me with the syntax?
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 ;)Happens! :D
Glad it helped!