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
Dipa87Dipa87 

How to Override Opportunity View Link with a VF page for a certain profile?

I have a requirement where I need to override the Opportunity view and to redirect the user of a certain profile to a VF page instead of standard detail page.


I tried something like this:

Created a VF page. Added below script inside it.

<apex:page standardController="Opportunity">
<script>
    if('{!$Profile.Name}' == 'Manager Profile'){
        window.open("/apex/mypage?id={!Opportunity.Id},'_parent');
    }
    else{
        window.open("/{!Opportunity.Id}",'_parent');
    }
</script>
</apex:page>

and in Links , I override the view link of Opportunity with this VF page.

But it works for only manager profile but not for other profiles. The page keeps on loading and loading for other profiles except the manager Profile.

Any idea?
             

Best Answer chosen by Admin (Salesforce Developers) 
Dipa87Dipa87

I got the solution. to stop recursion we have to redirect to the dafault detail page like this :

 

        window.open("/{!Opportunity.Id}?nooverride=1",'_parent');

All Answers

Maros SitkoMaros Sitko

When user has different profile, he is redirected to your opportunity view page again, so it is circular. You need create new page for other user, and redirect them to it. In this new page, use apex:detail to show them whole page layout of opportunity.

 

<apex:page standardController="Opportunity">
<script>
    if('{!$Profile.Name}' == 'Manager Profile'){
        window.open("/apex/mypage?id={!Opportunity.Id},'_parent');
    }
    else{
        window.open("/apex/newpage?id={!Opportunity.Id}",'_parent');
    }
</script>
</apex:page>

 

 

Dipa87Dipa87

I got the solution. to stop recursion we have to redirect to the dafault detail page like this :

 

        window.open("/{!Opportunity.Id}?nooverride=1",'_parent');

This was selected as the best answer
Peter_sfdcPeter_sfdc

Is there a reason you are not just overriding the opportunity view action with this Javascript? 

 

I would always avoid hard coding a profile into a Visualforce page. Profile names can change over time. Profiles can be added/removed. Make sure to put appropriate change management processes into place to avoid unexpected behaviors later. 

 

If I had to hard code the profile into the page, I would not do it as a redirect, I would simply make some conditionally rendered sections of the page and make that based on profile. 

 

<!-- section for manager profile -->
<apex:pageBlock ... rendered="{!$Profile.Name = 'Manager Profile'}">
...
</apex:pageBlock>

<!-- section for other profile -->
<apex:pageBlock ... rendered="{!$Profile.Name = 'Other Profile'}">
...
</apex:pageBlock>

 

Dipa87Dipa87
Peter, we are using thi sVF page to redirect it to some other page. Not to show something in the VF page. We will just keep the condition check in the VF page.

We are storing the profile ids in custom label and using them.