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
Satya413Satya413 

Get Record ID from selected value in Lookup

Hi, 

I am trying to display the account record details as well as the related contacts and opportunities for a selected account record through lookup.
I am stuck at the point where I am unable to obtain the record id for the selected account record.
Below is my code. Any help is appreciated. 

VF Page:

<apex:page controller="accountdisplay">
    <apex:form>
        <apex:pageBlock title="Display Account Details">
            <apex:outputText value="Select Account: " />
            <apex:inputField value="{!con.accountid}"/>
            <apex:commandButton action = "{!getdetails}" value='Submit' />
            <apex:pageBlockSection title="Account Details">
                <apex:outputtext value="{!acc.name}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class AccountDisplay {

    public account acc {get; set;}
    public contact con {get; set;}
        
    public pagereference getdetails(){
        acc = [select id, name from account where id = :con.AccountId]; 
        return null;
    }
}

Thank You,
Satya
Best Answer chosen by Satya413
Keyur  ModiKeyur Modi
Hi,
Please do the changes in your class  as mention below
 
public class AccountDisplay {

    public account acc {get; set;}
    public contact con {get; set;}
    
    public AccountDisplay (){
    con=new contact();
    acc=new Account();
    }
        
    public pagereference getdetails(){
        system.debug('ccc=='+con);
        acc = [select id, name from account where id = :con.AccountId]; 
        return null;
    }
}

Thanks,
Keyur Modi
 

All Answers

Keyur  ModiKeyur Modi
Hi,
Please do the changes in your class  as mention below
 
public class AccountDisplay {

    public account acc {get; set;}
    public contact con {get; set;}
    
    public AccountDisplay (){
    con=new contact();
    acc=new Account();
    }
        
    public pagereference getdetails(){
        system.debug('ccc=='+con);
        acc = [select id, name from account where id = :con.AccountId]; 
        return null;
    }
}

Thanks,
Keyur Modi
 
This was selected as the best answer
Satya413Satya413
Hi,

Thanks for your reply. It worked. However, can you please explain your code especially the system.debug part.

Thank you,
Satya
Keyur  ModiKeyur Modi
Hi,
public class AccountDisplay {

    public account acc {get; set;}
    public contact con {get; set;}
    
    public AccountDisplay (){   // here I created constructor  for this class 
    con=new contact(); //  I create the instance of contact 
    acc=new Account(); //  I create the instance of Account 
    }
        
    public pagereference getdetails(){
        system.debug('ccc=='+con);  // This is to just check either value in con.account.id is comming or not  so that account query will work properly
        acc = [select id, name from account where id = :con.AccountId]; 
        return null;
    }
}

Only one diffrance between my code and your code is you had not created the instance of the two object contact and account ... and so i create the instace of both oject in constructor so whenever your page load it will create instace for both object. so it will not give the error which you faced before.
i hope this clarification will make your doubt clear .. if still you have any confusion feel free to ask,

Thanks,
Keyur Modi
Satya413Satya413
Cool. Thank you. 

Satya
TejashriTejashri
How can we write test class for above code any help please ?