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
RadnipRadnip 

System.isBatch() always false

I just wanted to check my understanding of the System.isBatch() method. Essentially if your implementing batchable would you expect System.isBatch() to be true?

 

global class myClass implements Database.Batchable<sObject>{

	global final String query;
	
	/* Constructor */
	global myClass(String q){
		query = q;
	}

	global Database.QueryLocator start(Database.BatchableContext bc){
  		System.debug('*** Batch start  ***');
		return Database.getQueryLocator(query); 
	}
	
	global void execute(Database.BatchableContext BC, List<sObject> myList) {
		
		// Run Stuff.
		System.debug(System.isBatch());
		
	}
	
	global void finish(Database.BatchableContext BC){
		System.debug('*** Batch finish ***');
	}
 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
RadnipRadnip

Just to let everyone know that I've just had confirmation from Salesforce that there is an issue with System.isBatch() and product development is working on a fix. I also tested System.isScheduled() which didn't work either so think there is an issue with that as well.

All Answers

RadnipRadnip

Just to let everyone know that I've just had confirmation from Salesforce that there is an issue with System.isBatch() and product development is working on a fix. I also tested System.isScheduled() which didn't work either so think there is an issue with that as well.

This was selected as the best answer
Force2b_MikeForce2b_Mike

I'm running into this same problem with one of my clients. Is there a work-around, or possibly an ETA on a bug fix?

 

Best Regards,

 

Mike

RadnipRadnip

I've asked if they can put it up on the known issues list so we can keep an eye on it:

 

http://success.salesforce.com/issues_index

 

 

virkvirk
Can someone help me in writing a code snippet for these methods:

public static void hierarchyAccountDeploiment(List<Account> accs, Map<Id, Account> oldMap)
    {
        List<Id> accIds = new List<Id>();
        Boolean hasHierarchy;
        List<Account> toUpdate = new List<Account>();

        for (Account a : accs)
            if (a.RecordTypeID == '012D00000002qdE' && a.R_seau_int_gr__c == true && a.Etat_relation_GEO__c == 'Déploiement' && (oldmap == null || oldMap.get(a.Id).Etat_relation_GEO__c != 'Déploiement'))
            {
                hasHierarchy = true;
                accIds.add(a.id);
                while (hasHierarchy)
                {
                    List<Account> res = [SELECT id, Etat_relation_GEO__c FROM Account WHERE Etat_relation_GEO__c != 'Terminé' AND parentId IN :accIds];
                    accIds.clear();
                    if (res == null || res.size() == 0)
                        hasHierarchy = false;
                    for (Account aa : res)
                    {
                        accIds.add(aa.id);
                        toUpdate.add(aa);
                    }
                }
            }
        for (Account a : toUpdate)
            a.Etat_relation_GEO__c = 'Déploiement';
        if (toUpdate != null && toUpdate.size() > 0)
            update toUpdate;
    }


    public static void hierarchyAccountTerminer(List<Account> accs, Map<Id, Account> oldMap)
    {
        List<Id> accIds = new List<Id>();
        Boolean hasHierarchy;
        List<Account> toUpdate = new List<Account>();

        for (Account a : accs)
            if (a.RecordTypeID == '012D00000002qdE' && a.R_seau_int_gr__c == true && a.Etat_relation_GEO__c == 'Terminé' && (oldmap == null || oldMap.get(a.Id).Etat_relation_GEO__c != 'Terminé'))
            {
                hasHierarchy = true;
                accIds.add(a.id);
                while (hasHierarchy)
                {
                    List<Account> res = [SELECT id, Etat_relation_GEO__c FROM Account WHERE parentId IN :accIds];
                    accIds.clear();
                    if (res == null || res.size() == 0)
                        hasHierarchy = false;
                    for (Account aa : res)
                    {
                        accIds.add(aa.id);
                        toUpdate.add(aa);
                    }
                }
            }
        for (Account a : toUpdate)
            a.Etat_relation_GEO__c = 'Terminé';
        if (toUpdate != null && toUpdate.size() > 0)
            update toUpdate;
    }

public static void terminerFilByAccount(List<Account> accs)
    {
        List<Id> accIds = new List<Id>();
        List<Contexte__c> toUpdate = new List<Contexte__c>();

        for (Account a : accs)
            if (a.Etat_relation_GEO__c == 'Terminé')
                accIds.add(a.id);
        for (Contexte__c fil : [SELECT id, Statut__c FROM Contexte__c WHERE (PART_ETX__c IN :accIds OR PART_Siege__c IN :accIds OR PART_CA__c IN :accIds) AND Statut__c != 'Terminé'])
            if (allowedToChangeStatus(fil.Statut__c, 'Terminé'))
            {
                fil.Statut__c = 'Terminé';
                toUpdate.add(fil);
            }
        if (toUpdate != null && toUpdate.size() > 0)
            update toUpdate;
    }

I am calling these methods from a trigger

if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
    {
        UpdateStatus.hierarchyAccountDeploiment(trigger.new, trigger.oldMap);
        UpdateStatus.hierarchyAccountTerminer(trigger.new, trigger.oldMap);
        UpdateStatus.terminerFilByAccount(trigger.new);
    }

I want to autmate this. If there is a batch callling this trigger it should check the table for change in status and run the soql, call the respective method and empty the table of satus change.

I am tootally new to this kind of coding any sort of help will be kind.

Thanks,