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
Collen Mayer 6Collen Mayer 6 

Problem passing value from Visualforce page to controller

Hi All,
I'm need to pass a recordtype id from a visualforce page to my controller extension.  However debug log is showing the variable in the controller I'm trying to set is NULL (not getting the value from the visualforce page).  Here's what I've tried.  Any help/feedback would be greatly appreciated.  
Best,
Collen 

My test page: 
 
<apex:page standardController="Satisfaction_Surveys__c" extensions="CreateSatisfactionSurvey2">  
  <apex:sectionHeader title="Satisfaction Survey" subtitle="Please rate the service you receieved."/>

  <apex:form >
     
    <apex:pageMessages /> 
    <apex:pageBlock title="Survey Response">
        

      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2">
          <apex:inputField value="{!survey.Access_Comm_Resources__c}" />
      <apex:param assignTo="{!recordtypetext}" value="01236000000xk08" name="recordtypetext"/> 

        </apex:pageBlockSection>

    </apex:pageBlock>
  </apex:form>
</apex:page>

My controller:
 
public  class CreateSatisfactionSurvey2 {
    public ApexPages.StandardController stdCntrlr {get; set;}
    public string recordtypetext {get;set;}
    public Satisfaction_Surveys__c survey {get;set;}
    

  public CreateSatisfactionSurvey2 (ApexPages.StandardController controller) {
        stdCntrlr = controller; 
     if (survey == null)
        survey = new Satisfaction_Surveys__c ();
  }

  // save button is clicked
  public PageReference save() {
		
    try {
		recordtypetext = ApexPages.CurrentPage().getParameters().get('recordtypetext');
        survey.RecordTypeId = recordtypetext;
        system.debug(recordtypetext);
        insert survey; // inserts the new record into the database
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new survey.'));
      return null;
    }

    // if successfully inserted new survey, then displays the thank you page.
    return Page.Survey_Create_Thankyou;
  }

}

 
Best Answer chosen by Collen Mayer 6
Alain CabonAlain Cabon
Hi Collen,

It is just because you have a very specific need (you sould have get the following tricks from another forum perhaps yet).

Anyway, that could also help other people because that cannot work without the used tricks in the code below.

Tell me if that works fine for: return Page.Survey_Create_Thankyou;  

<apex:commandButton action="{!save}" value="Save" rerender="bloc">
        <apex:param assignTo="{!recordtypetext}" value="01236000000xk08" name="recordtypetext"/>
 </apex:commandButton>  

<apex:pageBlockSection showHeader="false" columns="2" id="bloc" >

<apex:actionRegion> ... </apex:actionRegion>  // the weird part could be changed
<apex:page standardController="Satisfaction_Surveys__c" extensions="CreateSatisfactionSurvey2">  
  <apex:sectionHeader title="Satisfaction Survey" subtitle="Please rate the service you receieved."/>

  <apex:form >
    <apex:pageMessages /> 
    <apex:pageBlock title="Survey Response">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"  rerender="bloc">
             <apex:param assignTo="{!recordtypetext}" value="01236000000xk08" name="recordtypetext"/> 
         </apex:commandButton>   
      </apex:pageBlockButtons>
      <apex:actionRegion>
      <apex:pageBlockSection showHeader="false" columns="2"  id="bloc"  >
          <apex:inputField value="{!survey.Access_Comm_Resources__c}" />
      </apex:pageBlockSection>
      </apex:actionRegion> 
    </apex:pageBlock>
  </apex:form>
</apex:page>
 
public class CreateSatisfactionSurvey2 { 
   public ApexPages.StandardController stdCntrlr {get; set;} 
   public string recordtypetext {get;set;} 
   public Satisfaction_Surveys__c survey {get;set;} 

   public CreateSatisfactionSurvey2 (ApexPages.StandardController controller) { 
       stdCntrlr = controller; 
       if (survey == null) survey = new Satisfaction_Surveys__c (); 
   } 
   // save button is clicked 
   public PageReference save() { 
      try { 
         survey.RecordTypeId = recordtypetext; 
         system.debug(recordtypetext); 
         insert survey; // inserts the new record into the database
      } catch (DMLException e) {           
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new survey.')); 
            return null; 
     } 
// if successfully inserted new survey, then displays the thank you page. 
       return Page.Survey_Create_Thankyou; 
  } 
}
You will get your constant value passed.

Best regards
Alain

All Answers

Alain CabonAlain Cabon
Hi,

I don't understand clearly the constant value recordtypetext but you can pass this value by the URL when calling your VFPage.
Wait and see for other solutions.

.../apex/myVFPpage?recordtypetext=01236000000xk08

recordtypetext = ApexPages.CurrentPage().getParameters().get('recordtypetext');
<apex:inputHidden value="{!recordtypetext}"   />
public  class CreateSatisfactionSurvey2 {
    public ApexPages.StandardController stdCntrlr {get; set;}
    public string recordtypetext {get;set;}
    public Satisfaction_Surveys__c survey {get;set;}

  public CreateSatisfactionSurvey2 (ApexPages.StandardController controller) {
     stdCntrlr = controller; 
     recordtypetext = ApexPages.CurrentPage().getParameters().get('recordtypetext');
     if (survey == null)
        survey = new Satisfaction_Surveys__c ();
  }
  // save button is clicked
  public PageReference save() {		
    try {		
        survey.RecordTypeId = recordtypetext;
        system.debug(recordtypetext);
        insert survey; // inserts the new record into the database
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new survey.'));
      return null;
    }
    // if successfully inserted new survey, then displays the thank you page.
    return Page.Survey_Create_Thankyou;
  }
}
<apex:page standardController="Satisfaction_Surveys__c" extensions="CreateSatisfactionSurvey2">  
  <apex:sectionHeader title="Satisfaction Survey" subtitle="Please rate the service you receieved."/>
  <apex:form >    
    <apex:pageMessages /> 
    <apex:pageBlock title="Survey Response">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection showHeader="false" columns="2">
          <apex:inputField value="{!survey.Access_Comm_Resources__c}" />
          <apex:inputHidden value="{!recordtypetext}"   />
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Regards
Alain
Collen Mayer 6Collen Mayer 6
Thanks, Alain.  Passing the value by link definitely works. 

I am still a bit baffled that I can't assign the value in the visualforce page without including in the link.  Essentiallly I'm going to have about 15 visualforce pages that use the same controller, but each page creates a record with a different record type.  My plan was to have each page assign the corresponding recordtypeid value and pass it to the controller to create the record.  I'm trying to figure out how to have 15 pages but not have to have 15 controllers. 

If I can't do this in the visualforce page, is there a way to differentiate in my controller which page referenced the controller: if page 1, recordtypeid = 'aaaaaaaaaaaaa', if page 2, recordtypeid = 'bbbbbbbbbbb', etc?  I'm a bit over my head here so I'd appreciate any thoughts from you or others. 
Alain CabonAlain Cabon
Hi Collen,

It is just because you have a very specific need (you sould have get the following tricks from another forum perhaps yet).

Anyway, that could also help other people because that cannot work without the used tricks in the code below.

Tell me if that works fine for: return Page.Survey_Create_Thankyou;  

<apex:commandButton action="{!save}" value="Save" rerender="bloc">
        <apex:param assignTo="{!recordtypetext}" value="01236000000xk08" name="recordtypetext"/>
 </apex:commandButton>  

<apex:pageBlockSection showHeader="false" columns="2" id="bloc" >

<apex:actionRegion> ... </apex:actionRegion>  // the weird part could be changed
<apex:page standardController="Satisfaction_Surveys__c" extensions="CreateSatisfactionSurvey2">  
  <apex:sectionHeader title="Satisfaction Survey" subtitle="Please rate the service you receieved."/>

  <apex:form >
    <apex:pageMessages /> 
    <apex:pageBlock title="Survey Response">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"  rerender="bloc">
             <apex:param assignTo="{!recordtypetext}" value="01236000000xk08" name="recordtypetext"/> 
         </apex:commandButton>   
      </apex:pageBlockButtons>
      <apex:actionRegion>
      <apex:pageBlockSection showHeader="false" columns="2"  id="bloc"  >
          <apex:inputField value="{!survey.Access_Comm_Resources__c}" />
      </apex:pageBlockSection>
      </apex:actionRegion> 
    </apex:pageBlock>
  </apex:form>
</apex:page>
 
public class CreateSatisfactionSurvey2 { 
   public ApexPages.StandardController stdCntrlr {get; set;} 
   public string recordtypetext {get;set;} 
   public Satisfaction_Surveys__c survey {get;set;} 

   public CreateSatisfactionSurvey2 (ApexPages.StandardController controller) { 
       stdCntrlr = controller; 
       if (survey == null) survey = new Satisfaction_Surveys__c (); 
   } 
   // save button is clicked 
   public PageReference save() { 
      try { 
         survey.RecordTypeId = recordtypetext; 
         system.debug(recordtypetext); 
         insert survey; // inserts the new record into the database
      } catch (DMLException e) {           
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new survey.')); 
            return null; 
     } 
// if successfully inserted new survey, then displays the thank you page. 
       return Page.Survey_Create_Thankyou; 
  } 
}
You will get your constant value passed.

Best regards
Alain
This was selected as the best answer
Collen Mayer 6Collen Mayer 6
Wow that did it.  Thank you so much for your help and for teaching me the new tricks. 

Best,
Collen