You need to sign in to do that
Don't have an account?

Assistance with my Test Class
Hi Experts,
I am somewhat apex newb, I need a second eye with my test class and codes. As I am only getting 72% code coverage and I am not sure if I wrote it correctly.
Goal: Redirect the page layout to VF (Displaying 4 fields only) IF the Lead Owner is not equal to Current Running User
VF 1:
VF 2:
Controller:
TEST CLASS:
Any assistance with arranging the framework is greatly appreciated.
Thank you.
I am somewhat apex newb, I need a second eye with my test class and codes. As I am only getting 72% code coverage and I am not sure if I wrote it correctly.
Goal: Redirect the page layout to VF (Displaying 4 fields only) IF the Lead Owner is not equal to Current Running User
VF 1:
<apex:page standardController="Lead" extensions="LeadViewRestriction" action="{!nullValue(redir.url, urlFor($Action.Lead.View, Lead.id, null, true))}"> </apex:page>
VF 2:
<apex:page standardController="Lead"> <apex:sectionheader title="{!$ObjectType.Lead.label} Detail" subtitle="{!Lead.Company}"/> <apex:pageblock mode="maindetail" title="{!$ObjectType.Lead.label} Detail"> <apex:pageBlockSection title="Lead Information" columns="1"> <apex:outputField title="Lead Owner" value="{!Lead.OwnerId}"/> <apex:outputField title="Company Name" value="{!Lead.Company}"/> <apex:outputField title="Status" value="{!Lead.Lead_Status_NEW__c}"/> <BR></BR> </apex:pageBlockSection> </apex:pageBlock> </apex:page>
Controller:
public class LeadViewRestriction { public LeadViewRestriction (ApexPages.StandardController controller) { this.controller = controller; } public PageReference getRedir() { Lead l = [Select id, OwnerId From Lead Where Id =: ApexPages.currentPage().getParameters().get('id')]; User u = [Select Id, Profile.Name from User where Id =: UserInfo.getUserId()]; PageReference newPage; if (u.Profile.Name == 'VL - Sales Agent' && l.OwnerId != u.Id) { newPage = Page.LeadBasicView; } else { return null; } newPage.getParameters().put('id', l.Id); return newPage.setRedirect(true); } private final ApexPages.StandardController controller; }
TEST CLASS:
@isTest private class TestLeadViewRestriction { @isTest static void testsLeadView(){ Lead l = new Lead(); l.Company= 'Test'; l.LastName = 'Doe'; l.Lead_Status_NEW__c = 'New'; insert l; Test.setCurrentPageReference(new PageReference('Page.LeadBasicView')); System.currentPageReference().getParameters().put('Id', l.Id); ApexPages.StandardController sc = new ApexPages.standardController(l); LeadViewRestriction controller = new LeadViewRestriction (sc); controller.getRedir(); } }
Any assistance with arranging the framework is greatly appreciated.
Thank you.
You will need to create a test profile with the name "VL - Sales Agent' and a test user assigned to that profile. Then you can run the test as that user.
Here's some documentation on the runAs method.
http://https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm
Good luck! Let me know if that helps!
Hi,
I am getting red flags in the Developer Console in Line 14,20,21 of Apex Class.
Thank you.
I am getting an error:
Pass/Fail: Fail
Error Message: System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.LeadViewRestriction.getRedir: line 8, column 1
Class.TestLeadViewRestriction.testsLeadView1: line 45, column 1
I appreciate the help. I am still getting an error:
Pass/Fail: Fail
Error Message: System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.LeadViewRestriction.getRedir: line 8, column 1
Class.TestLeadViewRestriction.testsLeadView1: line 48, column 1
Weird. Still getting the same error as above.
Thank you.