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
PascalPascal 

Page Redirection to either edit or view

Trying to write a controller extension that either sends a user to the edit page or the view page of a campaign based on the regions (custom fields) of the campaign and user.  For example, if the user's region equals the campaign's region, then allow the edit.  Otherwise, prevent the edit of the campaign.

 

Here's thde code I have so far.  Just want to do a simple redirect.  There is an override of the Campaign Edit that calls a VF page (with extension shown below) ... on the VF page, I have an action that calls the method on the class:

 

public class CampaignExtension{
    String recordId;
    Campaign campaign;
    public CampaignExtension(ApexPages.StandardController controller) {
        recordId = controller.getId();
        campaign = [select Region__c from campaign where id = :recordId];
    }
   
    public PageReference redirect(){
         
        User user = new User();
        user = [select Region__c from User where id =:UserInfo.getUserId()];
              
        if (user.Region__c == campaign.Region__c){
            return null;
        }
        else{
            return null;
        }
    }
}

jwetzlerjwetzler
store off the controller in your class and return controller.view() or controller.edit();
PascalPascal

Thanks Jill.  The controller.view() works great.  It's the edit part that I'm having trouble with.  Seems as though when I use controller.edit(), I get into an infinite loop, i.e., looks as though it's trying to go to the edit page, which in turn calls my redirect method which calls controller.edit() and on and on.  This code is being called as an override to the edit (functionality) button.

 

How does one override the edit button and not get into this situation?

 

Many thanks.

jwetzlerjwetzler

Sorry I should have looked a little closer.  I don't think you need an extension at all.  Look at this example, should be easy to adapt to your own:

 

 

<apex:page standardcontroller="contact" action="{!if($USER.firstname == 'Jill', URLFOR($Action.Contact.View, contact.id, null, true), URLFOR($Action.Contact.Edit, contact.id, null, true))}"> </apex:page>

 

 

 

PascalPascal

that may do the trick ... I'll give that a try

 

Thanks, Jill