You need to sign in to do that
Don't have an account?
mita mukwevho
Apex batch class code
Hello, I'm trying to make an update on properties on more than a thousand, updating a single field should automatically update two other related fields. Please show me how do I do this batch Apex class?, because currently it only updates up to 200 records
see below my code
global class FieldValueClonerBatchUpdate implements Database.Batchable<sObject>{
public String queryStr;
public Map<String, String> fieldsMap;
global FieldValueClonerBatchUpdate (String qry, Map<String, String> clonedFieldsMap){
this.queryStr = qry;
this.fieldsMap = clonedFieldsMap;
}
global List<sObject> start(Database.BatchableContext BC) {
List<sObject> queryRecords = database.query(queryStr);
return queryRecords;
}
global void execute(Database.BatchableContext BC, List<sobject> scope) {
List<sObject> sObjsToUpdate = new List<sObject>();
if (scope.size() > 0) {
for (sObject sObj : scope) {
for (String fieldAPIName : fieldsMap.keySet()) {
sObj.put(fieldsMap.get(fieldAPIName), sObj.get(fieldAPIName));
}
sObjsToUpdate.add(sObj);
}
update sObjsToUpdate;
}
}
global void finish(Database.BatchableContext BC) {
System.debug('records finished process.');
}
}
see below my code
global class FieldValueClonerBatchUpdate implements Database.Batchable<sObject>{
public String queryStr;
public Map<String, String> fieldsMap;
global FieldValueClonerBatchUpdate (String qry, Map<String, String> clonedFieldsMap){
this.queryStr = qry;
this.fieldsMap = clonedFieldsMap;
}
global List<sObject> start(Database.BatchableContext BC) {
List<sObject> queryRecords = database.query(queryStr);
return queryRecords;
}
global void execute(Database.BatchableContext BC, List<sobject> scope) {
List<sObject> sObjsToUpdate = new List<sObject>();
if (scope.size() > 0) {
for (sObject sObj : scope) {
for (String fieldAPIName : fieldsMap.keySet()) {
sObj.put(fieldsMap.get(fieldAPIName), sObj.get(fieldAPIName));
}
sObjsToUpdate.add(sObj);
}
update sObjsToUpdate;
}
}
global void finish(Database.BatchableContext BC) {
System.debug('records finished process.');
}
}
You have not query locator in your batch class that`s why it runs for 1st batch of 200 records only.
Use this start method instead of the one you are using now. Hope this helps. Do let me know if you have any further queries.
P.S: Please Marks this as solved if this helped you.
Regards
Shubham Kumar
Still it give me the erro message say line 1.....
.Unexpected token 'Database.QueryLocator'.
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(queryStr);
}
public String queryStr;
public Map<String, String> fieldsMap;
global FieldValueClonerBatchUpdate (String qry, Map<String, String> clonedFieldsMap){
this.queryStr = qry;
this.fieldsMap = clonedFieldsMap;
}
global List<sObject> start(Database.BatchableContext BC) {
List<sObject> queryRecords = database.query(queryStr);
return queryRecords;
}
global void execute(Database.BatchableContext BC, List<sobject> scope) {
List<sObject> sObjsToUpdate = new List<sObject>();
if (scope.size() > 0) {
for (sObject sObj : scope) {
for (String fieldAPIName : fieldsMap.keySet()) {
sObj.put(fieldsMap.get(fieldAPIName), sObj.get(fieldAPIName));
}
sObjsToUpdate.add(sObj);
}
update sObjsToUpdate;
}
}
global void finish(Database.BatchableContext BC) {
System.debug('records finished process.');
}
}
You have 2 start method use this code. Hope this helps. Do let me know if you have any further queries.
P.S: Please Marks this as solved if this helped you.
Regards
Shubham Kumar
MY PUBLIC CLASS
--------
public class FieldValueCloner {
// Static variable and method to prevent the Maximum Trigger Depth Exceeded error caused by trigger recursion
public static Set<String> flagList = new Set<String>();
public static boolean runOnce(String triggerName) {
if (!flagList.contains(triggerName)) {
flagList.add(triggerName);
} else {
return false;
}
return true;
}
public static void cloneFieldValues(String sObjectAPIName, String filterFieldAPIName, List<String> filterValues) {
String queryString = 'SELECT Id';
Map<String, String> fieldsToCloneMap = new Map<String, String>();
if (sObjectAPIName != null && sObjectAPIName != '') {
List<Field_Value_Clone_Configurations__c> fieldValueCloneConfigs = [SELECT From_Field_API_Name__c, To_Field_API_Name__c FROM Field_Value_Clone_Configurations__c WHERE Object_API_Name__c=:sObjectAPIName];
if (!fieldValueCloneConfigs.isEmpty()) {
for(Field_Value_Clone_Configurations__c fieldValueCloneConfig : fieldValueCloneConfigs) {
queryString += ', ' + fieldValueCloneConfig.From_Field_API_Name__c + ', ' + fieldValueCloneConfig.To_Field_API_Name__c;
fieldsToCloneMap.put(fieldValueCloneConfig.From_Field_API_Name__c, fieldValueCloneConfig.To_Field_API_Name__c);
}
queryString += ' FROM ' + sObjectAPIName;
if (filterValues != null && !filterValues.isEmpty() && filterFieldAPIName != null && filterFieldAPIName != '') {
queryString += ' WHERE ';
Integer counter = 1;
/*for (String filterValue : filterValues) {
queryString += filterFieldAPIName + ' = \'' + filterValue + '\'';
if (counter != filterValues.size()) {
queryString += ' OR ';
}
}*/
queryString += filterFieldAPIName + ' in : filterValues';
}
try {
system.debug('FieldValueCloner queryString to run: ' + queryString);
List<sObject> sObjs = database.query(queryString);
if (!sObjs.isEmpty()) {
for (sObject sObj : sObjs) {
for (String fromFieldAPIName : fieldsToCloneMap.keySet()) {
sObj.put(fieldsToCloneMap.get(fromFieldAPIName), sObj.get(fromFieldAPIName));
}
}
update sObjs;
}
} catch (exception e) {
system.debug('Error occurred while updating records: ' + e.getMessage());
}
}
}
}
}
----------------------------------------------------------------------------
MY TRIGEER
trigger FormulaValueFieldUpdate_PropertyValue on Property_Value__c (after insert, after update) {
if (FieldValueCloner.runOnce('FormulaValueFieldUpdate_PropertyValue')) {
Set<String> propertyValueIDs = new Set<String>();
List<Field_Value_Clone_Configurations__c> fieldValueCloneConfigs = [SELECT From_Field_API_Name__c, To_Field_API_Name__c FROM Field_Value_Clone_Configurations__c WHERE Object_API_Name__c='Property_Value__c'];
for (Property_Value__c propertyValue : Trigger.New) {
for (Field_Value_Clone_Configurations__c fieldValueCloneConfig : fieldValueCloneConfigs) {
if (Trigger.isInsert || (Trigger.isUpdate && (Trigger.oldMap.get(propertyValue.Id).get(fieldValueCloneConfig.From_Field_API_Name__c) != Trigger.newMap.get(propertyValue.Id).get(fieldValueCloneConfig.From_Field_API_Name__c) || Trigger.oldMap.get(propertyValue.Id).get(fieldValueCloneConfig.To_Field_API_Name__c) != Trigger.newMap.get(propertyValue.Id).get(fieldValueCloneConfig.To_Field_API_Name__c)))) {
if (!propertyValueIDs.contains(propertyValue.Id)) {
propertyValueIDs.add(propertyValue.Id);
}
}
}
}
if (!propertyValueIDs.isEmpty()) {
FieldValueCloner.cloneFieldValues('Property_Value__c', 'Id', new List<String>(propertyValueIDs));
}
}
}
----------------------------------------------------------------
BATCH CLASS
global class FieldValueClonerBatchUpdate implements Database.Batchable<sObject>{
public String queryStr;
public Map<String, String> fieldsMap;
global FieldValueClonerBatchUpdate (String qry, Map<String, String> clonedFieldsMap){
this.queryStr = qry;
this.fieldsMap = clonedFieldsMap;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(queryStr);
}
global void execute(Database.BatchableContext BC, List<sobject> scope) {
List<sObject> sObjsToUpdate = new List<sObject>();
if (scope.size() > 0) {
for (sObject sObj : scope) {
for (String fieldAPIName : fieldsMap.keySet()) {
sObj.put(fieldsMap.get(fieldAPIName), sObj.get(fieldAPIName));
}
sObjsToUpdate.add(sObj);
}
update sObjsToUpdate;
}
}
global void finish(Database.BatchableContext BC) {
System.debug('records finished process.');
}
}
------------------PLEASE HELP