Hello I would like to know if there is a way to change record ownership in a Visualforce Page.

Currently this is part of the code of my VFP extension

private final Account acc {get;set;}

public my_extension(ApexPages.StandardController controller) {
  if(ApexPages.currentPage().getParameters().get('id') != null) {
    this.acc = (Account)controller.getRecord();

public PageReference next() {
  try {
    Account acc = [select OwnerId from Account where =: 
    acc.OwnerId = UserInfo.getUserId();
  catch(DmlException e) {
  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'failed to assign ownership');

And here is my VFP

<apex:page controller='my_controller' extensions='my_extension'>
<apex:pageMessages />
<apex:outputPanel id='all'>
  <apex:pageBlock title='My test page'>
<apex:PageBlockButtons location='top'>
<apex:commandButton value='Next' acction='{!next}' rerendered='{!hasNext}' />
<apex:pageBlockTable value={'!records'} var='a'>
<apex:column value='{!a.Id}' />
<apex:column value='{!a.Name}' />

Thanks :)

  • your Visual force is not showing. – Ronnie Nov 2 '17 at 2:32

Here is what I would do. It is simple I think and you don't need any big code.

<apex:page standardController="dmn__Client__c" extensions="Test">


        <apex:inlineEditSupport showOnEdit="projectInlineSave" hideOnEdit="editButton" event="ondblclick"  
                                    changedStyleClass="myBoldClass" resetFunction="resetInlineEdit" /> 
        <apex:pageBlock mode="mainDetail" >
            <apex:pageBlockSection columns="2">
                <apex:outputField value="{!dmn__Client__c.Name}" />
                    <apex:outputPanel id="ownerChangeBox">
                        <apex:outputLabel for="owner" value="{!$ObjectType.dmn__Client__c.fields.OwnerId.label}" />
                            <apex:outputField value="{!dmn__Client__c.OwnerId}" rendered="{!NOT(editingOwner)}" />
                            <apex:inputField value="{!dmn__Client__c.OwnerId}" rendered="{!editingOwner}" />
                            <apex:commandLink value="Change Owner" action="{!editOwner}" reRender="ownerChangeBox" />


            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Save" action="{!Save}" />
                <apex:commandButton value="Cancel" action="{!Cancel}"    />

And my controller is quite simple - you can implement it any way you choose to work with your own layout.

public class Test{

    public boolean editingOwner { get; set; }

    public Test( ApexPages.standardController controller ){
        editingOwner = false;

    public void editOwner(){
        if( !editingOwner ){
            editingOwner = true;
            editingOwner = false;

Dont mind my objects - they are just part of my dev objects - you can obviously use your own - but this is a working example i just slapped together for demo.

Before After

| improve this answer | |

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.