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
gaurav arora 33gaurav arora 33 

Get Id from lookup inputfield.

I have a Code in which i am using apex:inputField for lookup field. when i am getting this field value in script it gives me the name of the record selected.
I want to get the ID of the record selected either in javascript variable or in apex controller.
Page------------------------------->>

<apex:page Controller="ApexParamController">
    <script>
    function fire(){
        var fr=document.getElementById('{!$Component.frm.acc}').value;
        alert('test:: '+fr);
    }
    </script>
    <apex:form id="frm">
        <apex:inputfield value="{!contact.AccountId}" id="acc"/>
        <apex:commandButton onclick="fire();" value="Save" action="{!print}" />
    </apex:form>
</apex:page>
 
Class------------------->>

public class ApexParamController {
    Public Contact contact{get;set;}
    public void print(){
        contact = new Contact();
        system.debug('Account::'+contact.AccountID);
    }
}

 
Best Answer chosen by gaurav arora 33
Aslam ChaudharyAslam Chaudhary
use below code 
public class ApexParamController {

    Public Contact contact{get;set;}
    
    public ApexParamController (){
      
      contact = new Contact();

    }

    public void print(){

        
        system.debug('Account::'+contact.AccountID);

    }

}

All Answers

Aslam ChaudharyAslam Chaudhary
use below code 
public class ApexParamController {

    Public Contact contact{get;set;}
    
    public ApexParamController (){
      
      contact = new Contact();

    }

    public void print(){

        
        system.debug('Account::'+contact.AccountID);

    }

}
This was selected as the best answer
gaurav arora 33gaurav arora 33
Thanks Aslam.
Its solved my issue.