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
mustapha L 1mustapha L 1 

ApexCode to open VisualForce Page

Hi all,

looking for Apex syntax how to open a VisualForce page
ie :
 if (field==Case1)
{open visualForce1}
 if (field==Case2)
{open visualForce2}

Many thaks for your assistance.
Best Answer chosen by mustapha L 1
Gaurav_SrivastavaGaurav_Srivastava
Hi,

try below code:
public pagereference Redirect()
{
    if (field==Case1)
    {
        PageReference vfPage1 = new PageReference('/apex/visualForce1');
        vfPage1.setRedirect(true);
        return vfPage1;
    }

    if (field==Case2)
    {
        PageReference vfPage2 = new PageReference('/apex/visualForce2');
        vfPage2.setRedirect(true);
        return vfPage2;
    }
    return null;
}

Thanks,
Gaurav

All Answers

Gaurav_SrivastavaGaurav_Srivastava
Hi,

try below code:
public pagereference Redirect()
{
    if (field==Case1)
    {
        PageReference vfPage1 = new PageReference('/apex/visualForce1');
        vfPage1.setRedirect(true);
        return vfPage1;
    }

    if (field==Case2)
    {
        PageReference vfPage2 = new PageReference('/apex/visualForce2');
        vfPage2.setRedirect(true);
        return vfPage2;
    }
    return null;
}

Thanks,
Gaurav
This was selected as the best answer
Nisar799Nisar799

Hi, 

You can try this code also 

public pagereference redirect(){
        Pagereference pg = null;
        if(field==Case1){
            pg = Page.YOUR_PAGE1; // put your page Name with Page.YourPage 
            pg.setRedirect(true);
        }
        if(field==Case2){
            pg = Page.YOUR_PAGE2;
            pg.setRedirect(true);
        }
        return pg;
    }
Feel free to ask if you have any question.
Thanks,
799 Coder
mustapha L 1mustapha L 1
You Both perfect ! Many thanks for your answers...just a little last point, when i do that i can redirect to the VisualPage i need But, my VisualPages needs ID (the Quote ID) i do not know how to aadd the ID 

I have added the following to extract my Quote ID (the button is into Quote Layout)

Id MyId= ApexPages.currentPage().getParameters().get('id');    //this looks OK
however i do not have syntax how to insert this MyId into PageReference.


 PageReference vfPage1 = new PageReference('/apex/visualForce1/MyId'); //of course doesn't work
or
pg = Page.YOUR_PAGE2.myId;  //doesn't work neither

could you just help me on this particular point ?
Many thanks
 
Gaurav_SrivastavaGaurav_Srivastava
Hi,

Adjusted code to pass the parameter into url.

Code Sample 1:
// Method to redirect to vf page and passing the parameter
public pagereference Redirect()
{
    PageReference vfPage = null;
    if (field==Case1)
    {
        vfPage = new PageReference('/apex/visualForce1?qID='+quoteID);
    }

    if (field==Case2)
    {
        vfPage = new PageReference('/apex/visualForce2?qID='+quoteID);
    }
    vfPage.setRedirect(true);
    return vfPage;
}

Code Sample 2:
// statement to get the parameter
quoteID = Apexpages.currentPage().getParameters().get('qID');

Thanks,
Gaurav
mustapha L 1mustapha L 1
ok, i did the next to pass my ID:

PageReference vfPage1 = new PageReference('/apex/visualForce1/?id=' +MyId)

this fix the issue how to pass the ID but doesn't work as expected for 2 reasons:

1) I do not kno reason but the "VisualForce1" is duplicated into opened page
in fact when i press button i have something like
/apex/VisualForce1/VisualForce1/id=value_of_id

2) second, it looks like the returned ID is not the expected one, what causng error.
it is suppoed to return the Quote ID (what works well when the Button is directly linked to the VisualForce1...but not since going trhought REdirect

i do not undesrtand why same command line could provide not same result:
this works well on "VisualForce1 extension"
                                                           Id MyId= ApexPages.currentPage().getParameters().get('id');


many thans for all assistance.
mustapha L 1mustapha L 1
FIXED....shame on me, i did a typo !!!

unexpected "/" just before parameter.

PageReference vfPage1 = new PageReference('/apex/visualForce1    /  ?id=' +MyId)

HUGE thanks to you both.
namrata vora 2namrata vora 2
Hi All,

I am trying to do the same thing to open a vf page from apex class:
public pageReference returnpage(){
        system.debug('in pdfPageClass///');
        PageReference vfPage1 = new PageReference('/apex/pdfPage');
        vfPage1.setRedirect(true);
        return vfPage1;
    }
and calling this method from another class, but when this runs, the vf age does not open.
Can someone pls help.

Thanks,
Namrata