Reputation: 13
I have a code to the visualforce page where it shows a specific record for specified user. This code is a IF-ELSE scenario assuming there are two specified users nothing more. However, I would like to specify 10 different users for different records. Could anyone help me with that please?
Exisitng code
/************** Visualforce page *****************/
<apex:page controller="myClass" action="{!redirect}" >
</apex:page>
/************* Controller ***********************/
public with sharing class myClass{
public string uId;
public myClass(){
uId = UserInfo.getUserId();
}
public pageReference redirect(){
PageReference pageRef = new PageReference('URL1');
pageRef.setRedirect(true);
if(uId == 'firstOption'){
pageRef.setRedirect(true);
return pageRef;
}else{
pageRef = new PageReference('URL2');
pageRef.setRedirect(true);
return pageRef;
}
}
}
Upvotes: 0
Views: 1017
Reputation: 431
If you are going to have multiple Users accessing the page, then you can create a Hierarchy Custom Setting to store a URL for each User. This way you have a configurable way to maintain your list without having to update/redeploy code.
Start by creating a Custom Setting, for this example we'll call it "User Redirect"(User_Redirect__c
), and make it a Hierarchy type. Give it a custom Text field called "Redirect URL"(Redirect_Url__c
).
Once done click the Manage button and you can then add individual records for Users (and/or Profiles). Make sure to create a default org-wide record too as a catch all.
Now, in your controller you simply need to reference your custom setting.
//the system will automatically grab the record for the user if one exists,
//else a for their profile if a record exists, else the org-default
String url = User_Redirect__c.getInstance().Redirect_Url__c;
PageReference pageRef = new PageReference(url);
//this is implied if redirecting to a different url. you only ever need to set
//this if you are refreshing the current page and want to reset the state
//pageRef.setRedirect(true);
return pageRef;
Upvotes: 0