Untitled diff
public with sharing class ZipAndSubDivisionPopulationWorkflow implements TriggerWorkflow
public with sharing class ZipAndSubDivisionPopulationWorkflow implements TriggerWorkflow
{
{
    List<Merchant_Addresses__c> newMAList, oldMAList;
    List<Merchant_Addresses__c> newMAList, oldMAList;
    List<Address__c> newRAList, oldRAList;
    List<Address__c> newRAList, oldRAList;
    Boolean isBeforeForMA, isAfterForMA, isBeforeForRA, isAfterForRA;
    Boolean isBeforeForMA, isAfterForMA, isBeforeForRA, isAfterForRA;
    Boolean isMATrigger, isRATrigger;
    Boolean isMATrigger, isRATrigger;
    public ZipAndSubDivisionPopulationWorkflow()
    public ZipAndSubDivisionPopulationWorkflow()
    {
    {
        List<Sobject> triggerRecords = Trigger.New;
        List<Sobject> triggerRecords = Trigger.New;
        if(triggerRecords != null && triggerRecords.size() > 0)
        if(triggerRecords != null && triggerRecords.size() > 0)
        {
        {
            if(triggerRecords[0].getSObjectType() == Merchant_Addresses__c.sObjectType)
            if(triggerRecords[0].getSObjectType() == Merchant_Addresses__c.sObjectType)
            {
            {
                this((List<Merchant_Addresses__c>)Trigger.New, (List<Merchant_Addresses__c>)Trigger.Old, Trigger.isBefore, Trigger.isAfter);
                this((List<Merchant_Addresses__c>)Trigger.New, (List<Merchant_Addresses__c>)Trigger.Old, Trigger.isBefore, Trigger.isAfter);
            }
            }
            else if(triggerRecords[0].getSObjectType() == Address__c.sObjectType)
            else if(triggerRecords[0].getSObjectType() == Address__c.sObjectType)
            {
            {
                this((List<Address__c>)Trigger.New, (List<Address__c>)Trigger.Old, Trigger.isBefore, Trigger.isAfter);
                this((List<Address__c>)Trigger.New, (List<Address__c>)Trigger.Old, Trigger.isBefore, Trigger.isAfter);
            }
            }
        }
        }
    }
    }
    public ZipAndSubDivisionPopulationWorkflow(List<Merchant_Addresses__c> newList, List<Merchant_Addresses__c> oldList , Boolean isBefore, Boolean isAfter)
    public ZipAndSubDivisionPopulationWorkflow(List<Merchant_Addresses__c> newList, List<Merchant_Addresses__c> oldList , Boolean isBefore, Boolean isAfter)
    {
    {
        this.newMAList = newList;
        this.newMAList = newList;
        this.oldMAList = oldList;
        this.oldMAList = oldList;
        this.isBeforeForMA = isBefore;
        this.isBeforeForMA = isBefore;
        this.isAfterForMA = isAfter;
        this.isAfterForMA = isAfter;
        isMATrigger = true;
        isMATrigger = true;
        isRATrigger = false;
        isRATrigger = false;
    }
    }
    public ZipAndSubDivisionPopulationWorkflow(List<Address__c> newList, List<Address__c> oldList , Boolean isBefore, Boolean isAfter)
    public ZipAndSubDivisionPopulationWorkflow(List<Address__c> newList, List<Address__c> oldList , Boolean isBefore, Boolean isAfter)
    {
    {
        this.newRAList = newList;
        this.newRAList = newList;
        this.oldRAList = oldList;
        this.oldRAList = oldList;
        this.isBeforeForRA = isBefore;
        this.isBeforeForRA = isBefore;
        this.isAfterForRA = isAfter;
        this.isAfterForRA = isAfter;
        isMATrigger = false;
        isMATrigger = false;
        isRATrigger = true;
        isRATrigger = true;
    }
    }
    private static Boolean hasExecutedBeforeForMA{ get{if(hasExecutedBeforeForMA == null) hasExecutedBeforeForMA = false; return hasExecutedBeforeForMA;} private set;}
    private static Boolean hasExecutedBeforeForMA{ get{if(hasExecutedBeforeForMA == null) hasExecutedBeforeForMA = false; return hasExecutedBeforeForMA;} private set;}
    private static Boolean hasExecutedAfterForMA{ get{if(hasExecutedAfterForMA == null) hasExecutedAfterForMA = false; return hasExecutedAfterForMA;} private set;}
    private static Boolean hasExecutedAfterForMA{ get{if(hasExecutedAfterForMA == null) hasExecutedAfterForMA = false; return hasExecutedAfterForMA;} private set;}
    private static Boolean hasExecutedBeforeForRA{ get{if(hasExecutedBeforeForRA == null) hasExecutedBeforeForRA = false; return hasExecutedBeforeForRA;} private set;}
    private static Boolean hasExecutedBeforeForRA{ get{if(hasExecutedBeforeForRA == null) hasExecutedBeforeForRA = false; return hasExecutedBeforeForRA;} private set;}
    private static Boolean hasExecutedAfterForRA{ get{if(hasExecutedAfterForRA == null) hasExecutedAfterForRA = false; return hasExecutedAfterForRA;} private set;}
    private static Boolean hasExecutedAfterForRA{ get{if(hasExecutedAfterForRA == null) hasExecutedAfterForRA = false; return hasExecutedAfterForRA;} private set;}
    private static Boolean shouldUpdateAccountWithNewSubDivs{ get{if(shouldUpdateAccountWithNewSubDivs == null) shouldUpdateAccountWithNewSubDivs = false; return shouldUpdateAccountWithNewSubDivs;} private set;}
    private static Boolean shouldUpdateAccountWithNewSubDivs{ get{if(shouldUpdateAccountWithNewSubDivs == null) shouldUpdateAccountWithNewSubDivs = false; return shouldUpdateAccountWithNewSubDivs;} private set;}
    private static Boolean shouldUpdateOpportunityWithNewSubDivs{ get{if(shouldUpdateOpportunityWithNewSubDivs == null) shouldUpdateOpportunityWithNewSubDivs = false; return shouldUpdateOpportunityWithNewSubDivs;} private set;}
    private static Boolean shouldUpdateOpportunityWithNewSubDivs{ get{if(shouldUpdateOpportunityWithNewSubDivs == null) shouldUpdateOpportunityWithNewSubDivs = false; return shouldUpdateOpportunityWithNewSubDivs;} private set;}
    //business logic
    //business logic
    public void execute()
    public void execute()
    {
    {
        if(isMATrigger)
        if(isMATrigger)
        {
        {
            if(isBeforeForMA)
            if(isBeforeForMA)
            {
            {
                if(!hasExecutedBeforeForMA)
                if(!hasExecutedBeforeForMA)
                {
                {
                    updateMerchantAddressesWithZipCodeAndSubDivisionReferences();
                    updateMerchantAddressesWithZipCodeAndSubDivisionReferences();
                    hasExecutedBeforeForMA = true;
                    hasExecutedBeforeForMA = true;
                }
                }
            }
            }
            else if (isAfterForMA && shouldUpdateAccountWithNewSubDivs)
            else if (isAfterForMA && shouldUpdateAccountWithNewSubDivs)
            {
            {
                if(!hasExecutedAfterForMA)
                if(!hasExecutedAfterForMA)
                {
                {
                    //updateParentRecordsWithSubvisions(newMAList, oldMAList, 'Zip_Postal_Code__c', 'Subdivision', 'Account__c', 'Merchant_Addresses__c');
                    //updateParentRecordsWithSubvisions(newMAList, oldMAList, 'Zip_Postal_Code__c', 'Subdivision', 'Account__c', 'Merchant_Addresses__c');
                    this.updateAccountWithSubvisions();
                    this.updateAccountWithSubvisions();
                    hasExecutedAfterForMA = true;
                    hasExecutedAfterForMA = true;
                }
                }
            }
            }
        }
        }
        else if(isRATrigger)
        else if(isRATrigger)
        {
        {
            if(isBeforeForRA)
            if(isBeforeForRA)
            {
            {
                if(!hasExecutedBeforeForRA)
                if(!hasExecutedBeforeForRA)
                {
                {
                    updateRedemptionAddressesWithZipCodeAndSubDivisionReferences();
                    updateRedemptionAddressesWithZipCodeAndSubDivisionReferences();
                    hasExecutedBeforeForMA = true;
                    hasExecutedBeforeForMA = true;
                }
                }
            }
            }
            else if (isAfterForRA && shouldUpdateOpportunityWithNewSubDivs)
            else if (isAfterForRA && shouldUpdateOpportunityWithNewSubDivs)
            {
            {
                if(!hasExecutedAfterForRA)
                if(!hasExecutedAfterForRA)
                {
                {
                    //updateParentRecordsWithSubvisions(newRAList, oldRAList, 'Zip_Postal_Code__c', 'Subdivision', 'Opportunity__c', 'Address__c');
                    //updateParentRecordsWithSubvisions(newRAList, oldRAList, 'Zip_Postal_Code__c', 'Subdivision', 'Opportunity__c', 'Address__c');
                    this.updateOpportunitysWithSubvisions();
                    this.updateOpportunitysWithSubvisions();
                    hasExecutedAfterForRA = true;
                    hasExecutedAfterForRA = true;
                }
                }
            }
            }
        }
        }
    }
    }
    private void updateMerchantAddressesWithZipCodeAndSubDivisionReferences()
    private void updateMerchantAddressesWithZipCodeAndSubDivisionReferences()
    {
    {
        Map<String, List<Merchant_Addresses__c>> qualifiedMAsMappedByZipCodes = AddressServices.getQualifiedZipCodesToAddressObjectMap(newMAList, oldMAList, 'Zip_Postal_Code__c', 'State_Province__c');
        List<Merchant_Addresses__c> oldM=new List<Merchant_Addresses__c>();
        List<Merchant_Addresses__c> newM= new List<Merchant_Addresses__c>();
        for(Integer i=0;i<newMAList.size();i++){
            if(newMaList[i].Country_Code__c=='US'||newMaList[i].Country_Code__c=='CA'||newMaList[i].Country_Code__c==''){
                oldM.add(oldMAList[i]);
                newM.add(newMAList[i]);
            }
        }
        Map<String, List<Merchant_Addresses__c>> qualifiedMAsMappedByZipCodes = AddressServices.getQualifiedZipCodesToAddressObjectMap(newM, oldM, 'Zip_Postal_Code__c', 'State_Province__c');
        if(qualifiedMAsMappedByZipCodes != null && qualifiedMAsMappedByZipCodes.size() > 0)
        if(qualifiedMAsMappedByZipCodes != null && qualifiedMAsMappedByZipCodes.size() > 0)
        {
        {
            AddressServices.assignZipCodeAndSubDivision(qualifiedMAsMappedByZipCodes, 'Zip_Code__c', 'Subdivision__c');
            AddressServices.assignZipCodeAndSubDivision(qualifiedMAsMappedByZipCodes, 'Zip_Code__c', 'Subdivision__c');
            //accounts should only be updated in the "after" part of trigger if there actually was a zipcode update
            //accounts should only be updated in the "after" part of trigger if there actually was a zipcode update
            shouldUpdateAccountWithNewSubDivs = true;
            shouldUpdateAccountWithNewSubDivs = true;
        }
        }
    }
    }
    private void updateRedemptionAddressesWithZipCodeAndSubDivisionReferences()
    private void updateRedemptionAddressesWithZipCodeAndSubDivisionReferences()
    {
    {
        List<Address__c> oldM=new List<Address__c>();
        List<Address__c> newM= new List<Address__c>();
        for(Integer i=0;i<newRAList.size();i++){
            if(newRAList[i].Country_Code__c=='US'||newRAList[i].Country_Code__c=='CA'||newRAList[i].Country_Code__c==''){
                oldM.add(oldRAList[i]);
                newM.add(newRAList[i]);
            }
        }
        Map<String, List<Address__c>> qualifiedRAsMappedByZipCodes = AddressServices.getQualifiedZipCodesToAddressObjectMap(newRAList, oldRAList, 'Zip_Postal_Code__c', 'State__c');
        Map<String, List<Address__c>> qualifiedRAsMappedByZipCodes = AddressServices.getQualifiedZipCodesToAddressObjectMap(newRAList, oldRAList, 'Zip_Postal_Code__c', 'State__c');
        if(qualifiedRAsMappedByZipCodes != null && qualifiedRAsMappedByZipCodes.size() > 0)
        if(qualifiedRAsMappedByZipCodes != null && qualifiedRAsMappedByZipCodes.size() > 0)
        {
        {
            AddressServices.assignZipCodeAndSubDivision(qualifiedRAsMappedByZipCodes, 'Dynamic_Zip_Code__c', 'Subdivision__c');
            AddressServices.assignZipCodeAndSubDivision(qualifiedRAsMappedByZipCodes, 'Dynamic_Zip_Code__c', 'Subdivision__c');
            //opptys should only be updated in the "after" part of trigger if there actually was a zipcode update
            //opptys should only be updated in the "after" part of trigger if there actually was a zipcode update
            shouldUpdateOpportunityWithNewSubDivs = true;
            shouldUpdateOpportunityWithNewSubDivs = true;
        }
        }
    }
    }
    //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 
    //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 
    private void updateAccountWithSubvisions()
    private void updateAccountWithSubvisions()
    {
    {
        Set<ID> accountIDs = new Set<ID>();
        Set<ID> accountIDs = new Set<ID>();
        Integer x = 0;
        Integer x = 0;
        for(Merchant_Addresses__c ma: newMAList)
        for(Merchant_Addresses__c ma: newMAList)
        {
        {
            if(oldMAList == null || (ma.Zip_Postal_Code__c != oldMAList[x].Zip_Postal_Code__c))
            if(oldMAList == null || (ma.Zip_Postal_Code__c != oldMAList[x].Zip_Postal_Code__c))
                accountIDs.add(ma.Account__c);
                accountIDs.add(ma.Account__c);
            x++;
            x++;
        }
        }
        if(accountIDs.size() > 0)
        if(accountIDs.size() > 0)
        {
        {
            Map<ID, Set<String>> mapAccountIDToSubDivs = new Map<ID, Set<String>>();
            Map<ID, Set<String>> mapAccountIDToSubDivs = new Map<ID, Set<String>>();
            for(Merchant_Addresses__c ma: [Select ID, Account__c, Subdivision__c, Subdivision__r.Name, Zip_Postal_Code__c from Merchant_Addresses__c where Account__c in : accountIDs])
            for(Merchant_Addresses__c ma: [Select ID, Account__c,Account__r.Feature_Country__c, Subdivision__c, Subdivision__r.Name, Zip_Postal_Code__c from Merchant_Addresses__c where Account__c in : accountIDs])
            {
            {
                Set<String> subDivs = new Set<String>();
                if(ma.Account__r.Feature_Country__c=='US'||ma.Account__r.Feature_Country__c=='CA'||ma.Account__r.Feature_Country__c==''){
                if(mapAccountIDToSubDivs.containsKey(ma.Account__c))
                    Set<String> subDivs = new Set<String>();
                    subDivs = mapAccountIDToSubDivs.get(ma.Account__c);
                    if(mapAccountIDToSubDivs.containsKey(ma.Account__c))
                        subDivs = mapAccountIDToSubDivs.get(ma.Account__c);
                if(ma.Subdivision__r.Name != null)
                {
                    if(ma.Subdivision__r.Name != null)
                    subDivs.add(ma.Subdivision__r.Name);
                    {
                    mapAccountIDToSubDivs.put(ma.Account__c, subDivs);
                        subDivs.add(ma.Subdivision__r.Name);
                        mapAccountIDToSubDivs.put(ma.Account__c, subDivs);
                    }
                }
                }
            }
            }
            if(mapAccountIDToSubDivs.size() > 0)
            if(mapAccountIDToSubDivs.size() > 0)
            {
            {
                List<Account> accountsToUpdate = new List<Account>();
                List<Account> accountsToUpdate = new List<Account>();
                for(ID accID: mapAccountIDToSubDivs.keySet())
                for(ID accID: mapAccountIDToSubDivs.keySet())
                {
                {
                    Account a = new Account(ID = accID);
                    Account a = new Account(ID = accID);
                    if(mapAccountIDToSubDivs.get(accID).size() > 99)
                    if(mapAccountIDToSubDivs.get(accID).size() > 99)
                        a.Subdivision__c = 'National Account: Too many subdivisions';
                        a.Subdivision__c = 'National Account: Too many subdivisions';
                    else
                    else
                    {
                    {
                        a.Subdivision__c = GrouponUtils.getCSVFromSet(mapAccountIDToSubDivs.get(accID), ';');
                        a.Subdivision__c = GrouponUtils.getCSVFromSet(mapAccountIDToSubDivs.get(accID), ';');
                        accountsToUpdate.add(a);
                        accountsToUpdate.add(a);
                    }
                    }
                }
                }
                if(accountsToUpdate.size() > 0)
                if(accountsToUpdate.size() > 0)
                    update accountsToUpdate;
                    update accountsToUpdate;
            }
            }
        }
        }
    }
    }
    //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 
    //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 
    private void updateOpportunitysWithSubvisions()
    private void updateOpportunitysWithSubvisions()
    {
    {
        Set<ID> opptyIDs = new Set<ID>();
        Set<ID> opptyIDs = new Set<ID>();
        Integer x = 0;
        Integer x = 0;
        for(Address__c ra: newRAList)
        for(Address__c ra: newRAList)
        {
        {
            if(ra.Country_Code__c =='US'||ra.Country_Code__c =='CA'||ra.Country_Code__c==''){
            if(oldRAList == null || (ra.Zip_Postal_Code__c != oldRAList[x].Zip_Postal_Code__c))
            if(oldRAList == null || (ra.Zip_Postal_Code__c != oldRAList[x].Zip_Postal_Code__c))
                opptyIDs.add(ra.Opportunity__c);
                opptyIDs.add(ra.Opportunity__c);
            }    
            x++;
            x++;
        }
        }
        if(opptyIDs.size() > 0)
        if(opptyIDs.size() > 0)
        {
        {
            Map<ID, Set<String>> mapOpptyIDToSubDivs = new Map<ID, Set<String>>();
            Map<ID, Set<String>> mapOpptyIDToSubDivs = new Map<ID, Set<String>>();
            for(Address__c ra: [Select ID, Opportunity__c, Subdivision__c, Subdivision__r.Name, Zip_Postal_Code__c from Address__c where Opportunity__c in : opptyIDs])
            for(Address__c ra: [Select ID, Opportunity__c, Subdivision__c, Subdivision__r.Name, Zip_Postal_Code__c from Address__c where Opportunity__c in : opptyIDs])
            {
            {
                Set<String> subDivs = new Set<String>();
                Set<String> subDivs = new Set<String>();
                if(mapOpptyIDToSubDivs.containsKey(ra.Opportunity__c))
                if(mapOpptyIDToSubDivs.containsKey(ra.Opportunity__c))
                    subDivs = mapOpptyIDToSubDivs.get(ra.Opportunity__c);
                    subDivs = mapOpptyIDToSubDivs.get(ra.Opportunity__c);
                if(ra.Country_Code__c =='US'||ra.Country_Code__c =='CA'||ra.Country_Code__c==''){
                if(ra.Subdivision__r.Name != null)
                    if(ra.Subdivision__r.Name != null)
                {
                    {
                    subDivs.add(ra.Subdivision__r.Name);
                        subDivs.add(ra.Subdivision__r.Name);
                    mapOpptyIDToSubDivs.put(ra.Opportunity__c, subDivs);
                        mapOpptyIDToSubDivs.put(ra.Opportunity__c, subDivs);
                    }
                }
                }
            }
            }
            System.debug('\n\nmapOpptyIDToSubDivs = '+mapOpptyIDToSubDivs+'\n\n');
            System.debug('\n\nmapOpptyIDToSubDivs = '+mapOpptyIDToSubDivs+'\n\n');
            if(mapOpptyIDToSubDivs.size() > 0)
            if(mapOpptyIDToSubDivs.size() > 0)
            {
            {
                List<Opportunity> opptysToUpdate = new List<Opportunity>();
                List<Opportunity> opptysToUpdate = new List<Opportunity>();
                for(ID opptyID: mapOpptyIDToSubDivs.keySet())
                for(ID opptyID: mapOpptyIDToSubDivs.keySet())
                {
                {
                    Opportunity oppty = new Opportunity(ID = opptyID);
                    Opportunity oppty = new Opportunity(ID = opptyID);
                    if(mapOpptyIDToSubDivs.get(opptyID).size() > 99)
                    if(mapOpptyIDToSubDivs.get(opptyID).size() > 99)
                        oppty.Subdivision__c = 'National Account: Too many subdivisions';
                        oppty.Subdivision__c = 'National Account: Too many subdivisions';
                    else
                    else
                    {
                    {
                        oppty.Subdivision__c = GrouponUtils.getCSVFromSet(mapOpptyIDToSubDivs.get(opptyID), ';');
                        oppty.Subdivision__c = GrouponUtils.getCSVFromSet(mapOpptyIDToSubDivs.get(opptyID), ';');
                        opptysToUpdate.add(oppty);
                        opptysToUpdate.add(oppty);
                    }
                    }
                }
                }
                if(opptysToUpdate.size() > 0)
                if(opptysToUpdate.size() > 0)
                    update opptysToUpdate;
                    update opptysToUpdate;
            }
            }
        }
        }
    }
    }
    /*
    /*
    //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 
    //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 
    private void updateParentRecordsWithSubvisions(List<SObject> newList, List<SObject> oldList, String zipFieldName, String subdivisionFieldName, String parentReferenceFieldName ,String SObjectName )
    private void updateParentRecordsWithSubvisions(List<SObject> newList, List<SObject> oldList, String zipFieldName, String subdivisionFieldName, String parentReferenceFieldName ,String SObjectName )
    {
    {
        Set<String> parentIDs = new Set<String>();
        Set<String> parentIDs = new Set<String>();
        Integer x = 0;
        Integer x = 0;
        for(SObject addressRecord: newList)
        for(SObject addressRecord: newList)
        {
        {
            if(oldList == null || (addressRecord.get(zipFieldName) != oldList[x].get(zipFieldName)))
            if(oldList == null || (addressRecord.get(zipFieldName) != oldList[x].get(zipFieldName)))
                parentIDs.add(String.valueOf(addressRecord.get(parentReferenceFieldName)));
                parentIDs.add(String.valueOf(addressRecord.get(parentReferenceFieldName)));
            x++;
            x++;
        }
        }
        if(parentIDs.size() > 0)
        if(parentIDs.size() > 0)
        {
        {
            Map<String, Set<String>> mapParentIDToSubDivs = new Map<String, Set<String>>();
            Map<String, Set<String>> mapParentIDToSubDivs = new Map<String, Set<String>>();
            String query = 'Select ID, '+SubdivisionFieldName+'__c, '+SubdivisionFieldName+'__r.Name, '+parentReferenceFieldName+' from '+SobjectName+' where '+parentReferenceFieldName+' in: parentIDs';
            String query = 'Select ID, '+SubdivisionFieldName+'__c, '+SubdivisionFieldName+'__r.Name, '+parentReferenceFieldName+' from '+SobjectName+' where '+parentReferenceFieldName+' in: parentIDs';
            System.debug('\n\nquery = '+query+'\n\n');
            System.debug('\n\nquery = '+query+'\n\n');
            for(SObject sObj: Database.query(query))
            for(SObject sObj: Database.query(query))
            {
            {
                Set<String> subDivs = new Set<String>();
                Set<String> subDivs = new Set<String>();
                if(mapParentIDToSubDivs.containsKey(String.valueOf(sObj.get(parentReferenceFieldName))))
                if(mapParentIDToSubDivs.containsKey(String.valueOf(sObj.get(parentReferenceFieldName))))
                    subDivs =  mapParentIDToSubDivs.get(String.valueOf(sObj.get(parentReferenceFieldName)));
                    subDivs =  mapParentIDToSubDivs.get(String.valueOf(sObj.get(parentReferenceFieldName)));
                if(sObj.get(subdivisionFieldName+'__r.Name') != null)
                if(sObj.get(subdivisionFieldName+'__r.Name') != null)
                {
                {
                    subDivs.add(String.valueOf(sObj.get(subdivisionFieldName+'__r.Name')));
                    subDivs.add(String.valueOf(sObj.get(subdivisionFieldName+'__r.Name')));
                    mapParentIDToSubDivs.put(String.valueOf(sObj.get(parentReferenceFieldName)), subDivs);
                    mapParentIDToSubDivs.put(String.valueOf(sObj.get(parentReferenceFieldName)), subDivs);
                }
                }
            }
            }
            //System.debug('\n\nmapOpptyIDToSubDivs = '+mapOpptyIDToSubDivs+'\n\n');
            //System.debug('\n\nmapOpptyIDToSubDivs = '+mapOpptyIDToSubDivs+'\n\n');
            if(mapParentIDToSubDivs.size() > 0)
            if(mapParentIDToSubDivs.size() > 0)
            {
            {
                List<SObject> parentsToUpdate = new List<SObject>();
                List<SObject> parentsToUpdate = new List<SObject>();
                for(String parentID: mapParentIDToSubDivs.keySet())
                for(String parentID: mapParentIDToSubDivs.keySet())
                {
                {
                    SObject parentRecord;
                    SObject parentRecord;
                    if(isMATrigger) 
                    if(isMATrigger) 
                        parentRecord = new Account(ID = parentID);
                        parentRecord = new Account(ID = parentID);
                    else if(isRATrigger)
                    else if(isRATrigger)
                        parentRecord = new Opportunity(ID = parentID);
                        parentRecord = new Opportunity(ID = parentID);
                    if(mapParentIDToSubDivs.get(parentID).size() > 99)
                    if(mapParentIDToSubDivs.get(parentID).size() > 99)
                        parentRecord.put(subdivisionFieldName+'__c', 'National Account: Too many subdivisions');
                        parentRecord.put(subdivisionFieldName+'__c', 'National Account: Too many subdivisions');
                    else
                    else
                    {
                    {
                        parentRecord.put(subdivisionFieldName+'__c', GrouponUtils.getCSVFromSet(mapParentIDToSubDivs.get(parentID), ';'));
                        parentRecord.put(subdivisionFieldName+'__c', GrouponUtils.getCSVFromSet(mapParentIDToSubDivs.get(parentID), ';'));
                        parentsToUpdate.add(parentRecord);
                        parentsToUpdate.add(parentRecord);
                    }
                    }
                }
                }
                if(parentsToUpdate.size() > 0)
                if(parentsToUpdate.size() > 0)
                    update parentsToUpdate;
                    update parentsToUpdate;
            }
            }
        }
        }
    }
    }
    */
    */
}
}