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
Thiruchuri AdityanThiruchuri Adityan 

fetch lookup value

I have two fields Opportunity(Lookup) and Products (Picklist).  On selection of opportunity i had written one soql query such that it retrieves only the products associated with opportunity.  But it is not working.

<apex:actionRegion >
                        <apex:inputField value="{!lineitem.OpportunityId}" >
                        <apex:actionsupport event="onChange" action="{!getprods}" reRender="thePanel"/>
<apex:selectList value="{!lineitem.custom__c}" size="1" id="thePanel">
                            <apex:selectOptions value="{!prods}">

and my controller is 

public List<selectOption> getprods()
            List<selectOption> options = new List<selectOption>();
            opp = [select PriceBook2Id from Opportunity where Id=:lineitem.OpportunityId];
            for (PriceBookEntry product : [SELECT Name from PriceBookEntry where PriceBook2Id=:opp.PriceBook2Id])
                options.add(new selectOption(product.Name, product.Name));
                return options;

For an existing product it is working.  But for a newly created record the page is not even opening and throwing an error "List has no rows for assignment to SObject"

Mani RenusMani Renus
Your query is based on Products list(where condition),it always returns null when there are no Products So

In Controller just add if condition before for loop to check weather list is empty or not

Thiruchuri AdityanThiruchuri Adityan
Here the problem ie regarding the "opp = [select PriceBook2Id from Opportunity where Id=:lineitem.OpportunityId];".  I am unable to fetch this value.  After selection of opportunity it is not being getted.  In the system.debug it is shwng 'null'
Mani RenusMani Renus
Just once check Opportunity id in debug logs

System.debug('Opportunity Id'+lineitem.OpportunityId);

If the value is passing then there is no records under that id or if  value is not passed then there is problem with 'lineitem.OpportunityId'
Thiruchuri AdityanThiruchuri Adityan
It is showing null value
Mani RenusMani Renus
Yes now how your are getting the 'lineitem.OpportunityId' value in you code
Thiruchuri AdityanThiruchuri Adityan
That is what I am saying it is not getting. But for an existing product when I click on edit it is showing the value of the selected opportunity..