function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bujjibujji 

error in datbase.query

Hi,

 

I have written this query i am getting "unexpected token: ="

 

accounts = Database.query(baseQuery +'WHERE Name ='+ABC+ 'AND Industry = \'BIG\'' +' limit '+pageSize);

 

What the error.How to resolve it.

 

Thanks,

Bujji

Best Answer chosen by Admin (Salesforce Developers) 
Rohit Kumar VermaRohit Kumar Verma

Hi bujji,

 

I have considered ABC as a string type variable and pageSize as Integer type.

 

Please find the running query.

 

string baseQuery = 'Select Id,Name from Account ';
integer pageSize = 10;
string ABC = 'a';
string finalQuery = baseQuery +'WHERE Name =\''+ABC+'\' AND Industry = \'BIG\'' +' limit '+pageSize;
system.debug('finalQuery : ' + finalQuery);
List<Account> accounts = Database.query(finalQuery);
system.debug('accounts : ' + accounts);

 

 

All Answers

Rohit Kumar VermaRohit Kumar Verma

Hi bujji,

 

I have considered ABC as a string type variable and pageSize as Integer type.

 

Please find the running query.

 

string baseQuery = 'Select Id,Name from Account ';
integer pageSize = 10;
string ABC = 'a';
string finalQuery = baseQuery +'WHERE Name =\''+ABC+'\' AND Industry = \'BIG\'' +' limit '+pageSize;
system.debug('finalQuery : ' + finalQuery);
List<Account> accounts = Database.query(finalQuery);
system.debug('accounts : ' + accounts);

 

 

This was selected as the best answer
bujjibujji

I tried same error boss.

 

Thanks,

Bujji

Rohit Kumar VermaRohit Kumar Verma

Dude, I feel there is a problem with your way of execution.

 

Please find the log.

 

Anonymous execution was successful.

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: /*string baseQuery = 'Select Id,Name from Account ';
Execute Anonymous: integer pageSize = 10;
Execute Anonymous: string ABC = 'a';
Execute Anonymous: string finalQuery = baseQuery +'WHERE Name =\''+ABC+'\' AND Industry = \'BIG\'' +' limit '+pageSize;
Execute Anonymous: //string finalQuery = baseQuery +'WHERE Name=:\''+ABC+'\'AND Industry = \'BIG\'' +' limit '+pageSize;
Execute Anonymous: system.debug('finalQuery : ' + finalQuery);
Execute Anonymous: List<Account> accounts = Database.query(finalQuery);
Execute Anonymous: system.debug('accounts : ' + accounts);*/
Execute Anonymous: string baseQuery = 'Select Id,Name from Account ';
Execute Anonymous: integer pageSize = 10;
Execute Anonymous: string ABC = 'Test';
Execute Anonymous: string finalQuery = baseQuery +'WHERE Name = \''+ABC+'\' AND Industry = \'Biotechnology\'' +' limit '+pageSize;
Execute Anonymous: system.debug('finalQuery : ' + finalQuery);
Execute Anonymous: List<Account> accounts = Database.query(finalQuery);
Execute Anonymous: system.debug('accounts : ' + accounts);
16:45:17.039 (39994000)|EXECUTION_STARTED
16:45:17.040 (40008000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
16:45:17.040 (40794000)|USER_DEBUG|[13]|DEBUG|finalQuery : Select Id,Name from Account WHERE Name = 'Test' AND Industry = 'Biotechnology' limit 10
16:45:17.045 (45322000)|SOQL_EXECUTE_BEGIN|[14]|Aggregations:0|Select Id,Name from Account WHERE Name = 'Test' AND Industry = 'Biotechnology' limit 10
16:45:17.056 (56700000)|SOQL_EXECUTE_END|[14]|Rows:1
16:45:17.056 (56870000)|USER_DEBUG|[15]|DEBUG|accounts : (Account:{Name=Test, Id=0019000000G0IS9AAN})
16:45:18.002 (56919000)|CUMULATIVE_LIMIT_USAGE
16:45:18.002|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 7 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

16:45:18.002|CUMULATIVE_LIMIT_USAGE_END

16:45:17.056 (56949000)|CODE_UNIT_FINISHED|execute_anonymous_apex
16:45:17.056 (56955000)|EXECUTION_FINISHED

bujjibujji

hey man it worked.I have done syntactical error.


Thanks Dude.

 

Thanks,

Bujji