Redirect to opportunity edit page

I have 2 user profiles: internal user and partner user (partner portal). I want to have separate behaviours when each user clicks on the Opportunity New button, to create a new opportunity:

  • internal user clicks on Opportunity New button – redirect to a custom visualforce page;
  • partner user clicks on Opportunity New button – redirect to a custom the standard opportunity edit page;

I have:

  1. replaced the standard “New” button with my custom VF page;
  2. in the VF page I’ve added an action to check and redirect to the correct page;
  3. in the controller, the method checks the context user and if it’s a partner user, redirects to the standard opportunity edit page.

This works fine for internal user; for the partner user, it does redirect to the standard page but, for some reason, it gets into some kind of loop and the page never actually loads.

I wonder if the link I am redirecting to is correct or if there’s something I am doing wrong.

Here’s a snippet of the code:

VF Page:

<apex:page standardController="Opportunity" extensions="OpportunityController" tabStyle="Opportunity" action="{!redirectForPartner}">

</apex:page>

Controller:

public PageReference redirectForPartner(){

    User user;
    Id userId = UserInfo.getUserId();

    if(userId != null){
        user = [Select Id, Name, Email, Username, ProfileId, Profile.Name From User Where Id =: userId];
    }

    if(user.Profile.Name.equals(PARTNER_PROFILE)){

        PageReference pr = new PageReference('/partner/006/e?retURL=%2Fpartner%2F006%2Fo');
        return pr;

    }

    return null;

}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could have done all of this without any apex code at all:

<apex:page standardController="Opportunity" 
  action="{!IF($Profile.Name=$Label.Partner_Profile_Name, 
    URLFOR($Action.Opportunity.New, null, null, true),
    URLFOR($Page.NewOpportunity, null, null)}"></apex:page>

I’d recommend having the custom Visualforce page be separate so that the redirect is on its own page. It’s a little easier to manage this way.

Method 2

You need to stop the override by using the nooverride parameter.

Change as follows:

if(user.Profile.Name.equals(PARTNER_PROFILE)){

    PageReference pr = new PageReference('/partner/006/e?retURL=%2Fpartner%2F006%2Fo&nooverried=1');
    return pr;

}

Method 3

Ok, so with the help of @sfdcfox and @Eric, I think I got the answer. I had to tweak the action a little bit.

2 things I had to change:

1) I needed to add a retURL to this URLFOR($Action.Opportunity.New, null, null, true) because the Cancel button on the standard page wasn’t doing anything.

2) I replaced this URLFOR($Page.NewOpportunity, null, null)})} for null, because I was getting one of those strange Internal Errors from Salesforce. I think because the page was calling itself, it might’ve entered some kind of loop.

I ended up with this:

action="{!IF($Profile.Id==PARTNER_PROFILE_ID, URLFOR($Action.Opportunity.New, null, [retURL='/006/o'], true), null)}"

It looks like it’s working 🙂

Cheers


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x