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
JntshumakerJntshumaker 

Setting the Owner of a Record

I'm trying to set the value of a visulaforce input field (Case.Owner) to the user creating the record, how would I go about doing this?

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

There are two options for you,

 

1) Just remove the

 

<apex:inputField value="{!case.ownerID}"/>

 

from page then ownerid will be set to current user id.

 

2) Use custom contrller class and create a method to save case then you can use the code that i gave in earlier post.

All Answers

Shashikant SharmaShashikant Sharma

If you want to set the owner of case to current user 

 

Case.OwnerId = UserInfo.getUserID();

 if any other particular user then 

 

Case.OwnerId = userObj.id;

 here userObj is the user which you want to make owner

JntshumakerJntshumaker

Within the framework of a visualforce page, where does this go?  Below is a snippet of my existing page:

 

<apex:page standardController="Case" sidebar="true">
        <apex:sectionHeader title="New Case" subtitle="{!case.Case_Name__c}"/>
        <apex:form >
            <apex:pageBlock title="New Case" id="thePageBlock" mode="new">
                <apex:pageMessages />
                <apex:pageBlockButtons >
                    <apex:commandButton value="Save" action="{!save}"/>
                    <apex:commandButton value="Cancel" action="{!cancel}"/>               
                </apex:pageBlockButtons>
                <apex:pageBlockSection title="Case Information" columns="2">
                <apex:inputField value="{!case.ownerID}"/>
                </apex:pageBlockSection>
            </apex:pageBlock>
        </apex:form>
</apex:page>

 

Shashikant SharmaShashikant Sharma

This will show on page as a lookup field which will open a lookup window for user selection. 

JntshumakerJntshumaker

I understand that my existing code just displays a lookup field for the user to input a value, however i want to use the code you supplied in the second post to set the owner of the case as the "running user".

Shashikant SharmaShashikant Sharma

There are two options for you,

 

1) Just remove the

 

<apex:inputField value="{!case.ownerID}"/>

 

from page then ownerid will be set to current user id.

 

2) Use custom contrller class and create a method to save case then you can use the code that i gave in earlier post.

This was selected as the best answer
JntshumakerJntshumaker

Thanks, just removing it from the "create new" VF page is what I did.  Since (by default) that field is read only until the record is created, i'm just going to make it a part of the process, that whoever creates a case is the owner of that case, unless you note otherwise after creation.