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 can I detect if I'm in a subshell?
Is the infant mortality rate among African-American babies in Youngstown, Ohio greater than that of babies in Iran?
New Site Design!
How to know whether to write accidentals as sharps or flats?
How can I maintain game balance while allowing my player to craft genuinely useful items?
How can I ping multiple IP addresses at the same time?
TiKZ won't graph 1/sqrt(x)
Is this set open or closed (or both?)
Converting 3x7 to a 1x7. Is it possible with only existing parts?
Huge Heap Table and table compression on SQL Server 2016
Fill the maze with a wall-following Snake until it gets stuck
Schedule Batch Apex too many rows
Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?
How could I create a situation in which a PC has to make a saving throw or be forced to pet a dog?
Co-worker is now managing my team. Does this mean that I'm being demoted?
At what temperature should the earth be cooked to prevent human infection?
Numerical second order differentiation
How to avoid offending original culture when making conculture inspired from original
what is "dot" sign in the •NO?
Why is gun control associated with the socially liberal Democratic party?
...and then she held the gun
Manager wants to hire me; HR does not. How to proceed?
Digital signature that is only verifiable by one specific person
Explicit direct #include vs. Non-contractual transitive #include
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;
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
add a comment |
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
add a comment |
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
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
apex batch scheduled-apex schedulebatch
asked 10 hours ago
adamadam
205
205
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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);
1
Thank you. This worked. I was trying to be cheeky and not write a different class
– adam
8 hours ago
Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on thefinishmethod
– Sebastian Kessel
8 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%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
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);
1
Thank you. This worked. I was trying to be cheeky and not write a different class
– adam
8 hours ago
Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on thefinishmethod
– Sebastian Kessel
8 hours ago
add a comment |
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);
1
Thank you. This worked. I was trying to be cheeky and not write a different class
– adam
8 hours ago
Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on thefinishmethod
– Sebastian Kessel
8 hours ago
add a comment |
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);
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);
answered 10 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
8 hours ago
Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on thefinishmethod
– Sebastian Kessel
8 hours ago
add a comment |
1
Thank you. This worked. I was trying to be cheeky and not write a different class
– adam
8 hours ago
Well, if you implement the suggestion you may not have to. :) Just call the same class with a different constructor on thefinishmethod
– Sebastian Kessel
8 hours ago
1
1
Thank you. This worked. I was trying to be cheeky and not write a different class
– adam
8 hours ago
Thank you. This worked. I was trying to be cheeky and not write a different class
– adam
8 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
8 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
8 hours ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f266027%2fschedule-batch-apex-too-many-rows%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown