3

According to this http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_soql.htm

I should be able to avoid casting using dyanmic soql

However when I try:

opportunity = 'select Name, isClosed, createdDate from opportunity where id=:oppid';

Where oppid is a String, I get

Save error: Illegal assignment from String to SOBJECT:Opportunity
3

You cannot cast just a SOQL query string, this should do what you need. While Database.query returns List of SObject and ordinarily you would have to cast these, as per the docs the runtime says you the trouble and a few characters in your code! As well as allowing you to cast it to a none array type as well!

Id oppId = '006G000000MPTmF';
Opportunity opp = Database.query('select Name, isClosed, createdDate from opportunity where id=:oppid');
1

Try this instead:

List<Opportunity> Opty = Database.query(
    'select Name, isClosed, createdDate from opportunity where id=:oppid');

Or, you could do this:

String OptyId = '006000000123456'; // Id could be retrieved from somewhere else, too
List<Opportunity> Opty = Database.query(
    'select Name, isClosed, createdDate from opportunity where id=\'' + OptyId + '\'');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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