You need to sign in to do that
Don't have an account?
Trying to dynamically assign the VF page based on a profile name
Good afternoon everyone. I have a question regarding the redirection of a page via a VisualForce page. If the user has a certain user right, I want the view on my object to default to a certain view. I have this working with this code:
<apex:Page tabStyle="SFDC_Projects__c" >
<script src="/soap/ajax/15.0/connection.js"></script>
<script type="text/javascript" />
<script>
window.onload = function() {
sforce.connection.sessionId = '{!$Api.Session_ID}';
var describeSObjectResult = sforce.connection.describeSObject("sfdc_projects__c");
var prefix = describeSObjectResult.keyPrefix;
// Determine the View based on the Role of the User
var cView;
switch ( "{!$Profile.Name}" )
{
case "Program Manager":
cView = "00BT0000001WNN8";
break ;
case "System Administrator":
cView = "00BT0000001WNN8";
break;
default:
cView = "00BT0000001WUWh";
break;
}
// Change the whole window to point to this location
parent.document.location.href = "/" + prefix + "?fcf=" + cView ;
}
</script>
</apex:page>
however, when I switch environments, the value i am assigning in the cView = ... is different in each environment. Is there a way to capture this value dynamically in the environment the code is located?
Thanks
You can capture cutom information in profiles using custom settings. Do a search for "custom settings" in the Winter 10 release notes, at
http://na6.salesforce.com/help/doc/en/salesforce_winter10_release_notes.pdf
You could store view information there, and access it using a VF expression (as the release notes explain).
Sorry about my lack of understanding...so the idea is, you want to be able to get the view ID based on, say the view name? If that's right, worst case scenario you can create a StandardSetController in Apex and then use the method StandardSetController.getListViewOptions(). This gives you an array of System.SelectOption instances, which you can cycle through, checking getLabel() on each for the name you want and retrieving the corresponding getValue().
Thanks for the help in pointing me to StandardSetController. Do you know of any examples that deal with this inside of a javascript script?
Honestly, this is screwy. I should just be able to look up in a table or something the id value for a list view based on the name.....but I can't find any table with that information.
--Todd Kruse
Hi Todd,
I'm looking into a more "natural" way to do this. In terms of a code sample, although I don't have one specifically along these lines, I'm actually not suggesting doing the loop in Javascript. Rather, you could create a custom Apex controller which instantiates the standard set controller and cycles through it, and then reference getter methods you define in it:
switch ( "{!$Profile.Name}" ) { case "Program Manager": cView = "{!programManagerViewId}"; break ; case "System Administrator": cView = "{!administratorViewId}"; break; default: cView = "{!defaultViewId}"; break; }
The expressions here refer to getters in the controller: getProgramManagerViewId(), etc. The Javascript will be generated with literals for the viewIDs, but this will happen at runtime.
Hope this helps,
Avrom
Edit: On further investigation, it seems that this is indeed probably the best way to go--there's no direct way to query views (AKA filters) by name.
Thank you for the help with this subject. I am still having a hard time wrapping my head around how I actually go about capturing the view id if its not actually stored in a table somewhere.
--Todd Kruse
The view id is stored somewhere, it's just not directly queryable.
You do need to query something--either explicitly or by just making your controller an extension of the standard set controller. But that thing isn't views--it's sfdc_projects__c. Once you have a standard set controller (in Apex) for sfdc_projects__c, you can call getListViewOptions() on it--*that* (effectively) queries the view ids, and returns an array of name/value pairs that will let you look up the id value by name.
I put this logic into an apex class:
List<SFDC_Projects__c> tstProj = [SELECT ID, Name FROM SFDC_Projects__c LIMIT 10];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController( tstProj);
List<SelectOption> result = ssc.getListViewOptions();
when I throw a system.debug(result); I get this:
[Lcommon.apex.runtime.impl.ApexSelectOptionValue;@cdefd2
I tried then doing a for loop for (SelectOption a : result) and it blew up on me.
--Todd
Can you be a bit more specific about "blew up on"? The specific code for the loop and error message would help.
the loop statement was this:
for (SelectOption a : result)
{
system.debug('here');
}
and the error is this:
Thank you again for your patience and assistance. And thanks for using salesforce.com!
Error ID: 385549022-102272 (-1365269522)
I never did see any 'here' statements in the debug logs either which leads me to believe that it never even hit inside the loop.
Hi Todd,
We've logged a bug for this issue and are looking into it. I'll let you know as soon as I have more information.
Best,
Avrom
Edit: This bug will be fixed in the upcoming Spring 10 release. In the meantime, there's a workaround: don't use a collection for loop; use an incrementing for loop, like this:
Integer size = result.size(); for (Integer i=0; i<size; i++) { SelectOption currentOption = result[i]; ... }
Sorry about the inconvenience.
Best,
Avrom