+ Start a Discussion
Alo SenAlo Sen 

SOQL passes through Developer console - how to use in apex

I have used this query in the qurey editor of the Developer Console, got the right results and no error.

Select MetricA_IParent_Id__r.Name, MetricA_IParent_Id__r.ADRC_Total_Allocated_Dol_Expenditure__c, id, Reporting_Month__c, ADRC_Percent_of_allocated_Dol_expended__c,ADRC_Total_Dol_Expended__c  from Metrics_AI__c order by Reporting_Month__c

Here Name, MetricA_IParent_Id__r is the parent(2 fields from parent) and Metrics_AI__c the child.
I was trying to use this in apex class as follows when I get this error - System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Metrics_AI__c.ADRC_Total_Allocated_Dol_Expenditure__c

public class Metrics_rptAIReportController2{
    public List<Metrics_AI__c> ACRecord1 {get; set;}
     
    public Metrics_rptAIReportController2() {
        ACRecord1 = new List<Metrics_AI__c>();
        
        ACRecord1 = [Select MetricA_IParent_Id__r.Name, 
        MetricA_IParent_Id__r.ADRC_Total_Allocated_Dol_Expenditure__c, 
        id, 
        Reporting_Month__c, 
        ADRC_Percent_of_allocated_Dol_expended__c,
        ADRC_Total_Dol_Expended__c  
        from Metrics_AI__c 
        order by Reporting_Month__c DESC LIMIT 12];
     }
}

Thanks in advance. 
Pramodh KumarPramodh Kumar
Metrics_AI__c.ADRC_Total_Allocated_Dol_Expenditure__c is quered in the soql statement.
replace the with the following one.

Let me know if you have any other issue.
ACRecord1 = [Select MetricA_IParent_Id__r.Name, 
        MetricA_IParent_Id__r.ADRC_Total_Allocated_Dol_Expenditure__c, 
        id, 
        Reporting_Month__c, 
        ADRC_Percent_of_allocated_Dol_expended__c,
        ADRC_Total_Dol_Expended__c,
        ADRC_Total_Allocated_Dol_Expenditure__c   
        from Metrics_AI__c 
        order by Reporting_Month__c DESC LIMIT 12];
Thanks,
pRAMODH.
Alo SenAlo Sen
Sorry, the field 'ADRC_Total_Allocated_Dol_Expenditure__c' is a field in the parent record that I need to capture.  
Alo SenAlo Sen
The original query for which i had posted initially started to work without any error - I do not know what caused the error initially. 

ACRecord1 = new List<Metrics_AI__c>();
        
        ACRecord1 = [Select MetricA_IParent_Id__r.Name, 
        MetricA_IParent_Id__r.ADRC_Total_Allocated_Dol_Expenditure__c, 
        id, 
        Reporting_Month__c, 
        ADRC_Percent_of_allocated_Dol_expended__c,
        ADRC_Total_Dol_Expended__c,
        Total_To_Date__c       
        from Metrics_AI__c 
        order by Reporting_Month__c DESC LIMIT 12];

In the above code I need to capture the value of 'MetricA_IParent_Id__r.ADRC_Total_Allocated_Dol_Expenditure__c'  so I can show the field value in the line !tad.XXXXX in the vfpage.
The outer loop can be the child record 
for (Metrics_AI__c a : ACRecord1) {
         for( - how do I loop through to get the value of the ADRC_Total_Allocated_Dol_Expenditure__c)
      
        }
         and then how should I tie it to the vf page?

Here is the visual force page:

<apex:page Controller="Metrics_rptAIReportController2">
    <head>

    </head>
    <body>
     
        <apex:outputLink target="_Blank" value="/apex/Metrics_AI_ADRC_Dashboard" id="theLink" ><apex:image id="ITPR_EnlargeIcon" value="{!$Resource.ITPR_EnlargeIcon}" /></apex:outputLink>
        <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script src="{!URLFOR($Resource.Highcharts)}"></script>
        <script>

        $(function () {
        $('#container').highcharts({
            chart: {
                zoomType: 'xy'
            },
            title: {
                text: 'ADRC Dollar Amount Expended'
            },
         
            xAxis: [{
                    categories: [<apex:repeat value="{!ACRecord}" var="im">
                                '{!MONTH(im.Reporting_Month__c)}/{!YEAR(im.Reporting_Month__c)}',  
                            </apex:repeat>],
                            labels: {
                    rotation: -45,
                    align: 'right',
            
                  }
           }],
          
           
           yAxis: [{ // Primary yAxis
              labels: {
                    format: '{value}%',
                    style: {
                        color: '#89A54E'
                    }
                },
                 title: {
                    text: '% of Allocated Dollars Expended',
                    style: {
                        color: '#4572A7'
                    }
                }
                
            }, 
            { // Secondary yAxis
                title: {
                    text: 'Total Dollars Allocated',
                    style: {
                        color: '#4572A7'
                   }
                },
                labels: {
                   format: '{value}',
                    style: {
                        color: '#4572A7'
                   }
                },
                opposite: true
            }],
            tooltip: {
                shared: true
            },
            
            series: [
                {
                name: 'Total Allocated Dollar',
               
                type: 'column',
                yAxis: 1,
                data: [<apex:repeat value="{!ACRecord}" var="tad">
                                {!tad.XXXXX}, 
                            </apex:repeat>],
                tooltip: {
                    valueSuffix: ''
                }},
                 {
                name: 'Total Allocated Dollar Expended',
              
                type: 'column',
                yAxis: 1,
                data: [<apex:repeat value="{!ACRecord}" var="tade">
                                {!tade.ADRC_Total_Dol_Expended__c}, 
                            </apex:repeat>],
                tooltip: {
                    valueSuffix: ''
                }},
                 {
                name: '% of Allocated Dollars Expended',
             
                type: 'line',
                data: [<apex:repeat value="{!ACRecord}" var="pade">
                                {!pade.ADRC_Percent_of_allocated_Dol_expended__c}, 
                            </apex:repeat>],
                tooltip: {
                    valueSuffix: '%'
                }}
                
                
            ]
        });
    });
    </script>
    
    </body>
 </apex:page>

Thank you once again for your help.