[SalesForce] Add an already existing record to a related list

Basically, we have records that are Unassigned. We would like to be able to assign them to an Account. These records are displayed in a related list on the Account page. Of course, a new button is there, but would it be possible to build a button to re-parent the selected Unassigned objects to the currently viewed account?

I'm thinking this will require a custom-button along with a VF page + controller.

Best Answer

It is possible to do this without custom Apex or a custom controller by creating a new Visualforce page and a custom list button.

You didn't mention if this is a custom object or a standard object so in the example below I use the standard Contact object. You could swap out "Contact" in the Visualforce with the name of a custom object too though.

Below is the Visualforce and instructions for creating the button and adding it to a search layout.

Visualforce:

<apex:page standardController="Contact" recordSetVar="Contacts" id="muCt" tabStyle="Contacts__tab" sidebar="false">
    <apex:form id="muform">
        <apex:pageBlock title="Mass-Update Assigned Account" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!Contact.AccountId}" id="account">
                    <apex:actionSupport event="onchange" rerender="muSelectedList"/>
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom" id="muBtns">
                <apex:commandButton value="Save" action="{!save}" id="btnSav"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="btnCan"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Selected Contacts" id="muSelectedList">
            <apex:pageBlockTable value="{!selected}" var="ct" id="mutab">
                <apex:column value="{!ct.name}" id="oppName"/>
                <apex:column value="{!ct.AccountId}" id="accountId"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Custom Button:

  1. Navigate to Setup/Customize/Contacts/Buttons, Links, and Actions"
  2. Click the "New button or link" button.
  3. Give your new button a label, name and select "List button" for the display type.
  4. Select "Visualforce Page" for the Content Source.
  5. Select your newly created Visualforce page for the Content.

Add the new button to the Search Layout:

  1. Navigate to Customize/Contact/Search Layouts
  2. Edit the Contacts List View
  3. Add your new button from the list of available buttons

Navigate to your Contacts list and you should see your new button. Select the contacts you want to assign to an account. You then click your new custom list button and you'll be taken to your new Visualforce page where you can specify which account to assign to the contacts. Finally click the Save button to save the changes.