Visualforce page to update custom field on current user record - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Derrick AbbeyDerrick Abbey 

Visualforce page to update custom field on current user record

Hello SF community,

I've created a visualforce page that I would like to display as a homepage component.  I've also created two custom fields on the user object that I would like that to be able to update based on the current user.  I'm using the "User" standard controller, here is a bit of my code for the input fields.  Do I need a custom controller or am I missing something simple?  I'm fairly new to Visualforce, so I'm still learning.  Thank you for any assistance!

<apex:page standardController="User" doctype="html-5.0">
                <apex:inputField value="{!User.Time_off_start_date__c }"
                    label="" style="width:125px" showDatePicker="false" type="date">
                </apex:inputField>
                <apex:inputField value="{! User.Time_off_end_date__c }"
                    label="" style="width:125px" showDatePicker="false" type="date">
                </apex:inputField>
                <apex:commandButton action="{! save }" value="Submit"/>
         
 
Best Answer chosen by Derrick Abbey
James SullivanJames Sullivan
Hi Derrick,

You either have to pass the current user id as a parameter in the page:
    ?id=xxxxxxx

Or you have to make a custom controller that will query for that record.

Hope that helps!

James Sullivan
CloudAnswers
Salesforce PowerPack (https://appexchange.salesforce.com/listingDetail?listingId=a0N30000000pvspEAA)

All Answers

Avi BAvi B
Derrick - I would suggest to post this question on the Developer forums or Salesforce stack exchange or better response. This community is focused on Non development related questions.
Developer forums: https://developer.salesforce.com/forums?dc=Apex_Code_Development
James SullivanJames Sullivan
Hi Derrick,

You either have to pass the current user id as a parameter in the page:
    ?id=xxxxxxx

Or you have to make a custom controller that will query for that record.

Hope that helps!

James Sullivan
CloudAnswers
Salesforce PowerPack (https://appexchange.salesforce.com/listingDetail?listingId=a0N30000000pvspEAA)
This was selected as the best answer
Derrick AbbeyDerrick Abbey
I will do that.  Thanks for the idea.
Derrick AbbeyDerrick Abbey
Hi James,

Thank you for your response.  I had tried adding the line:
<apex:param id="{!$User.id}"/>
However, I get an error that says "Literal value is required for attribute id in ..."

I tried adding  id="{!$User.id}" after the doctype in the apex page tag, but it gave me the same error. So I'm guessing I need a custom controller.
 
James SullivanJames Sullivan
The page actually needs to get the id of the user in the querystring.  You can't include it using a parameter as far as I know... for instance, you could load this visualforce page with:  /apex/mypage?id=<your user id>  to have it load your information.

When you say that you want to use this as a home page component, can you elaborate?  I'm just thinking of the ones that are static html, but maybe I'm misunderstanding.
Derrick AbbeyDerrick Abbey
Hi James,

I just want create a small component on the side bar that allows users to specify when they will be off.  I've created two custom fields on the user object for time off start and end dates.  I would like this element to be able to update those two fields when the user enters the dates and clicks submit.
User-added image
James SullivanJames Sullivan
Thanks for the visual - I'm with you now.  In that case, you're going to want to use a custom controller, or if you're more of a javascript guy and just new to salesforce, you can use Remote Objects in js (might be faster depending on where you're coming from):
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_remote_objects_example_simple.htm
Derrick AbbeyDerrick Abbey
Hi James,

Thank you very much.  I was just hoping to not have to write a custom controller.  But I like to learn, so I'll just treat it as more of a learning experience.  Thanks for the input.
James SullivanJames Sullivan
Wait - Remote Objects means you can write it all in javascript with no apex... that example has no apex code.
Derrick AbbeyDerrick Abbey
Hi James,

I'll read up on remote objects then.  I might be adding some javascript to this anyway, so perhaps that is the way to go.  Thanks again.