Getting an error in a SOQL












1














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?










share|improve this question



























    1














    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?










    share|improve this question

























      1












      1








      1







      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      gs650xgs650x

      12518




      12518






















          2 Answers
          2






          active

          oldest

          votes


















          5














          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);





          share|improve this answer























          • 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



















          2














          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.






          share|improve this answer

















          • 1




            Yeah Date Literal makes way more sense for this use case.
            – Adrian Larson
            yesterday











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          5














          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);





          share|improve this answer























          • 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
















          5














          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);





          share|improve this answer























          • 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














          5












          5








          5






          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);





          share|improve this answer














          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);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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













          2














          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.






          share|improve this answer

















          • 1




            Yeah Date Literal makes way more sense for this use case.
            – Adrian Larson
            yesterday
















          2














          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.






          share|improve this answer

















          • 1




            Yeah Date Literal makes way more sense for this use case.
            – Adrian Larson
            yesterday














          2












          2








          2






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          ReshmaReshma

          2,98331222




          2,98331222








          • 1




            Yeah Date Literal makes way more sense for this use case.
            – Adrian Larson
            yesterday














          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?