0

I have a table that I'm querying using SOQL and the query results should contain multiple rows. I'm trying to iterate through the query results and access attributes from each row of the query results. When I try to access the attributes using the dot operator, there is a pre-compiler error saying that the variable doesn't exist. Here is the code that is causing the error:

for(Available_records__c amc : [SELECT Id , Create_Period__c , Name, Record_Type__c, Frequency__c FROM Available_records__c WHERE Create_Period__c = TRUE]){
    if(amc.Frequency_c == weeklyCampaign.frequency){
                //Do some logic
    }
}

How can the individual attributes from the rows in the query results be accessed?

  • 2
    That error ('variable doesn't exist') often reflects a syntax error earlier in the same code unit preventing the variable declaration from being parsed correctly, not the field access as such. – David Reed Jan 29 '18 at 20:21
  • 2
    Specifically, at least in what you're showing here, there's an underscore missing in the field name in your if statement. – Thomas Taylor Jan 29 '18 at 20:22
  • Good catch, @ThomasTaylor. – David Reed Jan 29 '18 at 20:25
2

The same way you already do in your comparison:

if (amc.Frequency__c == someValue) { /* do stuff*/ }

So just do that with any other field as well:

String amcName = amc.Name;
Boolean createPeriod = amc.Create_Period__c
  • Yeah, it looks like the issue was just that an underscore was missing... -_- But I did also want to confirm that I had set everything up correctly since for some reason, at the time, the text suggestions weren't working. – loremIpsum1771 Jan 29 '18 at 20:37

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.