List<Opportunity> opps = [
        SELECT  Id
        FROM    Opportunity
        WHERE Stagename Not In ('Closed Lost', 'Closed Won')];
  • 6
    Your question is not clear Feb 15, 2019 at 9:58
  • 1
    This query does not need to be dynamic, and most likely shouldn't be. If you have a specific objective for using Dynamic SOQL, you should edit your question to clarify that.
    – David Reed
    Feb 15, 2019 at 13:37

3 Answers 3


I see you're new there, you can use the official documentation there ;

This may be what you're looking for ?

enter image description here


You can use below equivalent dynamic soql.

List<Opportunity> opps = Database.query( 'SELECT Id,Name,Amount,CloseDate,Stagename,Near_to_Close_Date__c '+
                               +'FROM Opportunity '+
                               +'WHERE Stagename Not In (\'Closed Lost\', \'Closed Won\')');

More on Dynamic soql.


You can use this link


String queryString = 'SELECT Id,Name,Amount,CloseDate,Stagename,Near_to_Close_Date__c FROM Opportunity WHERE Stagename Not In (\'Closed Lost\', \'Closed Won\')'; List<Opportunity> opps = Database.query(queryString);

  • 2
    Apex does not allow newlines in string literals.
    – David Reed
    Feb 15, 2019 at 13:15

Not the answer you're looking for? Browse other questions tagged .