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
jack__cjack__c 

collecting user info from visual force page using controller extension

 

I need a little help getting this to work.  I thought there were only two pieces of code to add (one to visual force page and one to the controller for the visual force page) to accept user input through a visual force page. 

 

 

 

I can initialize approvalComment in the visual force page's controller and I can enter text in the approvalComment field on the visual force page, but I only get my initialized value back...  NOT what the user types in the inputTextarea. 

 

 

 

I am trying to understand what I am missing and why. 

 

 

 

visualforce page code: 

 

<apex:form id="mainForm">  

 

.... 

 

<apex:actionRegion >  

 

 <apex:pageBlock id="approvalCommentBlock" title="Approval Comments" rendered="{!(currentApprovalStateName == 'Required')}" > 

 

      <table width="600"> 

 

         <tr>  

 

            <td width="40%"><apex:outputLabel value="Enter Approval Comments." for="approvalComment" /></td>  

 

            <td width="60%"><apex:inputTextarea cols="50" id="approvalComment" value="{!approvalComment}"/></td>  

 

         </tr>  

 

      </table> 

 

   </apex:pageBlock>   

 

</apex:actionRegion> 

 

....  

 

</apex:form> 

 

 

 

 

 

 

 

visual force page's controller code: 

 

private String approvalComment;  

 

public String getApprovalComment() {   

 

     return approvalComment;  

 

}  

 

public void setApprovalComment(String s) { 

 

     approvalComment = s;  

 

} 

 

 

 

private void initializeState() { 

 

    approvalComment = 'Approval Comments go here.'; 

 

 

 

 

 

 

 

System Log: 

 

14:36:32.924|USER_DEBUG|[300]|DEBUG|approvalComment: Approval Comments go here. 

 

Best Answer chosen by Admin (Salesforce Developers) 
sravusravu

The problem might be with the action region tag. You have the button in onc action region and the input field in another action region. May be that could be the error.

All Answers

sravusravu
Are you performing any action in your visual force page in order to pass the user inputted value from the page to controller?
jack__cjack__c

No.  Not at all.  Is that what I am missing?  I have been scouring the boards for an example to use.

 

I thought the field was bound to a controller extension.  I was hoping to not bind it to a sObject and pass this field to an approval process that is already built out.  Its puzzling because I can initalize the property, see the initialized property on my visual force page, type in user input which should wipe out the initialized value, but in the system log in the approval process I see the ONLY the initialized value.

sravusravu

Ok that could be the reason for you not fetching the right value. Add a command button and action in the controller which will pass the value from the page to the controller and see.

jack__cjack__c

I have a "submit for approval" button, but do not know how to code the part in the action to get the user input.  My page is named PricingToolPage and the form id is "mainForm".

 

I thought the getters and setters would do that for me.

sravusravu

Make the following changes in your code:(Marked in red)

 

visualforce page code: 

 

<apex:form id="mainForm">  

 

.... 

 

<apex:actionRegion >  

 

 <apex:pageBlock id="approvalCommentBlock" title="Approval Comments" rendered="{!(currentApprovalStateName == 'Required')}" > 

 

      <table width="600"> 

 

         <tr>  

 

            <td width="40%"><apex:outputLabel value="Enter Approval Comments." for="approvalComment" /></td>  

 

            <td width="60%"><apex:inputTextarea cols="50" id="approvalComment" value="{!approvalComment}"/></td>  

 

         </tr>  

 

      </table> 

<apex:commandButton value="Submit for Approval" action="{!submit}" id="submitButton"/>

 

   </apex:pageBlock>   

 

</apex:actionRegion> 

 

....  

 

</apex:form> 

 

 

 

 

 

 

 

visual force page's controller code: 

 

private String approvalComment;  

 

public String getApprovalComment() {   

 

     return approvalComment;  

 

}  

 

public void setApprovalComment(String s) { 

 

     approvalComment = s;  

 

} 


Instead of getter and setter methods you can also use


public String approvalComment{get;set;}

 

public PageReference submit(){

         //You can now use the updated approvalComment value in this method and proceed accordingly

        //Include the code you want to execute after the user submits the approval comment

 

return null;//If you do not want to move to a new page

  return Page.<Pagename>;//if you want to move to a new page

}

 

 

private void initializeState() { 

 

    approvalComment = 'Approval Comments go here.';

 

 

Let me know if you face any difficulty.........

jack__cjack__c

Thank you for your suggestions.  I already have multiple command buttons which fire as intended according to the system logs.  The command button fires and processes approvalComment showing the initialized value of approvalComments, but not what the user has typed over the initialized value, ie. the value typed in the inputTextarea.  I have also checked to make sure the initializeState function does not wipe out the user input.

I have added a little bit more of the code to show the command button and controller action. 

 

visualforce page code:

<apex:outputPanel id="buttonOutputPanel">

   <apex:actionRegion >

      <apex:commandButton id="requestApprovalButton" value="Request Approval"

   rerender="buttonOutputPanel, quoteLinesOutputPanel,      scriptOutputPanel,huntGroupsOutputPanel"

                 action="{!requestApproval}"

                 oncomplete="initQuoteLines();"

                 disabled="{! currentApprovalStateName != 'Required'}" />

….

   </apex:actionRegion>

</apex:outputPanel>

<apex:actionRegion >

   <apex:pageBlock id="approvalCommentBlock"  title="Approval Comments"

         rendered="{!(currentApprovalStateName == 'Required')}"

         <table width="600">

            <tr>

               <td width="40%"><apex:outputLabel value="Enter Approval Comments (if needed) before clicking the Request Approval button." for="approvalComment" /></td>

               <td width="60%"><apex:inputTextarea cols="50" id="approvalComment" value="{!approvalComment}"/></td>

            </tr>

         </table>          

      </apex:pageBlock>

</apex:actionRegion>

 

 

visual force page's controller code:

public String approvalComment { get; set; }   

 

….

public PageReference requestApproval() {

    System.debug('discountApprovalProcessRequired(): ' + discountApprovalProcessRequired());

    System.debug('approvalComment: ' + approvalComment);

    System.SavePoint sp = Database.setSavepoint();

    try {

            saveHeader();

            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();

            String sComment = 'Submitting quote for approval.';

            if (approvalComment != null) {

                sComment = sComment + '/n' + approvalComment;

            }

            req.setComments(sComment);

            req.setObjectId(quote.Id);

            Approval.ProcessResult result = Approval.process(req);

        }

        catch (Exception e) {

            Database.rollback(sp);

        }

        updateApprovalState();

        return null;

  }
….

 

 private void initializeState() {

   approvalComment = 'Approval Comments go here.';

   IsClosed = opportunity.StageName == 'Closed Won';  

sravusravu

The problem might be with the action region tag. You have the button in onc action region and the input field in another action region. May be that could be the error.

This was selected as the best answer
jack__cjack__c

Thats it!  Thank you very much.  I put the approvalComment inputTextarea in the same actionRegion and it works.