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
Eager-2-LearnEager-2-Learn 

How to lookup an account without being on any specific record(s).

Hi,
I am trying to lookup an account (source and look up an account (destination) on a visualforce page.  Then when the Get Contacts button is clicked I plan to populate the lower half of the screen with the contacts associated with the source account that is selected.  Then I want another button that will copy the selected contacts to the destination account.  I need other things too occur with the contacts that are created but I think I can do that.  The issue I am having is getting the controller to capture the id of the accounts selected.

I discovered that I need the FORM tag to get the icon for the look up but I can't seem to get any javascript to work where it will pass the account ids to the controller. 
Can someone please help with this issue?  The code I have so far, good or bad is below.

VF Page:
<apex:page Controller="AccountMergerController" tabStyle="Account"> 
    <apex:form > 
        <apex:pageBlock title="Account Merger" > 
            <apex:pageBlockButtons >
                <apex:commandButton value="Get Source Contacts" action="{!getContacts}" reRender="pageBlockSection"/>            
            </apex:pageBlockButtons>    
            
            <apex:pageBlockSection title="Select the Source and Destination (Master) Accounts">                    
                <apex:inputField label="Source Account" value="{!sourceOpp.AccountId}" required="true" />                                 
                <apex:inputField label="Destination (Master) Account" value="{!sourceOpp.AccountId}" required="true" />                 
            </apex:pageBlockSection>
        </apex:pageBlock>        
    </apex:form>
    <apex:pageblock >
        <apex:pageBlockSection id="pageBlockSection2" title="Select Contacts to Copy to Destination Account)">
            
        </apex:pageBlockSection> 
    </apex:pageblock>
</apex:page>


Controller:

public with sharing class AccountMergerController {

    public Opportunity sourceOpp { get; set; } 
    public Opportunity destOpp { get; set; }  
    public Id id;
    public Id sourceId { get; set; }
    public Id destinationId { get; set; }
    
    public AccountMergerController() {

    }
    
    public void getContacts() {
        System.debug('sourceOpp: ' + sourceOpp.AccountId );
    }
}



 

Rohit K SethiRohit K Sethi
Hi Eager-2-Learn,

You need to assign memory to destOpp and sourceOpp . Then you can use it to any where in class.
That is why when you use to other method it raise an exception.

So assign memory first then use the object variables.
 
    public Opportunity sourceOpp { get; set; } 
    public Opportunity destOpp { get; set; }  
    public Id id;
    public Id sourceId { get; set; }
    public Id destinationId { get; set; }
    
    public AccountMergerController() {
        sourceOpp = new Opportunity();
        destOpp = new Opportunity ();
    }

Thanks.