You need to sign in to do that
Don't have an account?
sumit d
invalid query locator in apex job
hI all,
I have a batch in which i am getting invalid query locator error for few records.
Can anyone help me with this error?
My batch is given below:-
public without sharing class BatchServiceScore implements Database.Batchable<sObject>,Database.Stateful{
public static Date backDate = System.Today().addDays(-365);
public static void run( Set<Id> AccIds ) {
List<Account> accountRecords = [ SELECT Id, Service_Score__c,NPS_Score__c,Customer_Satisfaction_Score__c,
Customer_Effort_Score__c,
( SELECT Id, Is_Negative_Case__c FROM Cases
WHERE CreatedDate >=: System.today().addDays(-365)
)
FROM Account WHERE ID IN : AccIds
];
executeHelper( accountRecords );
}
public Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator([ SELECT Id, Service_Score__c,NPS_Score__c,Customer_Satisfaction_Score__c,
Customer_Effort_Score__c,
( SELECT Id, Is_Negative_Case__c FROM Cases
WHERE CreatedDate >=: System.today().addDays(-365)
)
FROM Account
]);
}
public void execute(Database.BatchableContext BC, List<Account> scope){
executeHelper( scope );
}
public static void executeHelper( List<Account> scope ) {
List<Account> accsToUpdate = new List<Account>();
List<Case> casesToUpdate = new List<Case>();
for( Account acct : scope ){
Decimal totalNegativeCases = 0, totalPostiveCases = 0, service_Score = 0;
//for( Case c : acct.Cases){
casesToUpdate.addAll(acct.Cases);
if (casesToUpdate != null) {
for (Case c : casesToUpdate){
if( c.Is_Negative_Case__c ){
totalNegativeCases++;
}
else{
totalPostiveCases++;
}
}
}
System.debug('Case Size: '+ acct.Cases.Size()+' '+acct.Cases);
if( acct.Cases != null && acct.Cases.Size() > 0 ){
Decimal totalCases = acct.Cases.Size();
if( totalPostiveCases > 0 ) {
service_Score = (totalPostiveCases/totalCases)*75;
}
else if( totalNegativeCases > 0 ) {
service_Score = -(totalNegativeCases/totalCases)*75;
}
}
System.debug('service_Score after case review : '+service_Score);
if( acct.Service_Score__c != null
|| acct.Service_Score__c != service_Score ){
Decimal npsScoreContribution = 0;
if(acct.NPS_Score__c != null){
npsScoreContribution = acct.NPS_Score__c * 1.5;
System.debug('npsScoreContribution after case review : '+npsScoreContribution);
}
Decimal CustomerEffectScore = 0;
if(acct.Customer_Effort_Score__c == 1){
CustomerEffectScore = 5;
}else if(acct.Customer_Effort_Score__c == 2){
CustomerEffectScore = 4;
}else if(acct.Customer_Effort_Score__c == 3){
CustomerEffectScore = 3;
}else if(acct.Customer_Effort_Score__c == 4){
CustomerEffectScore = 2;
}else if(acct.Customer_Effort_Score__c == 5){
CustomerEffectScore = 1;
}
System.debug('CustomerEffectScore after case review : '+CustomerEffectScore);
Decimal CustomerSatisfaction = 0;
if(acct.Customer_Satisfaction_Score__c != null){
CustomerSatisfaction = acct.Customer_Satisfaction_Score__c/2.0;
System.debug('CustomerSatScore after case review : '+CustomerSatisfaction);
}
acct.Service_Score__c = service_Score + npsScoreContribution + CustomerSatisfaction + CustomerEffectScore;
accsToUpdate.add(acct);
}
}
if( accsToUpdate.size() > 0 ){
update accsToUpdate;
}
}
public void finish(Database.BatchableContext BC){
if( !Test.isRunningTest() ){
BatchOnAccountForMarketingActivityScore bch = new BatchOnAccountForMarketingActivityScore();
Database.executeBatch( bch, 1 );
}
}
}
Can anyone help me out with this error?
I have a batch in which i am getting invalid query locator error for few records.
Can anyone help me with this error?
My batch is given below:-
public without sharing class BatchServiceScore implements Database.Batchable<sObject>,Database.Stateful{
public static Date backDate = System.Today().addDays(-365);
public static void run( Set<Id> AccIds ) {
List<Account> accountRecords = [ SELECT Id, Service_Score__c,NPS_Score__c,Customer_Satisfaction_Score__c,
Customer_Effort_Score__c,
( SELECT Id, Is_Negative_Case__c FROM Cases
WHERE CreatedDate >=: System.today().addDays(-365)
)
FROM Account WHERE ID IN : AccIds
];
executeHelper( accountRecords );
}
public Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator([ SELECT Id, Service_Score__c,NPS_Score__c,Customer_Satisfaction_Score__c,
Customer_Effort_Score__c,
( SELECT Id, Is_Negative_Case__c FROM Cases
WHERE CreatedDate >=: System.today().addDays(-365)
)
FROM Account
]);
}
public void execute(Database.BatchableContext BC, List<Account> scope){
executeHelper( scope );
}
public static void executeHelper( List<Account> scope ) {
List<Account> accsToUpdate = new List<Account>();
List<Case> casesToUpdate = new List<Case>();
for( Account acct : scope ){
Decimal totalNegativeCases = 0, totalPostiveCases = 0, service_Score = 0;
//for( Case c : acct.Cases){
casesToUpdate.addAll(acct.Cases);
if (casesToUpdate != null) {
for (Case c : casesToUpdate){
if( c.Is_Negative_Case__c ){
totalNegativeCases++;
}
else{
totalPostiveCases++;
}
}
}
System.debug('Case Size: '+ acct.Cases.Size()+' '+acct.Cases);
if( acct.Cases != null && acct.Cases.Size() > 0 ){
Decimal totalCases = acct.Cases.Size();
if( totalPostiveCases > 0 ) {
service_Score = (totalPostiveCases/totalCases)*75;
}
else if( totalNegativeCases > 0 ) {
service_Score = -(totalNegativeCases/totalCases)*75;
}
}
System.debug('service_Score after case review : '+service_Score);
if( acct.Service_Score__c != null
|| acct.Service_Score__c != service_Score ){
Decimal npsScoreContribution = 0;
if(acct.NPS_Score__c != null){
npsScoreContribution = acct.NPS_Score__c * 1.5;
System.debug('npsScoreContribution after case review : '+npsScoreContribution);
}
Decimal CustomerEffectScore = 0;
if(acct.Customer_Effort_Score__c == 1){
CustomerEffectScore = 5;
}else if(acct.Customer_Effort_Score__c == 2){
CustomerEffectScore = 4;
}else if(acct.Customer_Effort_Score__c == 3){
CustomerEffectScore = 3;
}else if(acct.Customer_Effort_Score__c == 4){
CustomerEffectScore = 2;
}else if(acct.Customer_Effort_Score__c == 5){
CustomerEffectScore = 1;
}
System.debug('CustomerEffectScore after case review : '+CustomerEffectScore);
Decimal CustomerSatisfaction = 0;
if(acct.Customer_Satisfaction_Score__c != null){
CustomerSatisfaction = acct.Customer_Satisfaction_Score__c/2.0;
System.debug('CustomerSatScore after case review : '+CustomerSatisfaction);
}
acct.Service_Score__c = service_Score + npsScoreContribution + CustomerSatisfaction + CustomerEffectScore;
accsToUpdate.add(acct);
}
}
if( accsToUpdate.size() > 0 ){
update accsToUpdate;
}
}
public void finish(Database.BatchableContext BC){
if( !Test.isRunningTest() ){
BatchOnAccountForMarketingActivityScore bch = new BatchOnAccountForMarketingActivityScore();
Database.executeBatch( bch, 1 );
}
}
}
Can anyone help me out with this error?
1) if you are trying to query last year cases only i will suggent use Date Literals
SELECT Id, Is_Negative_Case__c FROM Cases WHERE CreatedDate >= LAST_YEAR
Or in case you are trying to find 365 dyas old casesCreatedDate = LAST_N_DAYS:365
you can refer other date literals
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
2) if you want to chack that list is empty or not use isempty do not use (!= null and .size())
if( acct.Cases != null && acct.Cases.Size() > 0 ){ // replace with if(!(acct.Cases.isempty()))
3) Are you running this batch with batch size as 1, otherwise it look like logically it might calcualte wrong value