Schedule Batch Apex too many rowsToo many query rows: 50001Initial term of field expression must be a...

Leveraging cash for buying car

How do I gain the trust of other PCs?

...and then she held the gun

Having some issue with notation in a Hilbert space

Why does my system use more RAM after an hour of usage?

Using roof rails to set up hammock

What is the color associated with lukewarm?

Co-worker is now managing my team. Does this mean that I'm being demoted?

what is "dot" sign in the •NO?

Testing thermite for chemical properties

Why is gun control associated with the socially liberal Democratic party?

Lead the way to this Literary Knight to its final “DESTINATION”

Redirecting output only on a successful command call

How to make all magic-casting innate, but still rare?

Converting 3x7 to a 1x7. Is it possible with only existing parts?

Is there any effect in D&D 5e that cannot be undone?

How can a flywheel makes engine runs smoothly?

Manager wants to hire me; HR does not. How to proceed?

TiKZ won't graph 1/sqrt(x)

Why are almost all the people in this orchestra recording wearing headphones with one ear on and one ear off?

Is this set open or closed (or both?)

Time at 1G acceleration to travel 100000 light years

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

Print the phrase "And she said, 'But that's his.'" using only the alphabet



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 9 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
            7 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
            7 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
            7 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
            7 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
            7 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
            7 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 9 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
            7 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
            7 hours ago














          • 1





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

            – adam
            7 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
            7 hours ago








          1




          1





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

          – adam
          7 hours ago





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

          – adam
          7 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
          7 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
          7 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

          Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

          The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

          Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...