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 ?

  • 1
    Could you try PageReference step2Form = new PageReference('step2');? – Dan Jones Jan 12 '16 at 9:44
  • 1
    can you try PageReference step2Form = new PageReference ('/apex/step2'); ?? – DOMINIC EDWARD Jan 12 '16 at 9:46
  • @Poet try using it but not working bro. – febriantarigan Jan 12 '16 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 Jan 12 '16 at 10:17
  • still not working. get redirect to www.company.com/step1/step1 – febriantarigan Jan 12 '16 at 10:20
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;
    }
}
| improve this answer | |
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;
| improve this answer | |
  • did it will be problem i use on force.com site ? – febriantarigan Jan 12 '16 at 9:52
  • I believe it should automatically generate correct url which can work on site as well. @FebrianTarigan – Ashwani Jan 12 '16 at 9:57
  • it didn't work too. get redirect to www.compay.com/step1/step2 – febriantarigan Jan 12 '16 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.