Nested Dynamic SOQL Query
I am Trying to write a dynamic SOQL Query with a nested query.
@AuraEnabled
public static List<Books__c> filterRecords(String bookNumber, String PDCN, String Brand, String Type){
System.debug('bookNumber__c->'+bookNumber__c);
System.debug('PDCN->'+PDCN);
System.debug('Brand->'+Brand);
String query = 'SELECT Id,Content__c,ForecastWeekIndicator__c,Generic_PDCN__c,isExpired__c, '+
'BrandStandardName__c,DPForecast_BaseUnits__c,ForecastStatus__c,Generic_DateTime__c, '+
'Type__c,OperationalForecast_BaseUnits__c,PackageStandardName__c,bookNumber__c__c '+
'FROM Books__c WHERE Type__c =:Type '+
'AND Id NOT IN (SELECT Books__c FROM User_Specific_Books__c '+
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
if(string.isNotEmpty(bookNumber__c) && bookNumber__c!=null){
query = query + ' AND bookNumber__c__c =: bookNumber__c';
}
if(string.isNotEmpty(PDCN) && PDCN != null){
query = query + ' AND Generic_PDCN__c =: PDCN';
}
if(string.isNotEmpty(Brand) && Brand != null){
query = query + ' AND BrandStandardName__c =: Brand';
}
System.debug('query--->'+query);
List<Notification__c> lst;
try{
lst = Database.query(query);
system.debug('List--->'+lst);
}catch(Exception e){
system.debug(e.getMessage());
}
if(!lst.isEmpty()){
return lst;
}
else
return null;
}
}
and, I am getting exception expecting a right parentheses, found '('.
Any explanation would be appreciated.
apex soql
add a comment |
I am Trying to write a dynamic SOQL Query with a nested query.
@AuraEnabled
public static List<Books__c> filterRecords(String bookNumber, String PDCN, String Brand, String Type){
System.debug('bookNumber__c->'+bookNumber__c);
System.debug('PDCN->'+PDCN);
System.debug('Brand->'+Brand);
String query = 'SELECT Id,Content__c,ForecastWeekIndicator__c,Generic_PDCN__c,isExpired__c, '+
'BrandStandardName__c,DPForecast_BaseUnits__c,ForecastStatus__c,Generic_DateTime__c, '+
'Type__c,OperationalForecast_BaseUnits__c,PackageStandardName__c,bookNumber__c__c '+
'FROM Books__c WHERE Type__c =:Type '+
'AND Id NOT IN (SELECT Books__c FROM User_Specific_Books__c '+
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
if(string.isNotEmpty(bookNumber__c) && bookNumber__c!=null){
query = query + ' AND bookNumber__c__c =: bookNumber__c';
}
if(string.isNotEmpty(PDCN) && PDCN != null){
query = query + ' AND Generic_PDCN__c =: PDCN';
}
if(string.isNotEmpty(Brand) && Brand != null){
query = query + ' AND BrandStandardName__c =: Brand';
}
System.debug('query--->'+query);
List<Notification__c> lst;
try{
lst = Database.query(query);
system.debug('List--->'+lst);
}catch(Exception e){
system.debug(e.getMessage());
}
if(!lst.isEmpty()){
return lst;
}
else
return null;
}
}
and, I am getting exception expecting a right parentheses, found '('.
Any explanation would be appreciated.
apex soql
add a comment |
I am Trying to write a dynamic SOQL Query with a nested query.
@AuraEnabled
public static List<Books__c> filterRecords(String bookNumber, String PDCN, String Brand, String Type){
System.debug('bookNumber__c->'+bookNumber__c);
System.debug('PDCN->'+PDCN);
System.debug('Brand->'+Brand);
String query = 'SELECT Id,Content__c,ForecastWeekIndicator__c,Generic_PDCN__c,isExpired__c, '+
'BrandStandardName__c,DPForecast_BaseUnits__c,ForecastStatus__c,Generic_DateTime__c, '+
'Type__c,OperationalForecast_BaseUnits__c,PackageStandardName__c,bookNumber__c__c '+
'FROM Books__c WHERE Type__c =:Type '+
'AND Id NOT IN (SELECT Books__c FROM User_Specific_Books__c '+
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
if(string.isNotEmpty(bookNumber__c) && bookNumber__c!=null){
query = query + ' AND bookNumber__c__c =: bookNumber__c';
}
if(string.isNotEmpty(PDCN) && PDCN != null){
query = query + ' AND Generic_PDCN__c =: PDCN';
}
if(string.isNotEmpty(Brand) && Brand != null){
query = query + ' AND BrandStandardName__c =: Brand';
}
System.debug('query--->'+query);
List<Notification__c> lst;
try{
lst = Database.query(query);
system.debug('List--->'+lst);
}catch(Exception e){
system.debug(e.getMessage());
}
if(!lst.isEmpty()){
return lst;
}
else
return null;
}
}
and, I am getting exception expecting a right parentheses, found '('.
Any explanation would be appreciated.
apex soql
I am Trying to write a dynamic SOQL Query with a nested query.
@AuraEnabled
public static List<Books__c> filterRecords(String bookNumber, String PDCN, String Brand, String Type){
System.debug('bookNumber__c->'+bookNumber__c);
System.debug('PDCN->'+PDCN);
System.debug('Brand->'+Brand);
String query = 'SELECT Id,Content__c,ForecastWeekIndicator__c,Generic_PDCN__c,isExpired__c, '+
'BrandStandardName__c,DPForecast_BaseUnits__c,ForecastStatus__c,Generic_DateTime__c, '+
'Type__c,OperationalForecast_BaseUnits__c,PackageStandardName__c,bookNumber__c__c '+
'FROM Books__c WHERE Type__c =:Type '+
'AND Id NOT IN (SELECT Books__c FROM User_Specific_Books__c '+
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
if(string.isNotEmpty(bookNumber__c) && bookNumber__c!=null){
query = query + ' AND bookNumber__c__c =: bookNumber__c';
}
if(string.isNotEmpty(PDCN) && PDCN != null){
query = query + ' AND Generic_PDCN__c =: PDCN';
}
if(string.isNotEmpty(Brand) && Brand != null){
query = query + ' AND BrandStandardName__c =: Brand';
}
System.debug('query--->'+query);
List<Notification__c> lst;
try{
lst = Database.query(query);
system.debug('List--->'+lst);
}catch(Exception e){
system.debug(e.getMessage());
}
if(!lst.isEmpty()){
return lst;
}
else
return null;
}
}
and, I am getting exception expecting a right parentheses, found '('.
Any explanation would be appreciated.
apex soql
apex soql
edited 2 days ago
Mark Pond
18.5k13288
18.5k13288
asked 2 days ago
Thomas PeteThomas Pete
457
457
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can't use actual Apex in dynamic queries. Only "simple" variables are allowed. The problem is in the following line.
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
Assign the value to a variable first:
Id userId = UserInfo.getUserId();
...
'WHERE Books_Delete__c = TRUE AND User__c = :userId)';
Worked! Thanks.
– Thomas Pete
2 days ago
@ThomasPete you're welcome! P.S. String.isNotBlank also checks for null values, no need to check that separately in your code.
– sfdcfox
2 days ago
add a comment |
You can't use dot reference in dynamic query. Here is your problem:
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())'
You need to add some context variable to store the Id
and then change it to:
'WHERE Books_Delete__c = TRUE AND User__c = :runningUserId'
Thanks, Adding a context variable did it!
– Thomas Pete
2 days ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f254321%2fnested-dynamic-soql-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't use actual Apex in dynamic queries. Only "simple" variables are allowed. The problem is in the following line.
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
Assign the value to a variable first:
Id userId = UserInfo.getUserId();
...
'WHERE Books_Delete__c = TRUE AND User__c = :userId)';
Worked! Thanks.
– Thomas Pete
2 days ago
@ThomasPete you're welcome! P.S. String.isNotBlank also checks for null values, no need to check that separately in your code.
– sfdcfox
2 days ago
add a comment |
You can't use actual Apex in dynamic queries. Only "simple" variables are allowed. The problem is in the following line.
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
Assign the value to a variable first:
Id userId = UserInfo.getUserId();
...
'WHERE Books_Delete__c = TRUE AND User__c = :userId)';
Worked! Thanks.
– Thomas Pete
2 days ago
@ThomasPete you're welcome! P.S. String.isNotBlank also checks for null values, no need to check that separately in your code.
– sfdcfox
2 days ago
add a comment |
You can't use actual Apex in dynamic queries. Only "simple" variables are allowed. The problem is in the following line.
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
Assign the value to a variable first:
Id userId = UserInfo.getUserId();
...
'WHERE Books_Delete__c = TRUE AND User__c = :userId)';
You can't use actual Apex in dynamic queries. Only "simple" variables are allowed. The problem is in the following line.
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())';
Assign the value to a variable first:
Id userId = UserInfo.getUserId();
...
'WHERE Books_Delete__c = TRUE AND User__c = :userId)';
answered 2 days ago
sfdcfoxsfdcfox
260k12206451
260k12206451
Worked! Thanks.
– Thomas Pete
2 days ago
@ThomasPete you're welcome! P.S. String.isNotBlank also checks for null values, no need to check that separately in your code.
– sfdcfox
2 days ago
add a comment |
Worked! Thanks.
– Thomas Pete
2 days ago
@ThomasPete you're welcome! P.S. String.isNotBlank also checks for null values, no need to check that separately in your code.
– sfdcfox
2 days ago
Worked! Thanks.
– Thomas Pete
2 days ago
Worked! Thanks.
– Thomas Pete
2 days ago
@ThomasPete you're welcome! P.S. String.isNotBlank also checks for null values, no need to check that separately in your code.
– sfdcfox
2 days ago
@ThomasPete you're welcome! P.S. String.isNotBlank also checks for null values, no need to check that separately in your code.
– sfdcfox
2 days ago
add a comment |
You can't use dot reference in dynamic query. Here is your problem:
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())'
You need to add some context variable to store the Id
and then change it to:
'WHERE Books_Delete__c = TRUE AND User__c = :runningUserId'
Thanks, Adding a context variable did it!
– Thomas Pete
2 days ago
add a comment |
You can't use dot reference in dynamic query. Here is your problem:
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())'
You need to add some context variable to store the Id
and then change it to:
'WHERE Books_Delete__c = TRUE AND User__c = :runningUserId'
Thanks, Adding a context variable did it!
– Thomas Pete
2 days ago
add a comment |
You can't use dot reference in dynamic query. Here is your problem:
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())'
You need to add some context variable to store the Id
and then change it to:
'WHERE Books_Delete__c = TRUE AND User__c = :runningUserId'
You can't use dot reference in dynamic query. Here is your problem:
'WHERE Books_Delete__c = TRUE AND User__c = : UserInfo.getUserId())'
You need to add some context variable to store the Id
and then change it to:
'WHERE Books_Delete__c = TRUE AND User__c = :runningUserId'
answered 2 days ago
Adrian Larson♦Adrian Larson
109k19116248
109k19116248
Thanks, Adding a context variable did it!
– Thomas Pete
2 days ago
add a comment |
Thanks, Adding a context variable did it!
– Thomas Pete
2 days ago
Thanks, Adding a context variable did it!
– Thomas Pete
2 days ago
Thanks, Adding a context variable did it!
– Thomas Pete
2 days ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f254321%2fnested-dynamic-soql-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown