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
Rakesh KumarRakesh Kumar 

Capture Lookup value in Apex controller

Hi All,

 

I would like to capture the lookup field value in the apex controller.

 

VF page

<apex:inputField value="{!Object__c.lookupFiled__c}"/>

 

Apex controller..

 

here i am getting the lookup value as ID but i want to get it as value(Text).

 


Is there any way that we can get it on apex controller.

 

Thanks

Rakesh

Vinit_KumarVinit_Kumar

Hi Rakesh,

 

Try below :-

 

<apex:inputField value="{!Object__c.lookupFiled__r.name}"/> //where name is the field we want to access

 

LakshmanLakshman

You will have to store the text in some hidden field on page or Query for the Name in the controller.

 

1. First one can be done as:

Page:

<apex:inputField id="objectId" value="{!Object__c.lookupFiled__c}"/>
<apex:inputhidden id="objectName" value="{!lookupText}"/>
<apex:commandButton value="Save" action="{!Save}" onclick=" return setObjectName();"/>
<script>
function setObjectName() {
document.getElementById("{!$Component.objectName}").value = document.getElementById("{!$Component.objectId}").value;
return true;
}
</script>

 Controller:

public class ControllerExt{
public String lookupText {get;set;}//here u will get the Name
}

2. In the Second one the controller will look like

 

public class ControllerExt{
public PageReference save(){
String lookupText = [Select Name from LookupObject__c where Id =: Object__c.lookupFiled__c].Name;//here you will get the name and you can use it for your further logic
}
}

 Please give KUDOS if it helps you.

 

Regards,

Lakshman

 

 

 

avijchakavijchak

you always get Id in lookup , Simply create a formula field with name and show it on page ,and give a custom look up to the field

 

@vijit Capgemini developer

Mike PardMike Pard
Thank you 
Lakshman


It helped me too much in such a thing i was spending too much time


Best regards!!!