Getting an error in a SOQL
I am using below query in a batch class
Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :date.today().addDays(-1) ';
and get an error in logs unexpected token: (
but the same works good in the anonymous window.
Can anyone please suggest?
soql
add a comment |
I am using below query in a batch class
Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :date.today().addDays(-1) ';
and get an error in logs unexpected token: (
but the same works good in the anonymous window.
Can anyone please suggest?
soql
add a comment |
I am using below query in a batch class
Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :date.today().addDays(-1) ';
and get an error in logs unexpected token: (
but the same works good in the anonymous window.
Can anyone please suggest?
soql
I am using below query in a batch class
Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :date.today().addDays(-1) ';
and get an error in logs unexpected token: (
but the same works good in the anonymous window.
Can anyone please suggest?
soql
soql
asked yesterday
gs650xgs650x
12518
12518
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In dynamic SOQL (where the SOQL is a string) dotted expressions are not supported for bind variables (that follow the :
).
Best approach is to use static SOQL instead and that way you also get more syntax checking done for you. So:
Contact contacts = [
SELECT Id
FROM Contact
WHERE SyncToMarketo__c = true
AND LastModifiedDate >= :Date.today().addDays(-1)
];
If you have a reason to use dynamic SOQL, then this should work providing the query is executed where d
is still in scope:
Date d = Date.today().addDays(-1)
String q = ''
+ ' SELECT Id'
+ ' FROM Contact'
+ ' WHERE SyncToMarketo__c = true'
+ ' AND LastModifiedDate >= :d';
...
Contact contacts = (Contact) Database.query(q);
Thank you so much for responding but is there any other way to write the same in a dynamic query?
– gs650x
yesterday
@gs650x See last bit of code I just added.
– Keith C
yesterday
add a comment |
Posted query will not work in anonymous block as well. You could try something like
Date dt = date.today().addDays(-1);
string Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :dt';
Database.query(Query);
For efficiency you can try with YESTERDAY
string Query = 'SELECT Id FROM Contact WHERE SyncToMarketo__c = true AND LastModifiedDate >= YESTERDAY';
More on Date Literals here.
1
Yeah Date Literal makes way more sense for this use case.
– Adrian Larson♦
yesterday
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%2f245657%2fgetting-an-error-in-a-soql%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
In dynamic SOQL (where the SOQL is a string) dotted expressions are not supported for bind variables (that follow the :
).
Best approach is to use static SOQL instead and that way you also get more syntax checking done for you. So:
Contact contacts = [
SELECT Id
FROM Contact
WHERE SyncToMarketo__c = true
AND LastModifiedDate >= :Date.today().addDays(-1)
];
If you have a reason to use dynamic SOQL, then this should work providing the query is executed where d
is still in scope:
Date d = Date.today().addDays(-1)
String q = ''
+ ' SELECT Id'
+ ' FROM Contact'
+ ' WHERE SyncToMarketo__c = true'
+ ' AND LastModifiedDate >= :d';
...
Contact contacts = (Contact) Database.query(q);
Thank you so much for responding but is there any other way to write the same in a dynamic query?
– gs650x
yesterday
@gs650x See last bit of code I just added.
– Keith C
yesterday
add a comment |
In dynamic SOQL (where the SOQL is a string) dotted expressions are not supported for bind variables (that follow the :
).
Best approach is to use static SOQL instead and that way you also get more syntax checking done for you. So:
Contact contacts = [
SELECT Id
FROM Contact
WHERE SyncToMarketo__c = true
AND LastModifiedDate >= :Date.today().addDays(-1)
];
If you have a reason to use dynamic SOQL, then this should work providing the query is executed where d
is still in scope:
Date d = Date.today().addDays(-1)
String q = ''
+ ' SELECT Id'
+ ' FROM Contact'
+ ' WHERE SyncToMarketo__c = true'
+ ' AND LastModifiedDate >= :d';
...
Contact contacts = (Contact) Database.query(q);
Thank you so much for responding but is there any other way to write the same in a dynamic query?
– gs650x
yesterday
@gs650x See last bit of code I just added.
– Keith C
yesterday
add a comment |
In dynamic SOQL (where the SOQL is a string) dotted expressions are not supported for bind variables (that follow the :
).
Best approach is to use static SOQL instead and that way you also get more syntax checking done for you. So:
Contact contacts = [
SELECT Id
FROM Contact
WHERE SyncToMarketo__c = true
AND LastModifiedDate >= :Date.today().addDays(-1)
];
If you have a reason to use dynamic SOQL, then this should work providing the query is executed where d
is still in scope:
Date d = Date.today().addDays(-1)
String q = ''
+ ' SELECT Id'
+ ' FROM Contact'
+ ' WHERE SyncToMarketo__c = true'
+ ' AND LastModifiedDate >= :d';
...
Contact contacts = (Contact) Database.query(q);
In dynamic SOQL (where the SOQL is a string) dotted expressions are not supported for bind variables (that follow the :
).
Best approach is to use static SOQL instead and that way you also get more syntax checking done for you. So:
Contact contacts = [
SELECT Id
FROM Contact
WHERE SyncToMarketo__c = true
AND LastModifiedDate >= :Date.today().addDays(-1)
];
If you have a reason to use dynamic SOQL, then this should work providing the query is executed where d
is still in scope:
Date d = Date.today().addDays(-1)
String q = ''
+ ' SELECT Id'
+ ' FROM Contact'
+ ' WHERE SyncToMarketo__c = true'
+ ' AND LastModifiedDate >= :d';
...
Contact contacts = (Contact) Database.query(q);
edited yesterday
answered yesterday
Keith CKeith C
94.6k1089204
94.6k1089204
Thank you so much for responding but is there any other way to write the same in a dynamic query?
– gs650x
yesterday
@gs650x See last bit of code I just added.
– Keith C
yesterday
add a comment |
Thank you so much for responding but is there any other way to write the same in a dynamic query?
– gs650x
yesterday
@gs650x See last bit of code I just added.
– Keith C
yesterday
Thank you so much for responding but is there any other way to write the same in a dynamic query?
– gs650x
yesterday
Thank you so much for responding but is there any other way to write the same in a dynamic query?
– gs650x
yesterday
@gs650x See last bit of code I just added.
– Keith C
yesterday
@gs650x See last bit of code I just added.
– Keith C
yesterday
add a comment |
Posted query will not work in anonymous block as well. You could try something like
Date dt = date.today().addDays(-1);
string Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :dt';
Database.query(Query);
For efficiency you can try with YESTERDAY
string Query = 'SELECT Id FROM Contact WHERE SyncToMarketo__c = true AND LastModifiedDate >= YESTERDAY';
More on Date Literals here.
1
Yeah Date Literal makes way more sense for this use case.
– Adrian Larson♦
yesterday
add a comment |
Posted query will not work in anonymous block as well. You could try something like
Date dt = date.today().addDays(-1);
string Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :dt';
Database.query(Query);
For efficiency you can try with YESTERDAY
string Query = 'SELECT Id FROM Contact WHERE SyncToMarketo__c = true AND LastModifiedDate >= YESTERDAY';
More on Date Literals here.
1
Yeah Date Literal makes way more sense for this use case.
– Adrian Larson♦
yesterday
add a comment |
Posted query will not work in anonymous block as well. You could try something like
Date dt = date.today().addDays(-1);
string Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :dt';
Database.query(Query);
For efficiency you can try with YESTERDAY
string Query = 'SELECT Id FROM Contact WHERE SyncToMarketo__c = true AND LastModifiedDate >= YESTERDAY';
More on Date Literals here.
Posted query will not work in anonymous block as well. You could try something like
Date dt = date.today().addDays(-1);
string Query='Select Id FROM Contact WHERE SyncToMarketo__c = true '
+ ' AND LastModifiedDate >= :dt';
Database.query(Query);
For efficiency you can try with YESTERDAY
string Query = 'SELECT Id FROM Contact WHERE SyncToMarketo__c = true AND LastModifiedDate >= YESTERDAY';
More on Date Literals here.
answered yesterday
ReshmaReshma
2,98331222
2,98331222
1
Yeah Date Literal makes way more sense for this use case.
– Adrian Larson♦
yesterday
add a comment |
1
Yeah Date Literal makes way more sense for this use case.
– Adrian Larson♦
yesterday
1
1
Yeah Date Literal makes way more sense for this use case.
– Adrian Larson♦
yesterday
Yeah Date Literal makes way more sense for this use case.
– Adrian Larson♦
yesterday
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f245657%2fgetting-an-error-in-a-soql%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