Skip to main content
added 367 characters in body
Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

You need to wrap any string literal in quotes. When using a dynamic query, you must also escape them.

'... WHERE SObjectType = \'Case\''
// missing:              ^^    ^^

Or you could also rewrite that as:

String caseType = '\'Case\'';
// wrap in quotes!    ^^    ^^

String soql = 'SELECT DeveloperName FROM RecordType WHERE SObectType = ' + caseType;

Or better yet, use merge variables:

String sObjectType = 'Case';
String soql = 'SELECT DeveloperName FROM RecordType WHERE SObjectType = :caseType';

As written, your current query would be the same as:

SELECT DeveloperName FROM RecordType WHERE SObjectType = Case

This query is obviously invalid. As stated above, the filter value needs to be wrapped in single quotes (as you do in the static version you include in the question).


A couple asides:

  1. The query language is named SOQL, not SQL.
  2. You should always favor static queries over dynamic when they are viable.
  3. When merging in strings provided by a user, make sure you escape single quotes to avoid injection vulnerabilities.

You need to wrap any string literal in quotes. When using a dynamic query, you must also escape them.

'... WHERE SObjectType = \'Case\''
// missing:              ^^    ^^

As written, your current query would be the same as:

SELECT DeveloperName FROM RecordType WHERE SObjectType = Case

This query is obviously invalid. As stated above, the filter value needs to be wrapped in single quotes (as you do in the static version you include in the question).


A couple asides:

  1. The query language is named SOQL, not SQL.
  2. You should always favor static queries over dynamic when they are viable.
  3. When merging in strings provided by a user, make sure you escape single quotes to avoid injection vulnerabilities.

You need to wrap any string literal in quotes. When using a dynamic query, you must also escape them.

'... WHERE SObjectType = \'Case\''
// missing:              ^^    ^^

Or you could also rewrite that as:

String caseType = '\'Case\'';
// wrap in quotes!    ^^    ^^

String soql = 'SELECT DeveloperName FROM RecordType WHERE SObectType = ' + caseType;

Or better yet, use merge variables:

String sObjectType = 'Case';
String soql = 'SELECT DeveloperName FROM RecordType WHERE SObjectType = :caseType';

As written, your current query would be the same as:

SELECT DeveloperName FROM RecordType WHERE SObjectType = Case

This query is obviously invalid. As stated above, the filter value needs to be wrapped in single quotes (as you do in the static version you include in the question).


A couple asides:

  1. The query language is named SOQL, not SQL.
  2. You should always favor static queries over dynamic when they are viable.
  3. When merging in strings provided by a user, make sure you escape single quotes to avoid injection vulnerabilities.
Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

You need to wrap any string literal in quotes. When using a dynamic query, you must also escape them.

'... WHERE SObjectType = \'Case\''
// missing:              ^^    ^^

As written, your current query would be the same as:

SELECT DeveloperName FROM RecordType WHERE SObjectType = Case

This query is obviously invalid. As stated above, the filter value needs to be wrapped in single quotes (as you do in the static version you include in the question).


A couple asides:

  1. The query language is named SOQL, not SQL.
  2. You should always favor static queries over dynamic when they are viable.
  3. When merging in strings provided by a user, make sure you escape single quotes to avoid injection vulnerabilities.