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
Pedro Afonso Polo AlbaPedro Afonso Polo Alba 

How to retrieve a record from a list using sub queries?


Hi, guys.
i´d like to retrieve a information from a list that i made a sub querie.

Following controller with the visualforce page
 
public TesteGuiasList(){
        //Instancias construtor
        this.insumosList = new List<INSUMO__c>();
        this.complGuiaList = new List<GUIA_SOLIC_COMPL__c>();
    }
    
    public List<INSUMO__c> getInsumosList(){
      
        insumosList = [select Name,COD_TUSS_DEFINITIVO__c,(Select COD_DISTRIBUIDOR__c from Insumos_Aprovados__r),
                       (select Name,QTD_AUTORIZADA__c,QTD_UTILIZ_PREST__c,QTD_LIBERADA_AUDIT__c,QTD_UTILIZ_INTERM__c from ITENS_GUIA__r) 
                       from INSUMO__c ];
       
        return insumosList;
    }


now following the VF page
 
<apex:pageBlockSection title="INSUMOS" columns="1" collapsible="false"  id="pageBlockSectionOpme">
            <apex:pageBlockTable value="{!insumosList}" var="insumos" id="tbOpme">
                
                <apex:column value="{!insumos.Name}" />
                <apex:column value="{!insumos.COD_TUSS_DEFINITIVO__c}" /> 
                <apex:column value="{!insumos.COD_DISTRIBUIDOR__c}" />  
                
                
            </apex:pageBlockTable>
            
        </apex:pageBlockSection>


I want to show insumos.COD_DISTRIBUIDOR__c in the same colunm, and shows me the following error:   Invalid field COD_DISTRIBUIDOR__c for SObject INSUMO__c


I know what that means, but how to show a field i query on my controller and is not working in my VF.  
 
PS; If my question get confused or you need more information, please let me know!
 
Thnak´s folks!
Best Answer chosen by Pedro Afonso Polo Alba
JeffreyStevensJeffreyStevens
{!insumos.fromInsumos_Aprovados__r[0].COD_DISTRIBUIDOR__c}

Couple of notes: 
insumosList is a list of INSUMO__c - right?  So, for each one of INSUMO__c - you could have multiple fromInsumos_Aprovados__r and multiple fromITENS_GUIA__r.

So specifying the [0] get's you the first of those mltiples.  (you'll get an error if there isn't at least one in the list.).
 

All Answers

JeffreyStevensJeffreyStevens
{!insumos.fromInsumos_Aprovados__r[0].COD_DISTRIBUIDOR__c}

Couple of notes: 
insumosList is a list of INSUMO__c - right?  So, for each one of INSUMO__c - you could have multiple fromInsumos_Aprovados__r and multiple fromITENS_GUIA__r.

So specifying the [0] get's you the first of those mltiples.  (you'll get an error if there isn't at least one in the list.).
 
This was selected as the best answer
Pedro Afonso Polo AlbaPedro Afonso Polo Alba

Hi, JeffreyStevens.

 

Thank´s, yes i got this error because the list is empty, but im testing and it is working.

 

Once again, thank´s for your support!