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
1
  • 4
    Where is your Database.query() ? Commented Apr 26, 2013 at 14:06

2 Answers 2

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 + '\'');

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.