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
Ola BamideleOla Bamidele 

How to transfer ID from one Visualforce page to another

Hi Gurus, 

I have a Visualforce page for a Survey form that saves entry in to a custom object (Survey) in Salesforce. Within this VF page, I have images of different countries flags which are links to the same survey but in a different language. i.e the french flag with be a link to another VF page but will be the same survey but in french.

Now my problem is that when the flag is click and goes to the different language page, the Survey ID in the web address bar is removed therefore the entry wouldnt be saved into the correct place in Salesforce.

So does any one please know I can transfer the ID from one VF page to another?

This is the Div Class for my UK Flag linking to the english version;
<div class="UK_Flag">
                   <a href="/apex/CustomerSatisfaction_EN"><apex:image id="UK_Flag" value="
                   {!URLFOR($Resource.UK_Flag)}" /></a>
       	 </div>

This is my Apex Class for keeping the ID in the address bar after clicking on the UK Flag but its not working:
public with sharing class CustomerSurvey{
public CustomerSurvey() {

}
 public PageReference UK_Flag () {
           PageReference pageref = new PageReference('/apex/CustomerSurvey_UK'); 
           pageref.getParameters().put('recordID', 'a0C3E000001yarn' + ''); 
           pageref.setRedirect(true); return pageref;
           return pageref;
}

}

Please if anyone know how I can resolve this issue, please let me know please!
Thanks!
 
Best Answer chosen by Ola Bamidele
Dmitry OfitserovDmitry Ofitserov
Hi Ola,
Yes, you've just misplaced it a bit :)
It should be like this :
public with sharing class CustomerSatisfactionFR2{
  public String currentRecordId {get;set;}


  public CustomerSatisfactionFR2(ApexPages.StandardController controller){
    this.currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
  }
  
    
  public PageReference UK_Flag () {                   
  		   PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_EN?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }

}

 

All Answers

Dmitry OfitserovDmitry Ofitserov
Hi Ola,
Maybe try writing your function in the following format :
public PageReference UK_Flag () {
          PageReference pageref = Page.CustomerSurvey_UK; 
          pageref.getParameters().put('recordID', 'a0C3E000001yarn'); 
          return pageref.setRedirect(true);
}

Let me know if it worked for you.
Ashish KumarAshish Kumar
Hi Ola,

Since you are using href to navigate to another page and that will just redirect you to that page without passing the parameter.

What you need to do is onclick on the flag call the apex method "UK_Flag" and from there it will redirect the user to right page along with the survey id.  

In case you want to use the image to be click then you can use apex:action function to call your apex method.
How to use apex:actionfunction 
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

Please let me know in case it helps.

Regards,
Ashish Kr.
Ola BamideleOla Bamidele
Hi Dmitry, 

I'm getting error stating :
Unknown constructor 'CustomerSurvey.CustomerSurvey(ApexPages.StandardController controller)'

Do you know why this is by any chance? 

Thanks!
 
Ola BamideleOla Bamidele
Hi Ashish, 

Okat thanks ill look into it and hopefully it helps resolve the issue


Thanks!
Shubham saini 14Shubham saini 14

Hello ola,

 

String recordID = 'a0C3E000001yarn';
PageReference pageref = new PageReference('/apex/CustomerSurvey_UK?Id='+recordID); 
           pageref.setRedirect(true);
           return pageref;

Try this
Ola BamideleOla Bamidele
Hi Shubham saini 14,

Thanks for your response. I tried your code and i recieved an error stating 
 
Unknown constructor 
'CustomerSurvey.CustomerSurvey(ApexPages.StandardController controller)'

This error appears when I add "CustomerSurvey" to the "extensions" tag within the standard controller. 

Do you know why did this by any chance?


Thanks very much!
Dmitry OfitserovDmitry Ofitserov
Hi Ola,
Just add a constructor to your CustomerSurvey controller that receives a standard controller as a parameter.
It should look something  like this:
public with sharing class CustomerSurvey{
ApexPages.StandardController ctrl {get;set;}

public CustomerSurvey() {
}

public CustomerSurvey(ApexPages.StandardController ctrl){
  this.ctrl = ctrl;
}

...


 
Shubham saini 14Shubham saini 14
Hello ola,

if you are using StandardController on vf page then you have to add this constructor in your apex class CustomerSurvey
public CustomerSurvey(ApexPages.StandardController controller){

}


Thanks,

Shubham

Ola BamideleOla Bamidele
Hi Shubham saini 14,

Thanks for the reply. I changes the 'ctrl' to 'controller' and im not recieving any errors. However when I switched pages, the ID doesnt transfer to the second page meaning the entry cant be saved. 

Do you know what changes ill need to make to ensure that the ID is transfered?


Thanks very much again 
Ashish KumarAshish Kumar
Hi Ola,

you can have as many methods in your apex class but they are of no use until you execute/call them, same is the case here.
you have your method to redirect to another page along with id but you sre not calling that method from your vf page.

please take reference from my earlier answer to call this method onclick of flag.

let me know in case if you need any help.

Regards,
Ashish Kr.
Ola BamideleOla Bamidele
Ashish Kumar, 

Thanks. This is my onclick in my VF page:
<apex:image url="{!URLFOR($Resource.UK_Flag)}" onclick="UK_Flag"/>

& this is my Apex code:
public with sharing class CustomerSatisfactionFR2{
ApexPages.StandardController ctrl {get;set;}

public CustomerSatisfactionFR2() {
}
    

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

public PageReference UK_Flag () {
           String recordID = 'a0C3E000001yarn';
		   PageReference pageref = new PageReference('/apex/CustomerSatisfaction_EN?Id='+recordID); 
           pageref.setRedirect(true);
           return pageref;
}

}

The flag is showing on the however, nothing is happening when I click the flag - its not redirecting or transferring the ID.

Do you know what changes I could make to make the redirection and transferr happen?

Thanks for the help!
Dmitry OfitserovDmitry Ofitserov
Hi Ola,
In this case you should use actionFunction element to call controller function.
In VF page add:
<apex:actionFunction name="goToUK" action="{!UK_Flag}""/>

<apex:image url="{!URLFOR($Resource.UK_Flag)}" onclick="goToUK()"/>

And also please fix your controller's constructor to initiate the controller correctly : this.ctrl = controller;
 
Shubham saini 14Shubham saini 14
Hello Ola,
 
<apex:form>

<apex:actionFunction name="UK_Flag" action="{!UK_Flag}"/>

<apex:image url="{!URLFOR($Resource.UK_Flag)}" onclick="MyFunc()"/>

<script>
function MyFunc(){
UK_Flag();
}
</script>
</apex:form>
 
public with sharing class CustomerSatisfactionFR2{

public CustomerSatisfactionFR2(ApexPages.StandardController controller){
  
}

public PageReference UK_Flag () {
           String recordID = 'a0C3E000001yarn';
		   PageReference pageref = new PageReference('/apex/CustomerSatisfaction_EN?Id='+recordID); 
           pageref.setRedirect(true);
           return pageref;
}

}

I hope this will help you

Thanks
Ashish KumarAshish Kumar
Hi Shubham,

You may even remove script tag and can call action function directly with onclick method name. 
#Write less code while do more. :)
<apex:form>

<apex:actionFunction name="MyFunc" action="{!UK_Flag}"/>

<apex:image url="{!URLFOR($Resource.UK_Flag)}" onclick="MyFunc()"/>

<!-- no need of this code too - save 4 lines of codes :)
<script>
function MyFunc(){
UK_Flag();
}
</script> -->
</apex:form>

Regards,
Ashish Kr.
 
Shubham saini 14Shubham saini 14
Hi Ashish, 

Yes, we can also do like this

Thank you 
Ola BamideleOla Bamidele
Hi Dmitry,
 
Thanks I tried your code. The ID is now remaining in the address bar but the page is’nt going to the English Visualforce page. So its also like it refreshing the page but not going to the second VF page.
I’m not sure why this is because the English VF page is in the page reference in the apex.

Thanks very much!
Ola BamideleOla Bamidele
Hi Shubham Saini 14,
Thanks again for the support. I tried your code and the ID remains in the address bar but the page doesn’t go to the English VF which is in the apex code in the page reference.
Do you know the changes ill need to make as for the page does refresh but doesn’t go the other VF page.


Thanks so much for the help!
Dmitry OfitserovDmitry Ofitserov
Hi Ola,
Please try writing the actionFunction as Ashish Kumar or Shubham saini 14 suggested but also add "immediate="true" to it so it will look like this :
<apex:actionFunction immediate="true" name="MyFunc" action="{!UK_Flag}"/>

If it won't work please share the VF and controller code with us.
Thanks​
 
Ola BamideleOla Bamidele
Hi Dmitry Ofitserov,

Thanks! the "immediate" tag allows the ID to be passed through to the second VF page!!

However there is one more problem before I stop bovering you gurus :D 


In the apex code, it defines a specific ID therefore entry will only be saved into that ID in Salesforce. So is there a adaptation that I can make so that it keeps whatever ID is in the address, rather than this specific one? 

because we'll have more than one person taking the survey and we dont it to be all saved in the same Customer ID. 

Thanks very much people, I truly appricate the help and support!
Dmitry OfitserovDmitry Ofitserov
Hi Ola,
Sure ! Just add a variable into the controller : public String currentRecordId {get;set;} and in the contructor with standard controller parameter add a line 
this.currentRecordId = ApexPages.CurrentPage().getparameters().get('id');

Then,jus  use this ID in you function (or functions) like this :
public PageReference UK_Flag () {
           String recordID = 'a0C3E000001yarn';
		   PageReference pageref = new PageReference('/apex/CustomerSatisfaction_EN?Id='+currentRecordId); 
           pageref.setRedirect(true);
           return pageref;
}

Also, please keep in mind that "immediate=true" on actionFunction bypasses validation (if you have any).

Regards,
Dmitry
Dmitry OfitserovDmitry Ofitserov
Sorry for the above.
The line 
String recordID = 'a0C3E000001yarn';

is no longer needed in the function body.
Ola BamideleOla Bamidele
Hi Dmitry Ofitserov, 

Thats great that it can be done! I made the adjustments that you mentioned to the best of my undertanding but im recieving several errors so im guessing ive made a mistake somewhere. 

This is what my apex code looks like now:
public with sharing class CustomerSatisfactionFR2{

public CustomerSatisfactionFR2(ApexPages.StandardController controller){

public String currentRecordId {get;set;} 
  
}

    
public PageReference UK_Flag () {
                   this.currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
		   PageReference pageref = new 
                   PageReference('/apex/CustomerSatisfaction_EN?Id='+currentRecordId); 
                   pageref.setRedirect(true);
                   return pageref;
}

}

Is this how its suppose to look like or did I put it in the wrong place? 

Thanks very much!
Dmitry OfitserovDmitry Ofitserov
Hi Ola,
Yes, you've just misplaced it a bit :)
It should be like this :
public with sharing class CustomerSatisfactionFR2{
  public String currentRecordId {get;set;}


  public CustomerSatisfactionFR2(ApexPages.StandardController controller){
    this.currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
  }
  
    
  public PageReference UK_Flag () {                   
  		   PageReference pageref = new 
                     PageReference('/apex/CustomerSatisfaction_EN?Id='+currentRecordId); 
                     pageref.setRedirect(true);
                     return pageref;
  }

}

 
This was selected as the best answer
Ola BamideleOla Bamidele
Hi Dmitry Ofitserov, 

Yes its working now!! Big thanks to ypi and everyone in this thread that help me out, its much appreciated!! I wish i could best answer everyones response as you all helped :D 

Again thanks very much!