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
Roger PavelleRoger Pavelle 

Using ID in SOQL query

I am trying to build an SOQL query inside a custom controller.  The query is supposed to return details of a selected record based on the record ID.  However, when I run the query I get an error saying "expecting a colon, found 'a0C1a00000115Xd' ".  If I put a colon in front of the ID, I get an error saying "Variable does not exist".  How can I fix this?
Best Answer chosen by Roger Pavelle
ManojjenaManojjena
Hi Roger ,

Modify your code liek below it will work .
 
private String selectedID = string.escapeSingleQuotes('a0C1a00000115Xd');
public Bolete__c getDetail(){
        Bolete__c detailResult = [SELECT Genus__c, Genus2__c, Genus3__c, regionSuspNC__c FROM Bolete__c  WHERE ID =: selectedID]
        return detailResult;
}

Let me know if it helps !!
Thanks
Manoj

All Answers

ManojjenaManojjena
Hi Roger ,

Can you post your query ? 
Roger PavelleRoger Pavelle
Here's the code.  Note that I've shortened the select statement, which shouldn't make any difference, and tried hard-coding the ID to make it easier to test the query.

    private String selectedID = string.escapeSingleQuotes(':a0C1a00000115Xd');

    public Bolete__c getDetail()
    {
        Bolete__c detailResult = Database.query(
            'SELECT Genus__c, Genus2__c, Genus3__c, regionSuspNC__c ' +
            'FROM Bolete__c ' +
            'WHERE ID = ' + selectedID);
        return detailResult;
    }
ManojjenaManojjena
Hi Roger ,

Modify your code liek below it will work .
 
private String selectedID = string.escapeSingleQuotes('a0C1a00000115Xd');
public Bolete__c getDetail(){
        Bolete__c detailResult = [SELECT Genus__c, Genus2__c, Genus3__c, regionSuspNC__c FROM Bolete__c  WHERE ID =: selectedID]
        return detailResult;
}

Let me know if it helps !!
Thanks
Manoj
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you.
private String selectedID = string.escapeSingleQuotes('a0C1a00000115Xd');

    public Bolete__c getDetail()
    {
        Bolete__c detailResult = Database.query(
            'SELECT Genus__c, Genus2__c, Genus3__c, regionSuspNC__c ' +
            'FROM Bolete__c ' +
            'WHERE ID = \'' +selectedID+'\'');
        return detailResult;
    }

One more example of you on dynamic query:-
public PageReference Search()
    {
        String query= '';
        String strFilter = '';
        if(acc.Name != null && (acc.Name ).trim() !='')
        {
           strFilter  = strFilter  +  ' where Name Like \''+acc.Name+'%\'' ;
        }
        if(acc.Phone != null && (acc.Phone).trim() !='' )
        {
           if(strFilter == '')
           { 
               strFilter  = strFilter  +  ' where Phone like \''+acc.Phone+'%\'' ;
           }
           else
           {
               strFilter  = strFilter  +  ' And Phone like \''+acc.Phone+'%\'' ;
           }
        }
        if(strFilter != '')
        {
            query = 'Select name ,id, phone from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(2);
        }
       return null;
    }
http://amitsalesforce.blogspot.in/search/label/Pagination

Please let us know if this will help you.

Thanks
Amit Chaudhary
ManojjenaManojjena
Hi ,

I think a simple query is appropriate  then Database.query or Database.getQueryLocator  for scenario like this .