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
hwdavidhwdavid 

Refresh object after update in Controller

I have added a custom field to the User object and I want to update that object and have it available in my javascript.  As of now I can get the object but once I update it the visualforce page still gets the old value. I suspect my problem is related to my initial query not being refreshed but rerunning it has no effect. Here is what I have thus far.

 

public class CustomController {

 

  public User user {get; set;}

 

  public CustomController(ApexPages.StandardController stdController) {

     this.user = [select Id, CustomField__c from User where Id=:UserInfo.getUserId() limit 1];
  }

 

  

  public void updateUser() {
    update user;
  }

 

}

 


<apex:page showHeader="false" controller="CustomController">

 

<apex:form >

<apex:actionFunction name="updateCustomField" action="{!updateUser}" rerender="tokenBlock" >
<apex:param id="customField" name="customField" value="" assignTo="{!User.CustomField__c}"/>

</apex:actionFunction>
</apex:form>

 

:

:

 updateCustomField('new value');

 

So this all works but if I try to get that value via '{!$user.CustomField__c}' I get the old value... What am I missing.

 

Thanks

 

Jeff MayJeff May

have your controller method return PageReference instead of void, and return null.

 

public PageReference updateUser(){

 

   return null;

}

hwdavidhwdavid

Thanks for replying!

 

I tried that:

 

  public PageReference updateUser() {
    update user;

    return null;
  }

 

It saves the object correctly but my call to {!$user.CustomField__c} gives me the old value still and not the updated one. What am I missing?

hwdavidhwdavid

How can I get the variable from the controller at runtime?