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 updateBatch Apex System.LimitException: Too many query locator rows: 50000001

Is the "Watchmen" TV series a continuation of the movie or the comics?

How did composers "test" their music?

Does the on'yomi of 輪 (リン) have any relation to the English "ring", or is it a coincidence?

Fantasy story about a magic sword with limited "charges"

How to learn Unpublished checkpoints

How do I add numbers from two txt files and write it to the same file?

Why is the air inside airliners so dry (low humidity)?

Call local emergency number using a foreign mobile number

Dealing with strict rules for paper submission using LaTeX

Is there a Ukrainian transcript of Trump's controversial July 25 call to President Zelensky?

/etc/shadow permissions security best practice (000 vs. 600 vs. 640)

Does the original Game Boy game "Tetris" have a battery memory inside the cartridge?

What would an inclusive curriculum look like in a computer science course?

An infinite version of the Dilworth theorem

Fast symmetric key cryptography class

How does an all-female medieval country maintain itself?

What is the equivalent of "if you say so" in German?

Is there a simple way to typeset playing cards?

How does Deep Packet Inspection work with encrypted packets?

Graph with cropped letters

Would webs catch fire if Fire Bolt is used on a creature inside of them?

Washing the skin of a dead rat

Luggage storage in Garmisch-Partenkirchen

Slash the matrix



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 updateBatch 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 Jun 14 at 15:28









      adamadam

      205 bronze badges




      205 bronze badges























          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
            Jun 14 at 17:20











          • 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
            Jun 14 at 17:40












          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/4.0/"u003ecc by-sa 4.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
            Jun 14 at 17:20











          • 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
            Jun 14 at 17:40















          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
            Jun 14 at 17:20











          • 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
            Jun 14 at 17:40













          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 Jun 14 at 15:31









          Sebastian KesselSebastian Kessel

          10.3k6 gold badges23 silver badges39 bronze badges




          10.3k6 gold badges23 silver badges39 bronze badges










          • 1





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

            – adam
            Jun 14 at 17:20











          • 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
            Jun 14 at 17:40












          • 1





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

            – adam
            Jun 14 at 17:20











          • 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
            Jun 14 at 17:40







          1




          1





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

          – adam
          Jun 14 at 17:20





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

          – adam
          Jun 14 at 17:20













          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
          Jun 14 at 17:40





          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
          Jun 14 at 17:40


















          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

          Distance measures on a map of a game The 2019 Stack Overflow Developer Survey Results Are Inmin distance in a graphShortest distance path on contour plotHow to plot a tilted map?Finding points outside of a diskDelaunay link distanceAnnulus from GeoDisks: drawing a ring on a mapNegative Correlation DistanceFind distance along a path (GPS coordinates)Finding position at given distance in a GeoPathMathematics behind distance estimation using camera

          How to get a smooth, uniform ParametricPlot of a 2D Region?How to plot a complicated Region?How to exclude a region from ParametricPlotHow discretize a region placing vertices on a specific non-uniform gridHow to transform a Plot or a ParametricPlot into a RegionHow can I get a smooth plot of a bounded region?Smooth ParametricPlot3D with RegionFunction?Smooth border of a region ParametricPlotSmooth region boundarySmooth region plot from list of pointsGet minimum y of a certain x in a region

          Genealogie vun de Merowenger Vum Merowech bis zum Chilperich I. | Navigatiounsmenü