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
Kathryn BullockKathryn Bullock 

Future Batch Error on Batch job

Hello,

I am receiving this error when I run a batch job on my accounts: The flow tried to update these records: null. This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: DrivingDistanceTrigger: execution of AfterUpdate caused by: System.AsyncException: Future method cannot be called from a future or batch method: DrivingDistance1.calculateDistances(String) ()

I need this flow to work, but I don't know what is going wrong.  Please help!

Class:
public class DrivingDistance1 {
    
    public static boolean isExecuting = false;
    
    public static void execute(List<Account> triggerNew, list<Account> triggerOld){
        //filter unneeded objects
        //modify needed objects
        //save!
       	list<Account> filtered = filterAccounts(triggerNew, triggerOld);
        if(!System.isFuture()){
            for(Account f:filtered){
            	calculateDistances(f.id);
       		}
        }
    }
    
    private static List<Account> filterAccounts(List<Account> triggerNew, List<Account> triggerOld){
        List<Account> returnList = new List<Account>();
        for(Integer i = 0; i < triggerNew.size(); i++){
            if((triggerOld[i].UCO_service_provider__c != triggerNew[i].UCO_service_provider__c) || (triggerOld[i].shippingStreet != triggerNew[i].shippingStreet) ){
                returnList.add(triggerNew[i]);
            }
        }
        return returnList;
    }    
    
    @Future(callout=true)
    public static void calculateDistances(String AccountId){
		geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
        // this will get the accounts where you already have the id.
        // ie: accounts that have changed.
        Account acc = [SELECT id, 
                                 distance1__c, 
                                 UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Latitude__c, 
                                 UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Longitude__c, 
                                 geopointe__Geocode__r.geopointe__Latitude__c, 
                                 geopointe__Geocode__r.geopointe__Longitude__c FROM Account WHERE Id = :AccountId LIMIT 1];
        
        ds.add(acc.geopointe__Geocode__r.geopointe__Latitude__c, acc.geopointe__Geocode__r.geopointe__Longitude__c,
                  acc.UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Latitude__c,
                  acc.UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Longitude__c); 
        
		List<double> distances = ds.getListOfDistances();
		acc.distance1__c = distances[0];		

        update acc;
    }
    
    
    public static void m1(List<Account> dAccount) {
        geopointe.API.DistanceService ds = new geopointe.API.DistanceService();
        // this will get the accounts where you already have the id.
        // ie: accounts that have changed.
        List<Account> accList = [SELECT id, 
                                 distance1__c, 
                                 UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Latitude__c, 
                                 UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Longitude__c, 
                                 geopointe__Geocode__r.geopointe__Latitude__c, 
                                 geopointe__Geocode__r.geopointe__Longitude__c FROM Account WHERE Id IN: dAccount];
        //create two lists.
        List<Account> origins = new List<Account>();
        List<Account> destinations = new List<Account>();
        
        // create list of origins and uco providers
        for(Account working:accList){
            ds.add(working.geopointe__Geocode__r.geopointe__Latitude__c, working.geopointe__Geocode__r.geopointe__Longitude__c,
                  working.UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Latitude__c,
                  working.UCO_Service_Provider__r.Geopointe_Geocode__r.geopointe__Longitude__c);
        }
            
		//a list is ordered!
		List<double> distances = ds.getListOfDistances();
		
        for(Integer i = 0; i < accList.size(); i++){
            accList[i].distance1__c = distances[i];
        }

        update accList;
    }
}

Trigger:
trigger DrivingDistanceTrigger on Account (after update) {
    
    // because were running in an after update context
    // we have trigger.new, and trigger.old 
    //engage semaphor when the uco object or address has changed
    // disengage when it's not
	DrivingDistance1.execute(Trigger.New, Trigger.Old);
   
    
}

 
Raj VakatiRaj Vakati
change your trigger to before update that wil solve your problem 
 
trigger DrivingDistanceTrigger on Account (before update) {
    
    // because were running in an after update context
    // we have trigger.new, and trigger.old 
    //engage semaphor when the uco object or address has changed
    // disengage when it's not
	DrivingDistance1.execute(Trigger.New, Trigger.Old);
   
    
}