0

I have a controller which creates a record (and consequently an ID). After the record is created, variables are set. However, when I attempt to access the variable after the controller action has run, it's not getting the updated values.

After I run the {!save} method the record is created BUT the controller reference variable is not updated. How can I get at that new ID value?

Javascript/VF

<apex:commandButton value="navigate to record" onclick="navigateToActivity(); return false;"/>    

<script>
   function navigateToActivity() 
   {
     var controllerValues = '{!recordID} {!geoLat}'
     alert(controllerValues);
   }
</script>

Controller

    public Activity__C act {get; set;}
    public User owner {get; set;}
    public boolean hasLocation {get; set;} 
    public double geoLat {get; set;}
    public double geoLong {get; set;}
    public string recordID {get; set;}

    public QuickCreateActivityController(){
       act = new Activity__c();
       owner = new user();   
       hasLocation = false;
       geoLat = 0.0;
       geoLong = 0.0;
       recordId = string.valueof(act.id);
    }

    public void save() {  

        system.debug(hasLocation);
        system.debug(string.valueof(geoLat));
        system.debug(string.valueof(geoLong));
        system.debug('pre insert id' + recordId);
        /**************ID is blank********/

        checkForWorkday();             
        insert act;
        recordID = string.valueof(act.Id);
        system.debug('post insert id' + recordId);
        /**************ID is Created********/
    }

1 Answer 1

1

you need to rerender this section

<apex:commandButton value="navigate to record" onclick="navigateToActivity(); return false;" rerender="panel" />    
<apex:outputpanel id="panel" >
<script>
   function navigateToActivity() 
   {
     var controllerValues = '{!recordID} {!geoLat}'
     alert(controllerValues);
   }
</script>
</apex:outputpanel>

then you will get updated value.

8
  • It's still coming up Null <apex:commandButton value="navigate to record" onclick="navigateToActivity(); return false;" rerender="jsPanel" />
    – tjcinnamon
    Jun 29, 2016 at 17:10
  • <apex:outputPanel id="jsPanel"> <script> function navigateToActivity() { var controllerValues = '{!recordID} {!geoLat}' alert(controllerValues);</script> </apex:outputPanel>
    – tjcinnamon
    Jun 29, 2016 at 17:10
  • @tjcinnamon where you are calling controller method here? Jun 29, 2016 at 17:23
  • yes, I created a secondary button for testing to make sure the write op finished: <button id="btnSave" onclick="prepToSavePosition(); return false;" >Log my location</button>
    – tjcinnamon
    Jun 29, 2016 at 17:25
  • 1
    @tjcinnamon then rerender this section from that action. And then check. Jun 29, 2016 at 17:27

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.