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
Sasha TsSasha Ts 

Method does not exist or incorrect signature: void query(String) from the type Database

Hi all,
quite easy question about Database.query method. I'm learning SF with trailhead.salesforce.com and I'm getting pretty strange error here. I'm using developer edition of SF so should be not problems with some code limits or restrictions here I hope.

https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/visualforce_fundamentals/units/visualforce_custom_controllers
 
public class ContactsListController {

    private String sortOrder = 'LastName';
    
    public List<Contact> getContacts() {
        
        List<Contact> results = Database.query(
            'SELECT Id, FirstName, LastName, Title, Email ' +
            'FROM Contact ' +
            'ORDER BY ' + sortOrder + ' ASC ' +
            'LIMIT 10'
        );
        return results;
    }

}

Error: Method does not exist or incorrect signature: void query(String) from the type Database

User-added image
Best Answer chosen by Sasha Ts
Sasha TsSasha Ts
Guys I really appreciate your help, thanks. Finally I've found an issue. That was an auto generated Database class. And after removing it everything became in it's right place. So problem is solved now.

The question why I missed that is quite easy to answer and it's up to SF creators team to resolve.
The auto suggest system was helped me to choose method for Database class and there was used full set of standard Database class methods. So I thought that I'm using correct standard Database class. But then the compiler used not exactly that one standard Database class but custom Database class which was auto generated(created) with an "lightningFirstApp" I've created.

So my advise to a newcomers is to check for any custom classes that could override standard ones because auto suggest system seems doesn't checks that.

All Answers

Waqar Hussain SFWaqar Hussain SF
public class ContactsListController {

    private String sortOrder {get; set;}
    
    public List<Contact> getContacts() {
        sortOrder = 'LastName';
        List<Contact> results = Database.query(
            'SELECT Id, FirstName, LastName, Title, Email ' +
            'FROM Contact ' +
            'ORDER BY ' + sortOrder + ' ASC ' +
            'LIMIT 10'
        );
        return results;
    }

}

 
Sasha TsSasha Ts
Hi Waqar Hussain SF! Thanks for your reply, but the question was another. The problem is: "Method does not exist or incorrect signature: void query(String) from the type Database". So the same problem with your code.User-added image
Waqar Hussain SFWaqar Hussain SF
The error is showing because sortOrder property is not accessible in the method, you will have to make this propery public to be accessible via method.
 
public String sortOrder {get; set;}

 
Sasha TsSasha Ts

Sadly that didn't help.

User-added image

Even to make my problem more clear I'll remove all not necessary things. And as you can see in the screenshot below the problem is still the same.
User-added image

Adarsh.SharmaAdarsh.Sharma
Hi Sasha,

please try this.

public class ContactsListController { 
    public String sortOrder {get;set;}
    public ContactsListController (){
      sortOrder  = 'LastName'; 
   }
    public List<Contact> getContacts() { 
        string sSOQL = 'SELECT Id, FirstName, LastName, Title, Email ';
               sSOQL+ = ' FROM Contact ';
               sSOQL+ = ' ORDER BY ' +sortOrder;
               sSOQL+ =  '    ASC limit 10';
        List<Contact> results = Database.query(sSOQL); 
        system.debug('@developer-->results:'+results);
        return results;
    }
​}

Hope to have helped!

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.

let me know if any issue occured.

Regards.

 
Sasha TsSasha Ts
Hi Brian O'Conner! Thanks for your reply. But that doesn't help. The error is still the same as you can see. And I think it isn't related to the code changes you made.
User-added image

The only line 
List<Contact> results = Database.query(sSOQL);
isn't compiling by the some reason I can't understand.

So I'm still looking for some explaination or way how could method List<sObject> query(String) from the type Database be used.
Sasha TsSasha Ts
Additional information. I've tried all available API Versions (from 36 till 40). That didn't change anything.
Sasha TsSasha Ts
Guys I really appreciate your help, thanks. Finally I've found an issue. That was an auto generated Database class. And after removing it everything became in it's right place. So problem is solved now.

The question why I missed that is quite easy to answer and it's up to SF creators team to resolve.
The auto suggest system was helped me to choose method for Database class and there was used full set of standard Database class methods. So I thought that I'm using correct standard Database class. But then the compiler used not exactly that one standard Database class but custom Database class which was auto generated(created) with an "lightningFirstApp" I've created.

So my advise to a newcomers is to check for any custom classes that could override standard ones because auto suggest system seems doesn't checks that.
This was selected as the best answer
Sumant SatpathySumant Satpathy
It seems the class is not saved. try to execute after saving the class. i faced the same issue and i resolved it by saving. you try the same @Sasha Ts
Smita HodiggeriSmita Hodiggeri
For anyone looking for an answer on how to resolve this. instead of using just Database.query use System.Database.query
Example code
public
public static List<Opportunity> getClosedWonOpportunities (Id accountId) {
        String stage = 'Closed Won';
        List<Opportunity> oppsRelatedToPassedAccount = new List<Opportunity>();
       String query = 'Select Id,Name,StageName,CloseDate,Amount,Account.Id from Opportunity where AccountId = :accountId AND StageName =:stage';
        oppsRelatedToPassedAccount = System.Database.query(query);

        System.debug(oppsRelatedToPassedAccount);
    //  The body of the method
    return oppsRelatedToPassedAccount;
    }