So, the relevant snippets of code are:

function doFunction(choice) { switch (choice) { case "changename": changeName(document.getElementById("makeNewName").value); break; } }

<apex:form >
    <apex:pageBlock mode="edit" title="Clone Job" >
       <apex:actionFunction name="changeName" action="{!saveCloneObjects}">
              <apex:param name="newName" value="" />
         <apex:pageBlockButtons >
         <button type="button" onclick="doFunction('changename');" id="saveButton" value="Clone Job">Clone Job</button>
             <!--<apex:commandButton action="doFunction('changename');" id="saveButton" value="Clone Job" />-->
            <apex:commandButton action="{!cancel}" id="cancelButton" value="Cancel" style="float:right;" />
        <apex:pageBlockSection title="Insert New Job Title" columns="3">
            <input type="text" value="insert new job name" onFocus="this.value=''" id="makeNewName"/>

public jobCloneClass(ApexPages.StandardController stdController){
j = (Job__c)stdController.getRecord();
clonedJob = (Job__c)clonedSObjects[0];
String newName = Apexpages.currentPage().getParameters().get('newName');
clonedJob.Name = null;
clonedJob.Name = newName;
upsert clonedJob;
PageReference jobPage = new PageReference('/' + clonedJob.id);
return jobPage;

where clonedJob was made by using sObject.clone() on the existing record, and all fields were selected in the relevant query.

Now, when I click the "clone job" button, a new record is indeed created, but instead of the cloned record updating it's name to be the string living in the text box id="makeNewName", the new record instead updates it's name to be it's ID.

I cannot figure out for the life of me why this is happening... does anyone have any insight?

  • 3
    I am not sure if apex:param puts the value into the url or not, but you could add public String newName{get; set;} to the controller and use <apex:param name="newName" assignTo="{!newName}" value="" /> on the page. (and take out the Apexpages line in controller) – zjeh Jan 25 '13 at 21:53

When the Name field on an SObject is not populated during an insert the platform uses the Id of the new record instead. So it quite likely Apexpages.currentPage().getParameters().get('newName'); is returning null.

apex:param seems to have lots of varying behaviors as a child of apex:commandButton, apex:commandLink and apex:actionFunction. I suspect using its HTTP POST parameter form is maybe getting lost in the AJAX routing in the case.

As zjeh has said, I personally would also try using apex:param's assignTo attribute to get the value placed into your view state and access it that way. I've got a few things doing this with apex:actionFunction and it seems to work.


public String newName {get;set;}

clonedJob.Name = this.newName;


<apex:actionFunction name="changeName" action="{!saveCloneObjects}">
    <apex:param name="newName" value="" assignTo="{!newName}" />
| improve this answer | |
  • 2
    Agree and based on the PageReference's getParameters() doc, I would expect the map to only contain params on the actual query string of the URL, so not in a POST: Returns a map of the query string parameters that are included in the page URL – Peter Knolle Jan 26 '13 at 0:54

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.