+ Start a Discussion
Frank TimmonsFrank Timmons 

Changes not getting saved to record

I created a VF page that will be used on the SF homepage, a checkbox users will click whenever they are going to be out of the office.  I'm testing the function but when I place a checkmark in the field, the change isn't getting saved to my own user record.  I thought everything was set up correctly but it's not working as expected.  Can someone help?

<apex:page standardController="User" standardStylesheets="true">
   <apex:form >
      <apex:pageBlock mode="edit">
         <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value=" Save "> </apex:commandButton>
         </apex:pageBlockButtons>
      <apex:pageBlockSection columns="2">
         <apex:inputField value="{!User.Out_of_Office__c}"></apex:inputField>
      </apex:pageBlockSection>
      </apex:pageblock>
   </apex:form>
</apex:page>
Best Answer chosen by Frank Timmons
William TranWilliam Tran
Okay, when you embed a VF, you need to do more stuffs, here's a way to get and update the user:

Apex Page:
<apex:page standardController="User" extensions="setUser" >
   <apex:form >
      <apex:pageBlock mode="edit">
         <apex:pageBlockButtons >
            <apex:commandButton action="{!saveUser}" value="Save"> </apex:commandButton>
         </apex:pageBlockButtons>
      <apex:pageBlockSection columns="2">
         <apex:inputField value="{!me.out_of_office__c}"></apex:inputField>
      </apex:pageBlockSection>
      </apex:pageblock>
   </apex:form>
</apex:page>

Extension class:
 
public class setUser{
    public user me {get;set;}
    public setUser(ApexPages.StandardController controller) {
        me = [select id, name, out_of_office__c from user where id = :UserInfo.getUserId()];
        }
        public PageReference saveUser() {update me; return null;}    
 }

I tested it and it worked for me.

Thx

All Answers

William TranWilliam Tran

Frank,

that should work.

Be sure to load the page with your user context that is, add ?id=0051a000000odGL   after your apex page.

to find your id go to setup/administer/Manage users/users click on you name,

you should see in the URL: https://na24.salesforce.com/0051a000000odGL?noredirect=1  where 0051a000000odGL is the ID for you as the user.

add ?id=0051a000000odGL  to the end of the apex page like below: my apex page is ooo (for out of office).

https://c.na24.visual.force.com/apex/ooo?id=0051a000000odGL

now load the page, change it, save and you should see it reflected.

Thx

 

Frank TimmonsFrank Timmons
A couple of questoins about your response:
  1. I don't have an Apex page to add the user ID to the URL.  I set up a VF page and a Homepage Component.  Where do I add the reference to the user?
  2. I'm not the only user who needs to click this box.  How do I reference all users becasuse we all have a different ID?  Is there a "GetUserID" function I could use?
William TranWilliam Tran
Okay, when you embed a VF, you need to do more stuffs, here's a way to get and update the user:

Apex Page:
<apex:page standardController="User" extensions="setUser" >
   <apex:form >
      <apex:pageBlock mode="edit">
         <apex:pageBlockButtons >
            <apex:commandButton action="{!saveUser}" value="Save"> </apex:commandButton>
         </apex:pageBlockButtons>
      <apex:pageBlockSection columns="2">
         <apex:inputField value="{!me.out_of_office__c}"></apex:inputField>
      </apex:pageBlockSection>
      </apex:pageblock>
   </apex:form>
</apex:page>

Extension class:
 
public class setUser{
    public user me {get;set;}
    public setUser(ApexPages.StandardController controller) {
        me = [select id, name, out_of_office__c from user where id = :UserInfo.getUserId()];
        }
        public PageReference saveUser() {update me; return null;}    
 }

I tested it and it worked for me.

Thx
This was selected as the best answer