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
virkvirk 

Can someone help me in understanding this code what is this doing exactly

This is called from a trigger which passes the list of new map and old map.

 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;
    }

Any help will be really helpful.
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- in this code you are updating parent account Etat_relation_GEO__c = 'Déploiement' .

Please check below comment
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>();
		
		// Looping on List of 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'))
            {
				// Code will come here once Account Record Type is 012D00000002qdE and R_seau_int_gr__c is checked and Etat_relation_GEO__c is Déploiement
				// Or in case of update it will execute once Etat_relation_GEO__c will be Déploiement and value of Etat_relation_GEO__c was not Déploiement
                hasHierarchy = true;
                accIds.add(a.id);
                while (hasHierarchy) // This in infinite loop will exit if res is null ot size is zero
                {
					// Below is Query inside the for loop which is not salesforce best pratice
					// Below query is fatching parent account
                    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) // here updating all parent record Etat_relation_GEO__c as Déploiement
            a.Etat_relation_GEO__c = 'Déploiement';
        if (toUpdate != null && toUpdate.size() > 0)
            update toUpdate;
    }

Let us know if this will help you
 
virkvirk
Thanks Amit. In this code I am not able to understand that intially I have new account map for which I am checking the a.RecordTypeID, a.Etat_relation_GEO__c  and in old map Etat_relation_GEO__c if the condition is true it wil execute the internal query which is agn quering Account and clearing accIds. And then again adding data to the accIds.What I understood is that this code is updating the accounts status to déploiment. What are the parent ids here? Also CCan you please help me in writing all this code in a batch class scheduled from a scheduler, which traces the records and get the new object with required fields for this update and then do the update and I can empty that new object in finish method of batch.