You need to sign in to do that
Don't have an account?

APEX Batch not updating field
I have a batch class that updated a custom object when a trigger runs.
Sceniro: Someone adds a state to a multi select picklist. The trigger will grab all states, and throw the last state in the variable. I then grab the id of the record in another variable. I pass both of those variables over to a batch class and run a query on another custome object, and update the fields.
Problem is, when I get to the update part, the first field (Flow_Kick_Off__c) gets the value of the variable, but the other field (Territory__c) does not..... I don't understand why one field is getting updated, but not the other.
Here is the trigger:
Here is the class: The only field that is not updating is (Territory__c)
Sceniro: Someone adds a state to a multi select picklist. The trigger will grab all states, and throw the last state in the variable. I then grab the id of the record in another variable. I pass both of those variables over to a batch class and run a query on another custome object, and update the fields.
Problem is, when I get to the update part, the first field (Flow_Kick_Off__c) gets the value of the variable, but the other field (Territory__c) does not..... I don't understand why one field is getting updated, but not the other.
Here is the trigger:
trigger updateFacility on Territory__c (before insert, before update) { //Adding a state index is -1. Removing a state index is normal. String[] oldStates; String[] newStates; Integer countStates; Integer countOldStates; Integer countNewStates; String terrID; String terrState; if(Trigger.isUpdate) { for(Territory__c oldTerr : Trigger.old) { oldStates = oldTerr.states__c.split(';'); countOldStates = oldStates.size(); terrID = oldTerr.Id; } } else if(Trigger.isInsert) { } for(Territory__c newTerr : Trigger.new) { newStates = newTerr.states__c.split(';'); countNewStates = newStates.size(); if(countNewStates > countOldStates) { terrState = newStates[countNewStates-1]; System.debug('TRIGGER ID = ' + terrID); System.debug('TRIGGER STATE = ' + terrState); FacilityUpdates db = new FacilityUpdates(terrState, terrID); database.executeBatch(db, 75); } else { } } }
Here is the class: The only field that is not updating is (Territory__c)
global class FacilityUpdates implements Database.Batchable<sObject> { global String terrState; global String terrID; global FacilityUpdates(String myStates, String terID){ terrState = myStates; terrID = terID; system.debug('STATE = ' + myStates); system.debug('ID = ' + terID); } public String query; global Database.querylocator start(Database.BatchableContext BC){ //String newString = '\''; system.debug('INSIDE EXECUTE STATE = ' + terrState); query = 'Select id, Name, physical_State__c, Territory__c FROM Facility__c WHERE physical_State__c = :terrState'; //query = 'Select id, Name, physical_State__c, Territory__c FROM Facility__c WHERE Territory__c = :terrId AND physical_State__c includes ' + states; system.debug(query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject> scope){ List<Facility__c> accns = new List<Facility__c>(); for(sObject s : scope) { Facility__c a = (Facility__c)s; a.Flow_Kickoff__c = terrID; a.Territory__c = terrID; accns.add(a); } update accns; } global void finish(Database.BatchableContext BC) { } }
All Answers
In batch code.
You are updating same value to Flow kick off and Territory field with terrId variable while terrId get value in update trigger not in insert trigger
But terrState get value in insert while terrId get value in update trigger.
Without executing i cant reach to conclusion but thinga are not well in trigger code.