function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ToddKruseToddKruse 

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 

AvromAvrom

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).

 

 

ToddKruseToddKruse
Thanks for the link, but the section on custom settings seems to deal with the creation/access of information you created via Custom Settings in Setup.  I haven't created any values, just created views within the page that is linked to a custom object.
AvromAvrom

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().

 

ToddKruseToddKruse

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 

AvromAvrom

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.

 

 

Message Edited by Avrom on 01-11-2010 01:40 PM
ToddKruseToddKruse

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

 

AvromAvrom

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.

 

ToddKruseToddKruse

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

 

AvromAvrom

Can you be a bit more specific about "blew up on"? The specific code for the loop and error message would help.

 

ToddKruseToddKruse

the loop statement was this:

 

  for (SelectOption a : result)

{

system.debug('here');

}

 

and the error is this:

An internal server error has occurred
An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience. 

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. 

AvromAvrom

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

 

Message Edited by Avrom on 01-13-2010 04:52 PM