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
Brandon NelsonBrandon Nelson 

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:
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) {
        
    }
    
}

 
Best Answer chosen by Brandon Nelson
Brandon NelsonBrandon Nelson
SOLVED: The trigger and batch class was fine. Turns out we had another trigger doing an update on the same field so they were fighting with each other. Once I found that out, I was able to modify the other trigger to run on when needed not on every update then it worked out. 

All Answers

Ankush SomaniAnkush Somani
In inesrt trigger part, you are checking countOldState variable which will be always null.
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. 
Brandon NelsonBrandon Nelson
@Ankush, no, not at all. Sorry, but you read that wrong. The trigger is is doing all the logic and passing the resulting IDs to the batch and the batch is processing the records in another object. 
Tad Aalgaard 3Tad Aalgaard 3
The logic in your trigger if flawed.  If this trigger receives more than one record then the last record read in the first for loop will be the only values associated to the oldStates, countOldStates, and terrID will be the values from the last record.  Any previous assigments to those variables are overwritten by the next record that is read.

 
Brandon NelsonBrandon Nelson
Actually the Trigger is now flawed. It works as intended. I'm very upset at the answers I have gotten from here as it seems none have been able to even understand what the trigger was doing. I will be answering my own question since I figured it out. 
Brandon NelsonBrandon Nelson
SOLVED: The trigger and batch class was fine. Turns out we had another trigger doing an update on the same field so they were fighting with each other. Once I found that out, I was able to modify the other trigger to run on when needed not on every update then it worked out. 
This was selected as the best answer