function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rushrush 

stop progressive triggers during a scheduled batch process

 

Is there any way to make sure that a batch process does not actviate any further triggers from occuring after it as a result of its run?

 

I have the below code in place and want to run it overnight, however it is producing far too many SOQL queries and limits are causing it to error.  The basic goal here is to summarize data from at least two other objects (leads and opps so far) and place the totals on the Contact record.

 

global class batchonContact_updatekpi implements Database.Batchable<Contact>{
	List<Contact> ConsToUpdate = [SELECT Id, KPI_Dig_Audit_Closed__c, KPI_Dig_Audit_Converts__c, KPI_Dig_Audit_Requests__c, KPI_Dig_Audit_Won_Value__c FROM Contact Where RecordTypeId = '012F0000000y9gDIAQ'];
	global Iterable<Contact> start(database.batchablecontext Conkpi){
		return (ConsToUpdate);
	}
	global void execute(Database.BatchableContext Conkpi, List<Contact> scope){

		Set<String> AfflRepList = new Set<String>();
		String ConIdToAdd;
		List<Contact> masterAFFLCON = [SELECT Id, KPI_Dig_Audit_Closed__c, KPI_Dig_Audit_Converts__c, KPI_Dig_Audit_Requests__c, KPI_Dig_Audit_Won_Value__c FROM Contact Where RecordTypeId = '012F0000000y9gDIAQ' AND AccountId = '001F000000gfrcZ'];
		for(Contact tempCon1 : masterAFFLCON){
			ConIdToAdd = tempCon1.Id;
			AfflRepList.add(ConIdToAdd);
		}
		List<Lead> sumLeads = [SELECT Id, RecordTypeId, SalesRep__c, Status, IsConverted FROM Lead Where SalesRep__c IN :AfflRepList];	
		List<Opportunity> sumOpps = [SELECT Id, RecordTypeId, Sales_Rep__c, StageName, IsWon, Amount FROM Opportunity Where Sales_Rep__c IN :AfflRepList];
		for(Contact tempCon2 : masterAFFLCON){
			tempCon2.KPI_Dig_Audit_Requests__c = 0;
			tempCon2.KPI_Dig_Audit_Converts__c = 0;
			tempCon2.KPI_Dig_Audit_Closed__c = 0;
			tempCon2.KPI_Dig_Audit_Won_Value__c = 0;
			for(Lead tempLead1 : sumLeads){
				if(tempLead1.SalesRep__c == tempCon2.Id){
					tempCon2.KPI_Dig_Audit_Requests__c = tempCon2.KPI_Dig_Audit_Requests__c + 1;
					if(tempLead1.IsConverted == True){
						tempCon2.KPI_Dig_Audit_Converts__c = tempCon2.KPI_Dig_Audit_Converts__c + 1;
					}
				}
			}
			for(Opportunity tempOpp1 : sumOpps){
				if(tempOpp1.Sales_Rep__c == tempCon2.Id && tempOpp1.IsWon == True){
					tempCon2.KPI_Dig_Audit_Closed__c = tempCon2.KPI_Dig_Audit_Closed__c + 1;
					tempCon2.KPI_Dig_Audit_Won_Value__c = tempCon2.KPI_Dig_Audit_Won_Value__c + tempOpp1.Amount;
				}
			}
		}
		update masterAFFLCON;
	}
	global void finish(Database.BatchableContext info){
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
rushrush

 

Nevermind the problem.....had a moment to look again and realized I was cycling through the contacts twice.

 

However, if there is any advice on how to do this better, by all means please post.   Especially with regards to writing the test code for this.  Writing the tests for scheduled batch is a little bit elusive so far.

 

thanks,

 

 

All Answers

rushrush

 

another note, it appears that the process is triggering a third party marketing tool's process, which may be causing the issue.  I am hoping to be able to stop it on my own without having to work with their developers on the issue.

rushrush

 

Nevermind the problem.....had a moment to look again and realized I was cycling through the contacts twice.

 

However, if there is any advice on how to do this better, by all means please post.   Especially with regards to writing the test code for this.  Writing the tests for scheduled batch is a little bit elusive so far.

 

thanks,

 

 

This was selected as the best answer
Anup JadhavAnup Jadhav

Good stuff Rush! Thanks for letting the community know!

 

Anup