0

I created a custom VF page to create custom object record. I have a Account lookup on page.

So whenever a user selects value from the lookup I would like to show dynamic picklist options on VF page by querying with LookupID for example I want to show names on dropdown, queried from selected lookup Id before saving the record.

Class:

public class createPriceRecord
{
    public Price__c price{get; set;}
    public Price__c priValue;
    public Id lookupId{get;set;}
    public Price__c ReqOpp;

    public createPriceRecord (ApexPages.StandardController controller) {
        price= (Price__c)controller.getRecord();
        stdCtrl=controller;
    }
    private ApexPages.StandardController stdCtrl;

    public void AccPopulated()
    {
        priValue= (Price__c)stdCtrl.getRecord();
    }

    public List<selectOption> getselectValues()
    {
        List<Prod__c> relatedprod = new List<Prod__c>();

        relatedprod = [Select Id,name from Prod__c where AccLookup__c.id =: priValue.Account__r.ID ]; // trying to get Account id from VF page

        List<SelectOption> Options = new List<SelectOption>();

        Options.add(new SelectOption('','-None-'));

        for(Prod__c prod:relatedprod)
        {
            Options.add(new SelectOption(prod.Id,prod.Name));
        }
        return Options;
    }

    public PageReference SaveRec(){

        price.Look_Up__c = lookupId;
        insert price;

        PageReference p=new PageReference('my.salesforce.com/'+price.Id);
        return p;

    }

}

Page :

<apex:page standardController="Price__c" extensions="createPriceRecord" tabStyle="Price__c">
 <apex:form >
    <apex:pageBlock title="Price Edit" mode="edit">
        <apex:pageBlockButtons>
            <apex:commandButton value="Save" action="{!SaveRec}"/>
        </apex:pageBlockButtons>

        <apex:pageBlockSection columns="2" title="Price Information">
            <apex:inputField value="{!price.AccountId}">
            <apex:actionSupport event="onchange" action="{!AccPopulated}" immediate="true" />
            </apex:inputfield>

            <apex:selectList value="{!lookupId}" title="Select" multiselect="false" size="1" >
            <apex:selectOptions value="{!selectValues}"/>
            </apex:selectList>

            <apex:inputField value="{!price.name}"/>
        </apex:PageBlockSection>
    </apex:pageBlock>
</apex:form >
</apex:page>
0

You have to use ActionSupport correctly to pass parameter to controller method and rerender the select list.

I have suggested following code.

  1. Create a fieldValue variable which will store AccountId from UI
  2. In AccPopulated() method capture fieldValue=price.AccountId;
  3. In actionSupport reRender="selectListValues" which will refresh the list.

Visualforce

<apex:inputField id="accountId" value="{!price.AccountId}">
    <apex:actionSupport event="onchange" reRender="selectListValues" action="{!AccPopulated}"/>          
</apex:inputfield>

<apex:selectList value="{!lookupId}" title="Select" multiselect="false" size="1" id="selectListValues">
    <apex:selectOptions value="{!selectValues}"/>
</apex:selectList>

Controller

//fieldValue will be captured from UI
public String fieldValue {get; set;}
public createPriceRecord (ApexPages.StandardController controller) {
    price= (Price__c)controller.getRecord();
    fieldValue = priValue.Account__r.ID;
    stdCtrl=controller;
}

public void AccPopulated()   
{
      fieldValue = price.AccountId;        
      getselectValues();  
}

public List<selectOption> getselectValues()
{
    List<Prod__c> relatedprod = new List<Prod__c>();        
    relatedprod = [Select Id,name from Prod__c where AccLookup__c.id =:fieldValue ]; // trying to get Account id from VF page

    List<SelectOption> Options = new List<SelectOption>();
    Options.add(new SelectOption('','-None-'));
    for(Prod__c prod:relatedprod)
    {
        Options.add(new SelectOption(prod.Id,prod.Name));
    }
    return Options;
}
| improve this answer | |
  • Thanks for your response.. implemented your logic. It is giving exception Too many query rows 50001. It is querying all the records regardless of the Account ID. – Learner Jan 26 '17 at 21:23
  • Very first time assign priValue.Account__r.ID to fieldValue in Controller, check the updated ans – Santanu Boral Jan 26 '17 at 21:30
  • still same issue and I observe It is not reRendering at all .When I choose Lookup the pick list values are not changing. – Learner Jan 26 '17 at 21:45
  • first check, values are getting passed in controller – Santanu Boral Jan 26 '17 at 21:46
  • Checked in debugs fieldValue is returning null. – Learner Jan 26 '17 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.