1

can you give a complete manual/page for referring Dynamic SOQL where as https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm never providing considerable information.

public class DynamicSOQL { 

    public sObject sobj{set;get;} 

    public List<sObject> sobje{set;get;} 

    public void main(){ 

        String myname='KSKSK'; 

        sobje=Database.query('select id,phone from account where name=:'+myname); 

        System.debug(sobj); 
    } 
}

Error

System.QueryException: sObject type 'accoount' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.

  • is there anything specific you are looking for ? means what you want to understand ? in doc everything mentioned – Ratan Paul Apr 15 '16 at 11:06
  • Yeah.. @sfdcweb – KS Kumaar Apr 15 '16 at 11:08
  • public class DynamicSOQL { public sObject sobj{set;get;} public List<sObject> sobje{set;get;} public void main(){ String myname='Burlington Textiles Corp of America'; sobje=Database.query('select id,phone from accoount where name=:'+myname); System.debug(sobj); } } This is the Exception that i am facing while executing System.QueryException: unexpected token: 'Textiles' – KS Kumaar Apr 15 '16 at 11:10
  • can you please add your soql what you are trying and what is the error in question ? – Ratan Paul Apr 15 '16 at 11:10
  • 1
    Its account not accoount – Vigneshwaran G Apr 15 '16 at 11:13
2

Dynamic SOQL queries are queries which are built on the fly like built by getting inputs from user selection.

It can use apex bind variables just like static SOQL by using : and reference the bind variable.

If there is no bind variable, then you need to provide values without :.

If the value references string, then you need to escape it.

Putting all together,

  1. Dynamic SOQL with apex bind variable

    String myname='Burlington Textiles Corp of America';
    sobje=Database.query('select id,phone from account where name=:myname');
    
  2. Dynamic SOQL without apex bind variable

    sobje=Database.query('select id,phone from account where name=\'Burlington Textiles Corp of America\'');
    

Hope it helps.

  • sobje=Database.query('select id,phone from account where name=\\'Burlington Textiles Corp of America\\''); expecting a right parentheses, found 'Burlington' – KS Kumaar Apr 15 '16 at 11:56
  • My bad. Its single \ not \\. – Vigneshwaran G Apr 15 '16 at 12:02
  • I got it Thank you so much – KS Kumaar Apr 15 '16 at 12:06
  • By performing all above, i got one doubt. please explain me what is bind variables in dynamic soql – KS Kumaar Apr 15 '16 at 12:29

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.