0

I have a force.com site with custom url for my web2lead just say www.company.com/step1. i want redirect it to www.company.com/step2 after button clicked but it redirecting to www.company.com/step1/step2.

here's my PageReference

public PageReference Next(){ 
web2lead.Name = firstName+' '+lastName;
web2lead.Phone__c = phone;
web2lead.City__c = city;
web2lead.Company__c ='Keluarga '+firstName+' '+lastName;
web2lead.Email__c = email;
insert web2lead;

web2leadId = web2lead.Id;

PageReference step2Form = new PageReference('/step2');
step2Form.getParameters().put('id',web2lead.Id);
step2Form.setRedirect(true);
return step2Form;
}

did i doing something wrong ? because i don't want to put full url to PageReference like PageReference('http://www.company.com/step2');. can i do that ?

5
  • 1
    Could you try PageReference step2Form = new PageReference('step2');?
    – Dan Jones
    Commented Jan 12, 2016 at 9:44
  • 1
    can you try PageReference step2Form = new PageReference ('/apex/step2'); ?? Commented Jan 12, 2016 at 9:46
  • @Poet try using it but not working bro. Commented Jan 12, 2016 at 10:00
  • @FebrianTarigan This might be a bit of a stretch, but try PageReference step2Form = new PageReference('../step2'); Normally what the ../ does is goes back a level in most languages I've used.
    – Dan Jones
    Commented Jan 12, 2016 at 10:17
  • still not working. get redirect to www.company.com/step1/step1 Commented Jan 12, 2016 at 10:20

2 Answers 2

1

You have suggested that you don't want to hard code the full URL like http://www.company.com/step2. In this case you can get the full URL dynamically with URL.getSalesforceBaseUrl().toExternalForm() and append the vf page name with it. I tried with the below solution and it is working as expected. Please try and let me know if this works.

step1 vf page

<apex:page controller="RedirectTestController"  >
    Hello World {!baseUrl}
    <apex:form>
        <apex:commandButton action="{!Next}" value="Step2"/>
    </apex:form>    
</apex:page>

step2 vf page

<apex:page >
    Hello World
</apex:page

Controller

public class RedirectTestController {
    public String baseUrl;
    public String getBaseUrl() {
        baseUrl=URL.getSalesforceBaseUrl().toExternalForm();
        return baseUrl;
    } 
    public PageReference Next(){ 
        String nextpageurl = getBaseUrl() +'/step2';
        PageReference step2Form = new PageReference(nextpageurl);
        step2Form.setRedirect(true);
        return step2Form;
    }
}
0
1

Instantiate PageReference approach Page.Page_API_Name as:

PageReference step2Form = Page.step2; // Return pagereference Page.Page_API_Name
step2Form.getParameters().put('id',web2lead.Id);
step2Form.setRedirect(true);
return step2Form;
3
  • did it will be problem i use on force.com site ? Commented Jan 12, 2016 at 9:52
  • I believe it should automatically generate correct url which can work on site as well. @FebrianTarigan
    – Ashwani
    Commented Jan 12, 2016 at 9:57
  • it didn't work too. get redirect to www.compay.com/step1/step2 Commented Jan 12, 2016 at 10:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .