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
Ed055Ed055 

Redirecting to correct VF page

I have a custom object which has lookup relationship with Account and Contact. On the account and contact page I have related list to that object. I have created 2 VF pages to display fields related to this custom object in the format I want. One of the VF page is for users who click on edit/new/view in the related list of the custom object on Account page and other is if the user comes from Contact page.
I'm trying to figure out how to redirect user to a particuar VF page depending on whether they click view/edit/new in the related list on Account or Contact page. 

Thanks for help.
Best Answer chosen by Ed055
kaustav goswamikaustav goswami
That is because it is not able to get the definition of the variable used by you in the method - "profileId".Declare the variable in the global scope of the class so that it is accessible to the method and assign the initial value of the variable in the constructor. Without this - when the method executes the profileId variable will not be available for the system. Alternatively if the value of the profileId variable is not required anywhere else then you can declare and initialise it in the method itself. Thanks,Kaustav

All Answers

kaustav goswamikaustav goswami
you can do this by checking the first three characters of the id of the current page/record. if it is 001 it is account if it is 003 it is contact.

thanks,
Kaustav
Ed055Ed055
Thanks Kaustav for your suggestion. I'm not sure how to do it. Can you please guide me with sample code? should I do it in VF or apex ?
kaustav goswamikaustav goswami
I would suggest you do the following.

Step 1. - Override the new button with any one of the VF pages
Step 2. - Add a method in the controller of the VF Page with which you have overriden that checkes the id 
 
public pageReference checkSource(){
    PageReference pgRef = null;
    String recId = ApexPages.currentPage().getParameters().get('retURL');
    if(recId != null){
        if(recId.subString(1,4) == '003'){ // this is considering that the page you ahve added to the button is the account page
            pgRef  = new PageReference('/apex/page_to_view_from_contact');
        }
    }
    return pgRef;
}


Step 3. - Call this from action attribute of the VF page.
<apex:page id="samplePage" ..... action="{!checkSource}">
....your code
</apex:page>

The concept is - when you click the new button on the related list a parameter called the return url is added to the url. You can see from that parameter whether the related list that was clicked was on a contact page (id it starts with 003) or an account page ( id starts with 001). If it is 003 then redirect that user to the contact page.
Note: If the object is exposed through a tab then you also need to keep a handling for that scenario as in that scenario the return url parameter will not be present. If the only way the users can click on the new button of this custom object is through the two related lists then you are okay.

Hope this helps.
Thanks,
Kaustav
Ed055Ed055
Thanks Kaustav . I did the change as per your suggestion. I'm not sure what is going on but it keeps redirecting in a loop. Profile ID does exist. 
public class DispatchController {

    public DispatchController (ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public PageReference getRedir() {
        
       
        String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
        String PageURL = ApexPages.currentPage().getUrl(); 
        String wholeURL = baseURL+PageURL; 
        
         
        
        if(profileid=='00eE0000000NY1f'){
            
            wholeURL =wholeURL.replace('DispatchPage','Test_Contact_Layout') ;
        }else if (profileid=='00eE0000000tJt3'){
             wholeURL =wholeURL.replace('DispatchPage','Test_Account_Layout') ;
        }
     
        newPage = new PageReference(wholeURL);
       
        
        return newPage;

    }

    private final ApexPages.StandardController controller;

}

VF page name: DispatchPage
<apex:page controller="DispatchController"     action="{!getRedir}">
</apex:page>
kaustav goswamikaustav goswami
That is because it is not able to get the definition of the variable used by you in the method - "profileId".Declare the variable in the global scope of the class so that it is accessible to the method and assign the initial value of the variable in the constructor. Without this - when the method executes the profileId variable will not be available for the system. Alternatively if the value of the profileId variable is not required anywhere else then you can declare and initialise it in the method itself. Thanks,Kaustav
This was selected as the best answer