List<Opportunity> opps = [
        SELECT  Id
        FROM    Opportunity
        WHERE Stagename Not In ('Closed Lost', 'Closed Won')];

closed as unclear what you're asking by Oleksandr Berehovskyi, Reshma, Derek F, glls, David Reed Feb 15 at 14:16

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    Your question is not clear – Santanu Boral Feb 15 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 at 13:37

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.


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 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 at 13:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.