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
Anji PavuluriAnji Pavuluri 

i am trying to fetch the data from Account object with the help of dynamic query i am getting "unexpected token: <EOF> An unexpected error has occurred. Your development organization has been notified."

public class SOQL3_1 {
    public List<Account> accounts{set;get;}
    public SOQL3_1(){
        List<String> listField=new List<String>{'Name','Phone','Rating'};
          String queryString='SELECT Id';
        for(String s:listField){
            queryString=queryString+','+s;
        }
        queryString=queryString+'from Account';
        accounts=Database.query(queryString);
    }
}


<apex:page controller="SOQL3_1">
    <apex:pageBlock title="Accounts Details">
        <apex:pageBlockTable value="{!accounts}" var="a">
         <apex:column value="{!a.name}"/>
         <apex:column value="{!a.phone}"/>
         <apex:column value="{!a.rating}"/>
         <apex:column value="{!a.industry}"/> 
       </apex:pageBlockTable>
    </apex:pageBlock>

</apex:page>
Maharajan CMaharajan C
Hi Anji,

There is two changes in Apex Class:

1. Add space before from in :   queryString=queryString+'  from Account';

2.  Add the industry also in listField because you are reffering this field also in Page.
public class SOQL3_1 {
    public List<Account> accounts{set;get;}
    public SOQL3_1(){
        List<String> listField=new List<String>{'Name','Phone','Rating','industry'};
        String queryString='SELECT Id';
        for(String s:listField){
            queryString=queryString+','+s;
        }
        queryString=queryString+' from Account';
        system.debug('queryString--> ' + queryString );
        accounts=Database.query(queryString); 
    }
}

Thanks,
Maharajan.C
Malika Pathak 9Malika Pathak 9

Hi Anji Pavuluri,

you can use the below code to check 
 

public class SOQL3_1 {
    @AuraEnabled(cacheable=true)
    public static List<Account> SOQL3_12(){
        List<Account> accounts;
        List<String> listField=new List<String>{'Name,','Phone,','Rating'};
          String queryString='SELECT Id,';
        for(String s:listField){
            queryString=queryString+' '+s;
        }
        queryString=queryString+' from Account';
        System.debug('queryString----->'+queryString);
        accounts=Database.query(queryString);
        System.debug('accounts------>'+accounts);
        return accounts;
    }
}
 


 if you want to show the account in the Aura component you also have to write the Controller or helper js for the aura component and call the class which returns your accounts.
 For your Reference, you can take a look at that example 
 https://developer.salesforce.com/forums/?id=9062I000000Qux3QAC
 
thanks, Regards
Malika Pathak