[SalesForce] How to i update param with JavaScript

I need to update from prompt 1 param in , but i get all the time a a JavaScript error "Cannot set property 'value'".

I need to set the "rejectReason" param to what the user has written in prompt and them to submit the action.

So Here's the code please help me to assign the variable to the param

<apex:form>
    <apex:commandButton action="{!processRejectButton}" onclick="rejectCandidate();" oncomplete="return location.reload();" value="Reject Candidate" id="theCommandLink">
      <apex:param name="approveId" assignTo="{!approveId}" value="{!c.con.ID}"/>
      <apex:param name="rejectReason" assignTo="{!rejectReason}" value="text" id="rejectReason"/>
   </apex:commandButton>
</apex:form>

<script>
    function rejectCandidate(){
      var missing = prompt("Enter reject reason details", "aaaa");

        if (missing != null && missing != "") {
           document.getElementById('{!$Component.rejectReason}').value = missing;
        }else{  
           alert("Please enter missing info details");
        }
   }    
</script>

Best Answer

You're half right here.

What you'll need to do is take the <apex:param> markup outside of the <apex:commandButton>. The button should then only call a JavaScript method, passing in the Id of the Contact(?). It shouldn't have an action:

<apex:commandButton onclick="rejectCandidate({!c.con.ID});" value="Reject Candidate" id="theCommandLink" />

Your rejectCandidate() method is fine as is, however it should do one more thing which is call a <apex:actionFunction> that passes in the parameters to the controller.

For example:

<script>
  function rejectCandidate(contactId){
    var missing = prompt("Enter reject reason details", "aaaa");

    if (missing != null && missing != "") {
       document.getElementById('{!$Component.rejectReason}').value = missing;
    }else{  
       alert("Please enter missing info details");
    }

    doRejectCandidate(
      missing,
      contactId
    );
  }
</script>

Alls we've really done to your JavaScript is add the latter part: something that calls doRejectCandidate() and passes in two String values (missing and contactId). "missing" is generated from the function you've already written, "contactId" is passed in from the button.

Finally, you'll just need to have an <apex:actionFunction> with the name doRejectCandidate that then assigns the parameters and calls the action you initially set in your button.

<apex:actionFunction name="doRejectCandidate" action="{!processRejectButton}">
      <apex:param name="missing" value="" />
      <apex:param name="contactId" value="" />
  </apex:actionFunction>

This will pass to the controller as a GET request, meaning you can then access the values of missing and contactId in your processRejectButton() method as below:

String missing = Apexpages.currentPage().getParameters().get('missing');
String contactId = Apexpages.currentPage().getParameters().get('contactId');