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
Richa KRicha K 

Get the value of the lookup field in controller onchange

I am using a custom controller(Not extension controller) for a VF page. That VF page has a lookup to Accounts. I want the selected lookup field value in my controller. I am using actionSupport for this(On change of lookup).

 

Any help?

Noam.dganiNoam.dgani

Hi,

 

you dont need to use action support (unless i misunderstood and you want to use it).

 

to have a lookup in a visualforce page, you need to have an <apex:inputField> tag in your page, in which the value attribute is binded to a lookup to account. e.g <apex:inputField value="{!Contact.AccountId}" /> and the controller contains a public varibale called "Contact"

 

if you use it this way, you can access the value in your controller by accessing Contact.AccountId.

 

If this solves the issue for you, please mark it as resolved. if not, please reply

Richa KRicha K

No, I have already examined this when I built the page ..... I am not using extension controller.... I am using a custom controller... It gives me a null value....

Noam.dganiNoam.dgani

It shouldnt really matter whether its an extension or a custom controller.

 

could you post the relevant parts of your code?

Richa KRicha K

Thanks for your reply. Please look at this code. I have left with no hope. :) I tried removing immediate = true in actionsupport, but when its removed, it is giving me a blank value, which is a preevious value when the page first loaded.

 

Class:

public with sharing class Invoice_Helper
{
   
     public List<CashFlow__Invoice__c> getLstRecs() {
            return lstRecs;
        }
        Public Id statusSelected {get;set;}
        public pagereference newTets() 
        {
            //updateInvs();
            system.debug('=========here===');
            statusSelected = objInv.Account__c; 
            system.debug('============'+statusSelected );
            return null;
        }
        public void updateInvs() 
        {
        //Id id = ApexPages.currentPage().getParameters().get('id');
        //statusSelected = objInv.Account__c; 
        //system.debug('============'+id );
            lstRecs = [SELECT Id, Name, Account__c
                    FROM CashFlow__Invoice__c ];
                    //return null;
        }

      public Pagereference NewSave()
        {
            if((!lstCat.isEmpty() && lstCat.KeySet().size() > 1) && (!lstPay.isEmpty() && lstPay.KeySet().size() > 1))
            {
            objInv.CashFlow__Payment__c = lstCat.get(strPay);
            objInv.CashFlow__Category__c = lstCat.get(strCategory);
            insert objInv ;
            }
            else
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'The category you have selected is duplicate'));
            }
            PageReference acctPage = new ApexPages.StandardController(objInv).view();
            acctPage.setRedirect(true);
            return acctPage;
              //return null;
            //write a code returning to the record using objCon.Id
        }
           public Pagereference saveNnew()
            {
                
                if(!lstCat.isEmpty() && lstCat.KeySet().size() > 1)
                {
                objInv.CashFlow__Category__c = lstCat.get(strCategory);
                insert objInv ;
                }
                else
                {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'The category you have selected is duplicate'));
                }
                PageReference pageRef = new PageReference('/apex/Invoice_Override');
                pageRef.setRedirect(true);
                return pageRef;
    
            }
            public Pagereference cancelOn()
            {
                PageReference pageRef = new PageReference('/a01/o');
                pageRef.setRedirect(true);
                return pageRef;
            }
            



}

 Page:

<apex:page Controller="Invoice_Helper" >

<script>

function search()

{

  //  alert('hi');

    sendvaltocontroller();

}
</script>
 <apex:sectionHeader title="Invoice Edit" subtitle="New Invoice" />
     <apex:form >
      <apex:pageBlock title="Invoice Edit" id="block">
        <apex:pageBlockButtons >
        <apex:commandButton action="{!NewSave}" value="Save"/>
        <apex:commandButton action="{!saveNnew}" value="Save and New"/>
        <apex:commandButton action="{!cancelOn}" value="Cancel" immediate="true"/>
        </apex:pageBlockButtons>
        <apex:pageblockSection title="Information" collapsible="false">       
        
        <apex:inputField value="{!objInv.Account__c}" >
        <apex:actionSupport event="onchange" action="{!newTets}" immediate="true"/>
        </apex:inputField>
        <br/>&nbsp;&nbsp;&nbsp;&nbsp;<apex:inputField required="true" value="{!objInv.Invoice_Date__c}"/>
        <br/><apex:inputField required="true" value="{!objInv.Invoice_Name__c}"/>
        <br/><apex:inputField required="true" value="{!objInv.Gross_Amount__c}"/>
        <br/><apex:inputField required="true" value="{!objInv.Currency__c}"/>
        <br/><apex:inputField required="true" value="{!objInv.BTW__c}"/>
        <br/><apex:inputField required="true" value="{!objInv.Description__c}"/>

        </apex:pageblockSection>
        <apex:pageblockSection title="Catagory" collapsible="false">
        <apex:outputLabel >Category &nbsp;&nbsp;&nbsp;<apex:selectlist value="{!strCategory}" size="1">
          <apex:selectOptions value="{!Categories}"/>
        </apex:selectList></apex:outputLabel>
        </apex:pageblockSection>
        
        <apex:pageblockSection title="Dropbox Document" collapsible="false">
        <apex:inputField required="true" value="{!objInv.Document_Scan__c}"/>
        </apex:pageblockSection>
        
        <apex:pageblockSection title="Payment" collapsible="false">
        <apex:inputField required="true" value="{!objInv.Due_Date__c}"/>
        <br/><apex:outputLabel >&nbsp;&nbsp;Payment Method &nbsp;&nbsp;&nbsp;&nbsp;<apex:selectlist value="{!strPay}" size="1">
          <apex:selectOptions value="{!payMethods}"/>
        </apex:selectList></apex:outputLabel>
        <br/><apex:inputField required="true" value="{!objInv.Payment_Date__c}"/>
        </apex:pageblockSection>
        
        </apex:pageBlock>
        
        <apex:pageBlock title="List of Invoices" id="Account">
        <apex:pageBlockSection title="Invoices"  columns="1" collapsible="false">
        <apex:pageBlockTable value="{!lstRecs}" var="item">
          <apex:column value="{!item.Name}" headerValue="Invoices" width="100"/>
        </apex:pageBlockTable>
        </apex:pageBlockSection>
        </apex:pageBlock>
      
      </apex:form>
            
</apex:page>

 

Mayank_JoshiMayank_Joshi

Shailesh ,

 

This blog info Helps me a lot ,sounds like you are facing similar requirement : 

 

http://blog.jeffdouglas.com/2011/08/12/roll-your-own-salesforce-lookup-popup-window/ 

 

Let me know ,if you need to understand anything on this . I am able to develop my requirement based on this .

 

Warm Regards,

 

 

Achilles21Achilles21

Shailesh,

 

I know its been a year since you posted this. A little late question but were you able to find out the solution to your problem?

vivek.negi1.3152055305667039E1vivek.negi1.3152055305667039E1

Hi Shailesh / Achilles21

If your requirement is to get the value of selected lookup into apex then given below is the sample code.

I have an object A__c which has lookup field on Account and field name is Account__c

VF:
<apex:page standardController="A__c" extensions="Vishal">
<apex:form>
<apex:inputfield value="{!A__c.Account__c}">
<apex:actionSupport event="onchange" action="{!AccountPopulated}" /> 
</apex:inputField>

</apex:form>
</apex:page>

Apex:
public class vishal
{

public vishal(ApexPages.StandardController std) {
stdCtrl=std;
}

private ApexPages.StandardController stdCtrl;

public void AccountPopulated()
{
A__c cont=(A__c) stdCtrl.getRecord();
system.debug('>>>>>>>>>>>>>>>>>>>>>'+cont.Account__c);
}
}

Regards,
Vivek Negi

neha_gupta1.3902035174243218E12neha_gupta1.3902035174243218E12
It worked vivek....
thanx... :) :)
Mike PardMike Pard
vivek negl, neha_gupta:

If there is a value stored previously in the lookup field , it works very fine, it get a value,  (case 1)

but if there is no a value stored previously in the lookup field, it get a null value, (case 2)

what I am searching is to get the value you select in the lookup popup window  (case 2)


Any idea

Thank you 


Rregards!!!
 
Salim MohamedSalim Mohamed
Hi,
I also noticed those following cases:
WORKING : <apex:actionSupport event="onchange" action="{!xxx}" immediate="false"  />
WORKING : <apex:actionSupport event="onchange" action="{!xxx}"  />
NOT WORKING : <apex:actionSupport event="onchange" action="{!xxx}" immediate="true"  />
NOT WORKING : <apex:actionSupport event="onchange" action="{!xxx}" reRender="something" />
NOT WORKING : <apex:actionSupport event="onchange" action="{!xxx}" immediate="true" reRender="something" />

If anyone found a solution it would be great to share.

Regards