+ Start a Discussion
Iqra TechIqra Tech 

Why this error occurs on my visual force page

User-added image
Deepali KulshresthaDeepali Kulshrestha
Hi Iqra,

I think there is a problem with your query in apex code and you have not to use try catch in the controller that's why this error you are getting in your Vf page.
I suggest you check your query if there is any query in your controller.
For more details refer to these links:
https://developer.salesforce.com/forums/?id=906F000000090VbIAI
http://www.infallibletechie.com/2016/07/systemqueryexception-unexpected-token-s.html

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Iqra TechIqra Tech
Can you please help me to add try catch block in my controller 
i tried to add try catch in my controller but getting error it will be very helpful to me please help me ....

Here is my controller class :

public with sharing class ObjectQueryController6 {
    public List<Floor_Plan_Mapping__c> conts{get;set;}
    public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    private Map<String, String> fldAPINameMap = new Map<String,String>();
    
    
    //Private set on SelectOption is so that values can be set from the controller.  
    public List<SelectOption> objectNames{public get; private set;}
    public String selectedObject {get; set;}
    public String selectedField {get;set;}
    public String selectedField1 {get;set;}
    public String selectedField2 {get;set;}
    public String selectedField3 {get;set;}
    public String selectedField4 {get;set;}
    public String selectedField5 {get;set;}
    public String Additionalfield1 {get;set;}
    public String Additionalfield2 {get;set;}
    public String Additionalfield3 {get;set;}
    public String Additionalfield4 {get;set;}
    public String Additionalfield5 {get;set;}
    public String Additionalfield6 {get;set;}
    
    public String selectedFieldAPI {get;set;}
    public String selectedFieldAPI1 {get;set;}
    public String selectedFieldAPI2 {get;set;}
    public String selectedFieldAPI3 {get;set;}
    public String selectedFieldAPI4 {get;set;}
    public String selectedFieldAPI5 {get;set;}
    public String AdditionalfieldAPI1 {get;set;}
    public String AdditionalfieldAPI2 {get;set;}
    public String AdditionalfieldAPI3 {get;set;}
    public String AdditionalfieldAPI4 {get;set;}
    public String AdditionalfieldAPI5 {get;set;}
    public String AdditionalfieldAPI6 {get;set;}
    

    
    private List<SelectOption> fields;
    Public string criteria{get;set;}
    transient public List<sObject> results{get;set;}
    public List<sObject> result1s{get;set;}
    public boolean queried {get;set;}
    
    // TO DO:
    // - Error handling when user selects ID or Name fields to query (Any Duplicate Fields)
    // - Handle queries of fields that are different than text
    // - Allow the selection of multiple fields
    // - Allow Mass Update of fields on queried records
    // - Insert new records
    
      public List<SelectOption> getObjFields() {        
        fields.clear();
        if(queried == null){queried = false;}
        system.debug('Page loading, Queried is: '+queried);
        
        // If an object was not selected yet, just populate the text 'Select object first'
        if(selectedObject == NULL){
            fields.add(new selectOption('Select Object First', 'Select Object First'));
        } 
        
        // if object was selected, create a list of the object's fields
        else { 
              fields.add(new selectOption('', '- None -'));           
            system.debug('$$$$$' + selectedObject);
            
            //We get a string of the sOjbects using the schemaMap.
            Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
            
            for(Schema.SObjectField sfield : fieldMap.Values())
            {
                schema.describefieldresult dfield = sfield.getDescribe();
                String fieldName = dfield.getName();      
                fields.add(new SelectOption(fieldName, dfield.getLabel()));
                fldAPINameMap.put(fieldName, dfield.getname());
            }
        }
        return fields;
    }
    
    // Constructor - this method will run when the controller is instantiated
    public ObjectQueryController6(){
        objectNames = initObjNames();
        fields = new List<SelectOption>();
        // List<sObject> results = new List<sObject>(); 
        String Criteria;
    }
    
    // Populate SelectOption list -
    
    // find all sObjects available in the organization
    
    private List<SelectOption> initObjNames() {
        List<SelectOption> objNames = new List<SelectOption>();
 
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
            objNames.add(new SelectOption(name,name));
           
        return objNames;
    }
    
    //Instantiate the list cast records.  We pull id, Name and other needed fields for our pageBlockTable in our VF page
    
    public void btnPerformQuery(){
        results=new List<sObject>();
        String Additionalfields = '';
        if(Additionalfield1 != null) {
        Additionalfields += fldAPINameMap.get(Additionalfield1)+',';
        }
        if(Additionalfield2 != null) {
        Additionalfields += fldAPINameMap.get(Additionalfield2)+',';
        }
       if(Additionalfield3 != null) {
       Additionalfields += fldAPINameMap.get(Additionalfield3)+',';
       }
       if(Additionalfield4 != null) {
       Additionalfields += fldAPINameMap.get(Additionalfield4)+',';
       }
       if(Additionalfield5 != null) {
       Additionalfields += fldAPINameMap.get(Additionalfield5)+',';
       }
       if(Additionalfield6 != null) {
       Additionalfields += fldAPINameMap.get(Additionalfield6)+',';
       }
        string queryFields = Additionalfields +''+fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+','+fldAPINameMap.get(selectedField2)+','+fldAPINameMap.get(selectedField3)+','+fldAPINameMap.get(selectedField4)+','+fldAPINameMap.get(selectedField5)+'';      
        string queryStr1 = 'SELECT createdDate,lastModifiedDate,'+queryFields+'  FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 1 ';
        system.debug('queryStr1-='+queryStr1);
        selectedFieldAPI = fldAPINameMap.get(selectedField);
        selectedFieldAPI1 = fldAPINameMap.get(selectedField1);
        selectedFieldAPI2 = fldAPINameMap.get(selectedField2);
        selectedFieldAPI3 = fldAPINameMap.get(selectedField3);
        selectedFieldAPI4 = fldAPINameMap.get(selectedField4);
        selectedFieldAPI5 = fldAPINameMap.get(selectedField5);
        AdditionalfieldAPI1=fldAPINameMap.get(Additionalfield1);
        AdditionalfieldAPI2=fldAPINameMap.get(Additionalfield2);
        AdditionalfieldAPI3=fldAPINameMap.get(Additionalfield3);
        AdditionalfieldAPI4=fldAPINameMap.get(Additionalfield4);
        AdditionalfieldAPI5=fldAPINameMap.get(Additionalfield5);
        AdditionalfieldAPI6=fldAPINameMap.get(Additionalfield6);
        results = database.query(queryStr1);
        result1s = results;
        System.Debug('results-='+results);
        queried = true;
        system.debug('Query performed, Queried is: '+queried);
        
    }
    
   
    List<Floor_Plan_Mapping__c > accountList = new List<Floor_Plan_Mapping__c>();

    public void Save()
    {  
     accountList.add(new Floor_Plan_Mapping__c(Object__c=selectedObject,Edition__c =selectedField,Opportunity_Id__c =selectedField1,Opportunity__c=selectedField2,Stage__c =selectedField3,Banner_Name__c=selectedField4, Stand_no__c=selectedField5,
                                                  Additional_field_1__c=Additionalfield1,Additional_field_2__c=Additionalfield2,Additional_field_3__c=Additionalfield3,Additional_field_4__c=Additionalfield4,
                                                  Additional_field_5__c=Additionalfield5,Additional_field_6__c=Additionalfield6));

       
       List<sObject> objects = new List<sObject>();
       objects.addAll((List<sObject>)(accountList)); 
            if (objects == null  && objects.isEmpty())
            {
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account Name'));
            }
            if (objects != null  && !objects.isEmpty())
           {
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Mapping Saved Sucessfully...'));
           }
       insert objects;
        system.debug('results-='+result1s);  
    }
}

 
Iqra TechIqra Tech
@Deepali Kulshrestha you there...
Deepali KulshresthaDeepali Kulshrestha
Hi,
I have added try catch in your code please make changes accordingly but i thing there is problem in your query that's why this exception occur.
By Try catch you can only avoid exception in your Vf page.

public with sharing class ObjectQueryController6 {
    public List<Floor_Plan_Mapping__c> conts{get;set;}
    public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    private Map<String, String> fldAPINameMap = new Map<String,String>();
    
    
    //Private set on SelectOption is so that values can be set from the controller.  
    public List<SelectOption> objectNames{public get; private set;}
    public String selectedObject {get; set;}
    public String selectedField {get;set;}
    public String selectedField1 {get;set;}
    public String selectedField2 {get;set;}
    public String selectedField3 {get;set;}
    public String selectedField4 {get;set;}
    public String selectedField5 {get;set;}
    public String Additionalfield1 {get;set;}
    public String Additionalfield2 {get;set;}
    public String Additionalfield3 {get;set;}
    public String Additionalfield4 {get;set;}
    public String Additionalfield5 {get;set;}
    public String Additionalfield6 {get;set;}
    
    public String selectedFieldAPI {get;set;}
    public String selectedFieldAPI1 {get;set;}
    public String selectedFieldAPI2 {get;set;}
    public String selectedFieldAPI3 {get;set;}
    public String selectedFieldAPI4 {get;set;}
    public String selectedFieldAPI5 {get;set;}
    public String AdditionalfieldAPI1 {get;set;}
    public String AdditionalfieldAPI2 {get;set;}
    public String AdditionalfieldAPI3 {get;set;}
    public String AdditionalfieldAPI4 {get;set;}
    public String AdditionalfieldAPI5 {get;set;}
    public String AdditionalfieldAPI6 {get;set;}
    

    
    private List<SelectOption> fields;
    Public string criteria{get;set;}
    transient public List<sObject> results{get;set;}
    public List<sObject> result1s{get;set;}
    public boolean queried {get;set;}
    
    // TO DO:
    // - Error handling when user selects ID or Name fields to query (Any Duplicate Fields)
    // - Handle queries of fields that are different than text
    // - Allow the selection of multiple fields
    // - Allow Mass Update of fields on queried records
    // - Insert new records
    
       public List<SelectOption> getObjFields() {     
       try{
        fields.clear();
        if(queried == null){queried = false;}
        system.debug('Page loading, Queried is: '+queried);
        
        // If an object was not selected yet, just populate the text 'Select object first'
        if(selectedObject == NULL){
            fields.add(new selectOption('Select Object First', 'Select Object First'));
        } 
        
        // if object was selected, create a list of the object's fields
        else { 
              fields.add(new selectOption('', '- None -'));           
            system.debug('$$$$$' + selectedObject);
            
            //We get a string of the sOjbects using the schemaMap.
            Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
            
            for(Schema.SObjectField sfield : fieldMap.Values())
            {
                schema.describefieldresult dfield = sfield.getDescribe();
                String fieldName = dfield.getName();      
                fields.add(new SelectOption(fieldName, dfield.getLabel()));
                fldAPINameMap.put(fieldName, dfield.getname());
            }
        }
        return fields;
      }
      catch(Exception e){
        system.debug('Exception at Line Number'+e.getLineNumber+'because of '+e.getMessage());
      }
    }
    
    // Constructor - this method will run when the controller is instantiated
    public ObjectQueryController6(){
        objectNames = initObjNames();
        fields = new List<SelectOption>();
        // List<sObject> results = new List<sObject>(); 
        String Criteria;
    }
    
    // Populate SelectOption list -
    
    // find all sObjects available in the organization
    
    private List<SelectOption> initObjNames() {
    try{
        List<SelectOption> objNames = new List<SelectOption>();
 
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
            objNames.add(new SelectOption(name,name));
           
        return objNames;
    }
    catch(Exception e){
      system.debug('Exception at Line Number'+e.getLineNumber()+'because of '+e.getMessage());
      return null;
    }
    
    }
    
    //Instantiate the list cast records.  We pull id, Name and other needed fields for our pageBlockTable in our VF page
    
    public void btnPerformQuery(){
    try{
        results=new List<sObject>();
        String Additionalfields = '';
        if(Additionalfield1 != null) {
        Additionalfields += fldAPINameMap.get(Additionalfield1)+',';
        }
        if(Additionalfield2 != null) {
        Additionalfields += fldAPINameMap.get(Additionalfield2)+',';
        }
       if(Additionalfield3 != null) {
       Additionalfields += fldAPINameMap.get(Additionalfield3)+',';
       }
       if(Additionalfield4 != null) {
       Additionalfields += fldAPINameMap.get(Additionalfield4)+',';
       }
       if(Additionalfield5 != null) {
       Additionalfields += fldAPINameMap.get(Additionalfield5)+',';
       }
       if(Additionalfield6 != null) {
       Additionalfields += fldAPINameMap.get(Additionalfield6)+',';
       }
        string queryFields = Additionalfields +''+fldAPINameMap.get(selectedField)+','+fldAPINameMap.get(selectedField1)+','+fldAPINameMap.get(selectedField2)+','+fldAPINameMap.get(selectedField3)+','+fldAPINameMap.get(selectedField4)+','+fldAPINameMap.get(selectedField5)+'';      
        string queryStr1 = 'SELECT createdDate,lastModifiedDate,'+queryFields+'  FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN (\'' + String.join( criteria.split( ',' ), '\',\'' ) +'\') limit 1 ';
        system.debug('queryStr1-='+queryStr1);
        selectedFieldAPI = fldAPINameMap.get(selectedField);
        selectedFieldAPI1 = fldAPINameMap.get(selectedField1);
        selectedFieldAPI2 = fldAPINameMap.get(selectedField2);
        selectedFieldAPI3 = fldAPINameMap.get(selectedField3);
        selectedFieldAPI4 = fldAPINameMap.get(selectedField4);
        selectedFieldAPI5 = fldAPINameMap.get(selectedField5);
        AdditionalfieldAPI1=fldAPINameMap.get(Additionalfield1);
        AdditionalfieldAPI2=fldAPINameMap.get(Additionalfield2);
        AdditionalfieldAPI3=fldAPINameMap.get(Additionalfield3);
        AdditionalfieldAPI4=fldAPINameMap.get(Additionalfield4);
        AdditionalfieldAPI5=fldAPINameMap.get(Additionalfield5);
        AdditionalfieldAPI6=fldAPINameMap.get(Additionalfield6);
        results = database.query(queryStr1);
        result1s = results;
        System.Debug('results-='+results);
        queried = true;
        system.debug('Query performed, Queried is: '+queried);
    }
catch(Exception e){
   system.debug('Exception at Line Number'+e.getLineNumber()+'because of '+e.getMessage());
}    
        
    }
    
   
    List<Floor_Plan_Mapping__c > accountList = new List<Floor_Plan_Mapping__c>();

    public void Save()
    {  
        try{
     accountList.add(new Floor_Plan_Mapping__c(Object__c=selectedObject,Edition__c =selectedField,Opportunity_Id__c =selectedField1,Opportunity__c=selectedField2,Stage__c =selectedField3,Banner_Name__c=selectedField4, Stand_no__c=selectedField5,
                                                  Additional_field_1__c=Additionalfield1,Additional_field_2__c=Additionalfield2,Additional_field_3__c=Additionalfield3,Additional_field_4__c=Additionalfield4,
                                                  Additional_field_5__c=Additionalfield5,Additional_field_6__c=Additionalfield6));

       
       List<sObject> objects = new List<sObject>();
       objects.addAll((List<sObject>)(accountList)); 
            if (objects == null  && objects.isEmpty())
            {
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account Name'));
            }
            if (objects != null  && !objects.isEmpty())
           {
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Mapping Saved Sucessfully...'));
           }
       insert objects;
        system.debug('results-='+result1s);  
        }
        catch(Exception e){
            system.debug('Exception at Line Number'+e.getLineNumber()+'because of '+e.getMessage());
        }
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
Iqra TechIqra Tech
I trird this code but getting error

Error: ObjectQueryController9 Compile Error: Variable does not exist: getLineNumber at line 81 column 51