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
bristolbluebearbristolbluebear 

Custom Button to change Account owner and Contact owner

Hello

I have been directed here for some help from Answers 

I am looking for help creating a button that sits in the list view of both Accounts and Contacts that allows me to change owner on both objects simultaneously regardless of the object list view I in. 

I have been advised I need to write a VF page but I'm afraid I am not a developer. 

Can anyone help or who use such a function? Appreciate in advance. 

Marc 

 
Best Answer chosen by bristolbluebear
DeveloperSudDeveloperSud
Hi,

You probably entered the full code in the VF page .Please use the below code for your visulaforce page.
You will need the extension class 'testing'' you can refer it starting from line no 24 from my previous reply.
Please reply if you need anything else.
<apex:page standardController="Account" extensions="testing" recordSetVar="accounts" >
<apex:form >
<apex:pageBlock mode="inlineEdit">
 <apex:pageBlockButtons >
  <apex:commandButton action="{!save}" value="SAVE"/>
 </apex:pageBlockButtons>
 <apex:pageBlockSection >
    <apex:pageBlockTable value="{!acc}" var="a" border="1">
        <apex:column value="{!a.name}" style="padding:20px"/>
        <apex:column headerValue="Contact Names" >
        <apex:pageBlockTable value="{!a.contacts}" var="con"  >        
         <apex:column value="{!con.Name}" rendered="{!if(con.name!='null',true,false)}" />
        </apex:pageBlockTable>
        </apex:column>
        <apex:column headerValue="Owner Name">
          <apex:inputField value="{!a.ownerid}" />
        </apex:column>  
    </apex:pageBlockTable>
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


 

All Answers

Sunil MadanaSunil Madana
Hello Marc, below steps might help you.

Step-1: Setup--> Build--> Customize--> Accounts --> Buttons, Links, and Actions.
Once you land on this page, click on "New Button or Link" button to see a page like below.
User-added image
User-added image
Choose the list button to create a list button under Accounts.
Note: Repeat the same steps for contact by navigating to;
Setup--> Build--> Customize--> Contacts --> Buttons, Links, and Actions.

Step-2: Once the button is created, add it to the List view using the below link;
Setup--> Build--> Customize--> Accounts --> Search Layouts.
In the above page, click on "Edit" link next to "Accounts List View" and add the button which you created in Step-1 above.

Step-3: Once the button is added, put your logic in a visualforce page and update the owner of the account and contact records using the VF page.

Hope the solution helps, or else let me know. Thanks.
DeveloperSudDeveloperSud
Hi Mark,

You can use the below code for your VF page .'SF Issue-Fixer' has already given the steps to use it as a List button.
<apex:page standardController="Account" extensions="testing" recordSetVar="accounts" >
<apex:form >
<apex:pageBlock mode="inlineEdit">
 <apex:pageBlockButtons >
  <apex:commandButton action="{!save}" value="SAVE"/>
 </apex:pageBlockButtons>
 <apex:pageBlockSection >
    <apex:pageBlockTable value="{!acc}" var="a" border="1">
        <apex:column value="{!a.name}" style="padding:20px"/>
        <apex:column headerValue="Contact Names" >
        <apex:pageBlockTable value="{!a.contacts}" var="con"  >        
         <apex:column value="{!con.Name}" rendered="{!if(con.name!='null',true,false)}" />
        </apex:pageBlockTable>
        </apex:column>
        <apex:column headerValue="Owner Name">
          <apex:inputField value="{!a.ownerid}" />
        </apex:column>  
    </apex:pageBlockTable>
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
***extension Class***
public  with sharing class testing 
{

   public List<Account> acc{get;set;}
   public testing(ApexPages.StandardSetController controller1) 
    {
        controller1.setPageSize(10);
        acc = new List<Account>();
        acc =  [Select Id,Name,Ownerid,(Select Id,name From contacts) From Account];
        }
    
    
    public PageReference save(){
     update acc;
     return null;
    }   
}

Please confirm if you find this helpful.
Thanks!!!
bristolbluebearbristolbluebear
Hi Mark

many thanks for taking the time to do this

I am creating the VF page but its throwing up an error:

Error: AccountOwnerChangeButton line 23, column 1: Content is not allowed in trailing section
Error: Content is not allowed in trailing section.

many thanks in advance 
DeveloperSudDeveloperSud
Hi,

You probably entered the full code in the VF page .Please use the below code for your visulaforce page.
You will need the extension class 'testing'' you can refer it starting from line no 24 from my previous reply.
Please reply if you need anything else.
<apex:page standardController="Account" extensions="testing" recordSetVar="accounts" >
<apex:form >
<apex:pageBlock mode="inlineEdit">
 <apex:pageBlockButtons >
  <apex:commandButton action="{!save}" value="SAVE"/>
 </apex:pageBlockButtons>
 <apex:pageBlockSection >
    <apex:pageBlockTable value="{!acc}" var="a" border="1">
        <apex:column value="{!a.name}" style="padding:20px"/>
        <apex:column headerValue="Contact Names" >
        <apex:pageBlockTable value="{!a.contacts}" var="con"  >        
         <apex:column value="{!con.Name}" rendered="{!if(con.name!='null',true,false)}" />
        </apex:pageBlockTable>
        </apex:column>
        <apex:column headerValue="Owner Name">
          <apex:inputField value="{!a.ownerid}" />
        </apex:column>  
    </apex:pageBlockTable>
   </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


 
This was selected as the best answer
bristolbluebearbristolbluebear
Cheers, I'll try that and let you know the outcome :) 
PenchuReddyPenchuReddy
I got this code for custom change owner button:

<apex:page standardController="Account"
    recordSetVar="Accounts"
    extensions="selectedSizeWorkaround"
    showHeader="false"
    id="muopp"
>
    <apex:form id="muform">
       <!-- <apex:pageMessage summary="Selected Collection Size: {!mySelectedSize}"
            severity="info"
            id="mupms"
        />
        <apex:pageMessage summary="Record Set Size: {!myRecordsSize}"
            severity="info"
            id="mupmr"
        />  -->
        <apex:pageBlock title="Account Owner Change" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!Account.ownerid}" id="Owner">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!save}" id="butsav"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="butcan"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Selected Accounts" id="muselectedlist">
            <apex:pageBlockTable value="{!selected}" var="opp" id="mutab">
                <apex:column value="{!opp.name}" id="AccountName"/>
                <apex:column value="{!opp.Ownerid}" id="OwnerName"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
===================================
public class selectedSizeWorkaround {
    ApexPages.StandardSetController setCon;
    public selectedSizeWorkaround(ApexPages.StandardSetController controller) {
        setCon = controller;
    }
    public integer getMySelectedSize() {
        return setCon.getSelected().size();
    }
    public integer getMyRecordsSize() {
        return setCon.getRecords().size();
    } 
}
pravin joshi 25pravin joshi 25
Hi, This is helpfull for me also and it work also but my requirement is owner ship should be change only selected accounts not for every account .
 How we have to do ??
DMGmefullDMGmefull
I want to rename account owner to ABC and want to display in the Visualforce Page but the Vf page content is blank.. How to achieve this!!
Here is my Code...

public class ownerassigning {
    
    public User u                {get;set;}
    public User u2                {get;set;}
    public List<Account> acc     {get;set;}
    public  List<Account> acc1  {get;set;}
 
        public void getData(){
          
         u = [Select Id From User where Firstname = 'XYZ'];
     
     acc1 = [Select Id,Name,Rating,Industry,Ownerid from Account where ownerid=:u.id];
     
          u2 = [Select Id From User where Firstname = 'ABC'];
        for(Account a :acc1){
            a.ownerid=u2.id;
            acc.add(a);
        }
           update acc; 
        
    }
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<apex:page controller="ownerassigning">
 <!--   <apex:page standardcontroller="Account" extensions="ownerassigning"> -->
    <apex:form >
        <apex:pageBlock title="Account">
            <apex:pageBlockTable value="{!acc}" var="a">
                 <apex:column headerValue="Account Name">
                     <apex:outputField value="{!a.Name}" />
                     </apex:column>
                 <apex:column headerValue="Account Owner">
                     <apex:outputField value="{!a.OwnerId}" />
                     </apex:column>
                 <apex:column headerValue="Rating">
                     <apex:outputField value="{!a.rating}" />
                     </apex:column>
                 <apex:column headerValue="Industry">
                     <apex:outputField value="{!a.Industry}" />
                     </apex:column>
              </apex:pageBlockTable> 
        </apex:pageBlock>    
    </apex:form>
</apex:page>

Thanks if anyone can solve it!!
DMGmefullDMGmefull
Here is my VF Page!

Anyone Please Help me out in this....
Kristen BrickerKristen Bricker
Im getting stuck on the last step of creating this button. There is no option for content. Any idea how to fix this?User-added image
alo vinalo vin
Online games are very popular at present. Most people are interested to play games online. Here are the newest and updated Robloxids Music Codes to listen to songs. https://robloxids.net
James Moore 73James Moore 73
I also love playing MMORPGs and I use this game site (https://www.ssegold.com/) to buy mmo gold, items and powerleveling services. Thus I can have more time in enjoying games.
fgfh rygffgfgfh rygffg
This custom button feature is fantastic for streamlining owner changes in Salesforce! It's as efficient as using a top-notch study abroad agency singapore (https://www.sprouts-lab.com/) to plan your international education journey – making complex tasks feel effortless. Great job on this implementation!