• Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?

X-Squared On Demand

Salesforce solutions delivered

  • Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?
  • Tips and Tricks
  • Configuration
  • Development
You are here: Home / Salesforce CRM / Configuration / Project: Change Owner Button in Visualforce

Project: Change Owner Button in Visualforce

November 13, 2008 by David Schach 7 Comments

Salesforce Labs has put out a series of Mass Action packages on the AppExchange. There is Mass Transfer Opportunity Owner (uses AJAX), Mass Update Contact Addresses (AJAX), Mass Update Opportunity Owner, Mass Update Opportunity Close Dates, etc. All of these are "thick client" tools, meaning that data must be loaded onto your computer, altered, and then uploaded back to Salesforce. Ron Hess released a super bit of code, found in the Visualforce documentation and elsewhere, to Mass Update Opportunity Stage and Close Date. It could, theoretically, be expanded to other fields as well. This got me thinking about doing so with other objects. A while ago, I took the code for the Mass Delete button and altered it to change the owner of the selected records to the User clicking the button. It was like an Accept button, but I could use it for any object. Actually, it wasn't as easy as that. I had to make one button for each object, and maintenance was a pain--whenever I created a new object, I needed to clone my code. Drawing upon some code I found in the Developers Guide, I have written a Visualforce page to mass-change the owner of all selected records to any active User in Salesforce. Yes, it only works with Contacts for now, but my next step will be to use Dynamic Apex to pass the sObject name to the page and its extension/controller, allowing me to reuse one bit of code for multiple objects. We'll see how it goes. But in the meantime, enjoy this page that lets you perform a search, check which records you want to transfer, and then input a User. I've tested it with 50 records at once, so that should suffice for most uses. VF Page:
<apex:page standardController="Contact" recordSetVar="Contacts" id="updateOwnerPage">
<apex:form>
<apex:sectionheader title="Change Owner for Contacts"/>
<apex:pageblock mode="edit">
<apex:pagemessages />
<apex:pageblocksection title="Change" columns="1">
<apex:pageblocksectionitem> 
<apex:outputlabel for="owner">New Owner</apex:outputlabel>
<apex:inputfield id="owner" value="{!Contact.OwnerId}"/>
</apex:pageblocksectionitem>
</apex:pageblocksection>
<apex:pageblocksection title="Selected Contacts" columns="1">
<apex:pageblocktable value="{!selected}" var="j" bgcolor="#F3F3EC" width="100%" 
styleClass="list" rowClasses="dataRow" onRowMouseOver="hiOn(this);" onRowMouseOut="hiOff(this);">
<apex:column>
<apex:facet name="header">Contact Name</apex:facet>
<apex:outputlink value="{!URLFOR($Action.Contact.View, j.id)}">
{!j.FirstName} {!j.LastName}</apex:outputlink>
</apex:column>
<apex:column>
<apex:facet name="header">Account Name</apex:facet>
<apex:outputlink value="{!URLFOR($Action.Account.View, j.Account.id)}">
{!j.Account.Name}</apex:outputlink>
</apex:column>
<apex:column>
<apex:facet name="header">Current Owner</apex:facet>
{!j.Owner.Name}
</apex:column>
</apex:pageblocktable>
</apex:pageblocksection>
<apex:pageblockbuttons location="bottom">
<apex:commandbutton value="Save" action="{!save}"/>
<apex:commandbutton value="Cancel" action="{!cancel}"/>
</apex:pageblockbuttons>
</apex:pageblock>
</apex:form>
</apex:page>
Button: Go to Setup -> Contact -> Buttons and Links Create a new button, set it to execute in the current window with sidebar and header, and set it to call a Visualforce page. Select the page you just created. Then add the button to Contact Search Layout and to any Contact related lists you like. Voila!

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to email a link to a friend (Opens in new window) Email

Related

Filed Under: Configuration, Development, Salesforce, Visualforce, X-Squared On Demand Tagged With: Projects, Visualforce

← Project: Lookup to Picklist Inline Visualforce Page Layouts! →

Comments

  1. Depton says

    June 10, 2011 at 11:27

    Hi,

    I would like to create a change owner button (to shown besides the “edit” or ” delete” buttons) in a custom object
    The ownership is based on a look up field:(users)

    Assigned to

    Can you please provide some tips for it!!

    Thank you

    Reply
    • David Schach says

      June 11, 2011 at 09:59

      Make a button that opens a web page: //a&retURL=
      That’s it! Enjoy.

      Reply
  2. Adam says

    March 6, 2013 at 05:16

    This is exactly what I need, but need to modify for the case object. getting a few errors though?!

    Reply
    • David Schach says

      March 8, 2013 at 12:52

      With no specifics, I can’t help you. Did you just change every “Contact” to “Case?” What are the errors?

      Reply
  3. athi says

    May 3, 2017 at 18:53

    Is it possible to do same thing for all contacts?
    I just changed value=”{!selected}” as value=”{!Contacts}” but it doesnt seem working. It is not changing ownership. Also It display only 20 records

    Reply
    • David Schach says

      October 9, 2017 at 09:31

      This code requires a list such as on a search results page or on a related list. That’s how it populates the Selected list. Just doing Contacts won’t work here.

      Reply
  4. Julian Olsen says

    May 4, 2018 at 02:17

    Hi David,

    I had nearly no experience with VF, but was able to copy your “code” and adjust it to do the same with a custom object. Did you get further with the generic version? I would be really interested in the outcome.

    Thank you for the great work and especially for sharing!
    Julian

    Reply

Share Your ThoughtsCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Is it Dreamforce Yet?

Find out!

Find us on the AppExchange

Mass Update Contacts 2.0

Get It Now

Recent Posts

  • Prevent Duplicate Emails on Leads
  • Duplicate Record Item Enrichment and Auto-Deletion Code
  • Lightning Component With Running User Information
  • ChatterBINGO is Now Open-Source
  • Display Only My Role’s Records on a Report

Post Categories

Popular Tags

#df09 #df10 Akismet Apex AppBuilder AppExchange Appirio Astadia Blogs Chatter Configuration Content DreamFactory Dreamforce Eclipse IDE Enterprise Force.com Builder Force.com Platform Google Infowelders Integration Just for fun Lightning New Developments New Features Partner Program PersonAccount Projects Publisher Salesforce Salesforce1 Salesforce for Google Apps sfdcverse Sites Visualforce Web-to X-Squared On Demand

Find Us Online

  • Twitter
  • Facebook
  • LinkedIn
  • RSS

Subscribe

RSS Feed Comments

Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 164 other subscribers

Copyright © 2008–2025 X-Squared On Demand · Genesis Framework by StudioPress · WordPress · Log in