0

I would like to know how to pass a parameter from one controller to another controller?

For example I am trying to pass the accountId value by using the following code in the first controller

public PageReference goToNextPage() {
    PageReference pageRef = Page.MyNextPage;
    pageRef.getParameters().put('AccId', accountId);
    return pageRef;
}

Page

<apex:page standardController="Lead" extensions="LeadToMerchConvertController">
  <apex:form > 
     <apex:pageBlock > 
        <apex:pageMessages > </apex:pageMessages> 
     </apex:pageBlock> 
  </apex:form>
</apex:page> 

Controller

public with sharing class LeadToMerchConvertController { 

       Lead objLead; 
       public static String accountId; 
       public LeadToMerchConvertController(ApexPages.StandardController controller) 
       { 
         objLead = (Lead)controller.getRecord();
         System.debug(ApexPages.currentPage()); 
         accountId = ApexPages.currentPage().getParameters().get('AccId');
         System.debug('accountId ' + accountId); 
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING,'There is an existing Merchant: ' + accountId + ''); 
         ApexPages.addMessage(myMsg); 
      } 
}

and according to the log file

DEBUG|pageRef System.PageReference[/apex/newconvtomerch?AccId=00156000002zGVsAAM

the value I need is added to the AccId.

The problem is that by using the following code

String AccountId = ApexPages.currentPage().getParameters().get('AccId');

I can not get the AccId value in the second controller which actually is the Extension controller

Could you please advise how to fetch the AccId value I need from the first controller?

Regards,

​Dilyan

8
  • Hi Dylan. Can you please let me know the second page url like this. /apex/newconvtomerch?AccId=00156000002zGVsAAM ?
    – Ratan Paul
    Dec 1 '15 at 10:23
  • Here is the second page url System.debug(ApexPages.currentPage()); DEBUG|System.PageReference[/apex/newConvToMerch
    – Dylan D
    Dec 1 '15 at 10:29
  • looks like you are passing AccountId. Can you please add your both page and controller in question section?
    – Ratan Paul
    Dec 1 '15 at 10:31
  • Hi here is the page code <apex:page standardController="Lead" extensions="LeadToMerchConvertController"> <apex:form > <apex:pageBlock > <apex:pageMessages > </apex:pageMessages> </apex:pageBlock> </apex:form> </apex:page>
    – Dylan D
    Dec 1 '15 at 10:40
  • public with sharing class LeadToMerchConvertController { Lead objLead; public static String accountId; public LeadToMerchConvertController(ApexPages.StandardController controller) { objLead = (Lead)controller.getRecord(); System.debug(ApexPages.currentPage()); accountId = ApexPages.currentPage().getParameters().get('AccId'); System.debug('accountId ' + accountId); ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING,'There is an existing Merchant: ' + accountId + ''); ApexPages.addMessage(myMsg); }
    – Dylan D
    Dec 1 '15 at 10:41
1

try this

public PageReference goToNextPage() {

    return new PageReference('/apex/newconvtomerch?AccId='+accountId);
}
2
  • Hi here is the page code <apex:page standardController="Lead" extensions="LeadToMerchConvertController"> <apex:form > <apex:pageBlock > <apex:pageMessages > </apex:pageMessages> </apex:pageBlock> </apex:form> </apex:page>
    – Dylan D
    Dec 1 '15 at 10:37
  • Hi, I've tried what you suggested but still the value of the AccId is not passed to the extension controller
    – Dylan D
    Dec 1 '15 at 10:44
0

I believe you simply need to set the redirect attribute of the pageReference to true before returning it.

public PageReference goToNextPage() {
    PageReference pageRef = Page.MyNextPage;
    pageRef.getParameters().put('AccId', accountId);
    pageRef.setRedirect(true);
    return pageRef;
}

The redirect attribute is false by default, which according to the setRedirect documentation will perform a server-side redirect.

By contrast, setting redirect to true will cause your browser to fire a new GET request. The pageReference parameters are passed as a query string in the URL, so the GET request is required to pass that information to your next controller.

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.