Schedule Batch Apex too many rowsToo many query rows: 50001Initial term of field expression must be a concrete SObject: LIST<Call2_vod__c>APEX CPU Time limit exceeded error. Any suggestions?Trying to implement a batchable wrapperHelp writing a simple APEX Trigger TestToo many DML rows: 10001(bulk update)batch class not running due to too many soql queriesturn an APEX trigger into scheduled batch updateHow to convert Datetime datatype to Date format only?Batch Apex System.LimitException: Too many query locator rows: 50000001

How to make a villain when your PCs are villains?

How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?

Fill the maze with a wall-following Snake until it gets stuck

...and then she held the gun

What kind of chart is this?

How Linux command "mount -a" works

I have found ports on my Samsung smart tv running a display service. What can I do with it?

How can the US president give an order to a civilian?

How do I run a script as sudo at boot time on Ubuntu 18.04 Server?

How to avoid offending original culture when making conculture inspired from original

What is this plant I saw for sale at a Romanian farmer's market?

Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?

Will users know a CardView is clickable?

Leveraging cash for buying car

How did the European Union reach the figure of 3% as a maximum allowed deficit?

Using roof rails to set up hammock

Right indicator flash-frequency has increased and rear-right bulb is out

Is this set open or closed (or both?)

How to search for Android apps without ads?

Can you cover a cube with copies of this shape?

How did space travel spread through the galaxy?

How to ask if I can mow my neighbor's lawn

Is it a bad idea to have a pen name with only an initial for a surname?

Is my research statement supposed to lead to papers in top journals?



Schedule Batch Apex too many rows


Too many query rows: 50001Initial term of field expression must be a concrete SObject: LIST<Call2_vod__c>APEX CPU Time limit exceeded error. Any suggestions?Trying to implement a batchable wrapperHelp writing a simple APEX Trigger TestToo many DML rows: 10001(bulk update)batch class not running due to too many soql queriesturn an APEX trigger into scheduled batch updateHow to convert Datetime datatype to Date format only?Batch Apex System.LimitException: Too many query locator rows: 50000001






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am having an issue because I believe I don't understand batch apex/scheduling very well. Every time I use this I get either a SOQL error of trying to query too many rows of 50001, or a DML error trying to add too many things in the list to update.
Any help would be greatly appreciated



The code



global class LastMktoSyncDate implements Database.Batchable<sObject>


global List<sObject> start(Database.BatchableContext c)

date d = system.today().addDays(-7);
List<sObject> scope = new List<sObject>();

scope.addAll([SELECT id, Name, Sync_to_mkto__c, account_name__c from Contact where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);

scope.addAll([SELECT id, Name, Sync_to_mkto__c from Lead where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);

return scope;


global void execute(Database.BatchableContext c, List<sObject> scope)

List<Contact> con_toupdate = new List<Contact>();

List<Lead> lead_toupdate = new List<Lead>();

for(sObject obj : scope)

switch on obj

when Contact con

con.Last_Marketo_Sync_Date__c = system.today();
con_toupdate.add(con);

when Lead lea

lea.Last_Marketo_Sync_Date__c = system.today();
lead_toupdate.add(lea);




update con_toupdate;
update lead_toupdate;


global void finish(Database.BatchableContext c)





The Scheduler



global class ScheduledMktoSync implements Schedulable
global void execute(SchedulableContext sc)
LastMktoSyncDate l = new LastMktoSyncDate();
database.executeBatch(l);











share|improve this question




























    1















    I am having an issue because I believe I don't understand batch apex/scheduling very well. Every time I use this I get either a SOQL error of trying to query too many rows of 50001, or a DML error trying to add too many things in the list to update.
    Any help would be greatly appreciated



    The code



    global class LastMktoSyncDate implements Database.Batchable<sObject>


    global List<sObject> start(Database.BatchableContext c)

    date d = system.today().addDays(-7);
    List<sObject> scope = new List<sObject>();

    scope.addAll([SELECT id, Name, Sync_to_mkto__c, account_name__c from Contact where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);

    scope.addAll([SELECT id, Name, Sync_to_mkto__c from Lead where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);

    return scope;


    global void execute(Database.BatchableContext c, List<sObject> scope)

    List<Contact> con_toupdate = new List<Contact>();

    List<Lead> lead_toupdate = new List<Lead>();

    for(sObject obj : scope)

    switch on obj

    when Contact con

    con.Last_Marketo_Sync_Date__c = system.today();
    con_toupdate.add(con);

    when Lead lea

    lea.Last_Marketo_Sync_Date__c = system.today();
    lead_toupdate.add(lea);




    update con_toupdate;
    update lead_toupdate;


    global void finish(Database.BatchableContext c)





    The Scheduler



    global class ScheduledMktoSync implements Schedulable
    global void execute(SchedulableContext sc)
    LastMktoSyncDate l = new LastMktoSyncDate();
    database.executeBatch(l);











    share|improve this question
























      1












      1








      1








      I am having an issue because I believe I don't understand batch apex/scheduling very well. Every time I use this I get either a SOQL error of trying to query too many rows of 50001, or a DML error trying to add too many things in the list to update.
      Any help would be greatly appreciated



      The code



      global class LastMktoSyncDate implements Database.Batchable<sObject>


      global List<sObject> start(Database.BatchableContext c)

      date d = system.today().addDays(-7);
      List<sObject> scope = new List<sObject>();

      scope.addAll([SELECT id, Name, Sync_to_mkto__c, account_name__c from Contact where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);

      scope.addAll([SELECT id, Name, Sync_to_mkto__c from Lead where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);

      return scope;


      global void execute(Database.BatchableContext c, List<sObject> scope)

      List<Contact> con_toupdate = new List<Contact>();

      List<Lead> lead_toupdate = new List<Lead>();

      for(sObject obj : scope)

      switch on obj

      when Contact con

      con.Last_Marketo_Sync_Date__c = system.today();
      con_toupdate.add(con);

      when Lead lea

      lea.Last_Marketo_Sync_Date__c = system.today();
      lead_toupdate.add(lea);




      update con_toupdate;
      update lead_toupdate;


      global void finish(Database.BatchableContext c)





      The Scheduler



      global class ScheduledMktoSync implements Schedulable
      global void execute(SchedulableContext sc)
      LastMktoSyncDate l = new LastMktoSyncDate();
      database.executeBatch(l);











      share|improve this question














      I am having an issue because I believe I don't understand batch apex/scheduling very well. Every time I use this I get either a SOQL error of trying to query too many rows of 50001, or a DML error trying to add too many things in the list to update.
      Any help would be greatly appreciated



      The code



      global class LastMktoSyncDate implements Database.Batchable<sObject>


      global List<sObject> start(Database.BatchableContext c)

      date d = system.today().addDays(-7);
      List<sObject> scope = new List<sObject>();

      scope.addAll([SELECT id, Name, Sync_to_mkto__c, account_name__c from Contact where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);

      scope.addAll([SELECT id, Name, Sync_to_mkto__c from Lead where ((Last_Marketo_Sync_Date__c = NULL OR Last_Marketo_Sync_Date__c < :d) AND Sync_To_Mkto__c != True)]);

      return scope;


      global void execute(Database.BatchableContext c, List<sObject> scope)

      List<Contact> con_toupdate = new List<Contact>();

      List<Lead> lead_toupdate = new List<Lead>();

      for(sObject obj : scope)

      switch on obj

      when Contact con

      con.Last_Marketo_Sync_Date__c = system.today();
      con_toupdate.add(con);

      when Lead lea

      lea.Last_Marketo_Sync_Date__c = system.today();
      lead_toupdate.add(lea);




      update con_toupdate;
      update lead_toupdate;


      global void finish(Database.BatchableContext c)





      The Scheduler



      global class ScheduledMktoSync implements Schedulable
      global void execute(SchedulableContext sc)
      LastMktoSyncDate l = new LastMktoSyncDate();
      database.executeBatch(l);








      apex batch scheduled-apex schedulebatch






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      adamadam

      205




      205




















          1 Answer
          1






          active

          oldest

          votes


















          5














          When you do addAll you're still subject to the Apex Governor limits for a single transaction. That is, you're getting too many records and the list cannot iterate.



          If you want to iterate on more records, you probably want to use a QueryLocator instead. Since you have two different queries, in your case that probably means that you're going to need two batch jobs (maybe you can chain them?)



          An example of using a queryLocator follows:



          public Database.QueryLocator start(Database.BatchableContext BC) 
          String query = 'SELECT Id FROM MyObject__c';

          return Database.getQueryLocator(query);



          The rest of your code (execute and finish) remains unchanged other than making sure you're only processing records that match the querylocator you sent (contacts OR leads)



          UPDATE:



          A fun way of doing this with minimal repetition is to have a constructor in your class and then returning two different locators depending on a parameter.



          For example:



          String myQuery;

          global LastMktoSyncDate(String objectName)
          switch on objectName
          when 'Contact'
          myQuery = 'your query for contacts';

          when 'Lead'
          myQuery = 'your query for lead';




          public Database.QueryLocator start(Database.BatchableContext BC)

          return Database.getQueryLocator(myQuery);






          share|improve this answer


















          • 1





            Thank you. This worked. I was trying to be cheeky and not write a different class

            – adam
            6 hours ago











          • Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on the finish method

            – Sebastian Kessel
            5 hours ago











          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%2f266027%2fschedule-batch-apex-too-many-rows%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          When you do addAll you're still subject to the Apex Governor limits for a single transaction. That is, you're getting too many records and the list cannot iterate.



          If you want to iterate on more records, you probably want to use a QueryLocator instead. Since you have two different queries, in your case that probably means that you're going to need two batch jobs (maybe you can chain them?)



          An example of using a queryLocator follows:



          public Database.QueryLocator start(Database.BatchableContext BC) 
          String query = 'SELECT Id FROM MyObject__c';

          return Database.getQueryLocator(query);



          The rest of your code (execute and finish) remains unchanged other than making sure you're only processing records that match the querylocator you sent (contacts OR leads)



          UPDATE:



          A fun way of doing this with minimal repetition is to have a constructor in your class and then returning two different locators depending on a parameter.



          For example:



          String myQuery;

          global LastMktoSyncDate(String objectName)
          switch on objectName
          when 'Contact'
          myQuery = 'your query for contacts';

          when 'Lead'
          myQuery = 'your query for lead';




          public Database.QueryLocator start(Database.BatchableContext BC)

          return Database.getQueryLocator(myQuery);






          share|improve this answer


















          • 1





            Thank you. This worked. I was trying to be cheeky and not write a different class

            – adam
            6 hours ago











          • Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on the finish method

            – Sebastian Kessel
            5 hours ago















          5














          When you do addAll you're still subject to the Apex Governor limits for a single transaction. That is, you're getting too many records and the list cannot iterate.



          If you want to iterate on more records, you probably want to use a QueryLocator instead. Since you have two different queries, in your case that probably means that you're going to need two batch jobs (maybe you can chain them?)



          An example of using a queryLocator follows:



          public Database.QueryLocator start(Database.BatchableContext BC) 
          String query = 'SELECT Id FROM MyObject__c';

          return Database.getQueryLocator(query);



          The rest of your code (execute and finish) remains unchanged other than making sure you're only processing records that match the querylocator you sent (contacts OR leads)



          UPDATE:



          A fun way of doing this with minimal repetition is to have a constructor in your class and then returning two different locators depending on a parameter.



          For example:



          String myQuery;

          global LastMktoSyncDate(String objectName)
          switch on objectName
          when 'Contact'
          myQuery = 'your query for contacts';

          when 'Lead'
          myQuery = 'your query for lead';




          public Database.QueryLocator start(Database.BatchableContext BC)

          return Database.getQueryLocator(myQuery);






          share|improve this answer


















          • 1





            Thank you. This worked. I was trying to be cheeky and not write a different class

            – adam
            6 hours ago











          • Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on the finish method

            – Sebastian Kessel
            5 hours ago













          5












          5








          5







          When you do addAll you're still subject to the Apex Governor limits for a single transaction. That is, you're getting too many records and the list cannot iterate.



          If you want to iterate on more records, you probably want to use a QueryLocator instead. Since you have two different queries, in your case that probably means that you're going to need two batch jobs (maybe you can chain them?)



          An example of using a queryLocator follows:



          public Database.QueryLocator start(Database.BatchableContext BC) 
          String query = 'SELECT Id FROM MyObject__c';

          return Database.getQueryLocator(query);



          The rest of your code (execute and finish) remains unchanged other than making sure you're only processing records that match the querylocator you sent (contacts OR leads)



          UPDATE:



          A fun way of doing this with minimal repetition is to have a constructor in your class and then returning two different locators depending on a parameter.



          For example:



          String myQuery;

          global LastMktoSyncDate(String objectName)
          switch on objectName
          when 'Contact'
          myQuery = 'your query for contacts';

          when 'Lead'
          myQuery = 'your query for lead';




          public Database.QueryLocator start(Database.BatchableContext BC)

          return Database.getQueryLocator(myQuery);






          share|improve this answer













          When you do addAll you're still subject to the Apex Governor limits for a single transaction. That is, you're getting too many records and the list cannot iterate.



          If you want to iterate on more records, you probably want to use a QueryLocator instead. Since you have two different queries, in your case that probably means that you're going to need two batch jobs (maybe you can chain them?)



          An example of using a queryLocator follows:



          public Database.QueryLocator start(Database.BatchableContext BC) 
          String query = 'SELECT Id FROM MyObject__c';

          return Database.getQueryLocator(query);



          The rest of your code (execute and finish) remains unchanged other than making sure you're only processing records that match the querylocator you sent (contacts OR leads)



          UPDATE:



          A fun way of doing this with minimal repetition is to have a constructor in your class and then returning two different locators depending on a parameter.



          For example:



          String myQuery;

          global LastMktoSyncDate(String objectName)
          switch on objectName
          when 'Contact'
          myQuery = 'your query for contacts';

          when 'Lead'
          myQuery = 'your query for lead';




          public Database.QueryLocator start(Database.BatchableContext BC)

          return Database.getQueryLocator(myQuery);







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 8 hours ago









          Sebastian KesselSebastian Kessel

          9,49262239




          9,49262239







          • 1





            Thank you. This worked. I was trying to be cheeky and not write a different class

            – adam
            6 hours ago











          • Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on the finish method

            – Sebastian Kessel
            5 hours ago












          • 1





            Thank you. This worked. I was trying to be cheeky and not write a different class

            – adam
            6 hours ago











          • Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on the finish method

            – Sebastian Kessel
            5 hours ago







          1




          1





          Thank you. This worked. I was trying to be cheeky and not write a different class

          – adam
          6 hours ago





          Thank you. This worked. I was trying to be cheeky and not write a different class

          – adam
          6 hours ago













          Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on the finish method

          – Sebastian Kessel
          5 hours ago





          Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on the finish method

          – Sebastian Kessel
          5 hours ago

















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f266027%2fschedule-batch-apex-too-many-rows%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

          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

          François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480