Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
/*****************************
* Class : AccountTrimHelper
* Created By : FuseIT
-----------------------------------------------------------------------------------
* @description : Utility Class for Account TRIM interaction
-----------------------------------------------------------------------------------
* 
****************************/
public without sharing class AccountTrimHelper {



    /*****************************
    * @method  onAfterInsert
    * @description  Check if the trigger is enabled and call createNewAccountContainers
    * @param  newAccountsList
    ****************************/
    public static void onAfterInsert(List<Account> newAccountsList) {

       if (fuseit_t4s.TrimCustomSettingHelper.isTriggerEnabled) {
            createNewAccountContainers(newAccountsList);
        }
    }


    /*****************************
    * @method  onAfterUpdate
    * @description  Check if the trigger is enabled and call checkAccountContainers
    * @param  accountsList
    * @param  oldAccountMap
    ****************************/
    public static void onAfterUpdate(List<Account> accountsList, Map<Id,Account> oldAccountMap) {

        if (fuseit_t4s.TrimCustomSettingHelper.isTriggerEnabled) {
            checkAccountContainers(accountsList, oldAccountMap);
        }
    }

    /*****************************
    * @method  onAfterDelete
    * @description  Delete orphan Trim Records when their parent Account has been deleted.
    * @param  oldAccountMap
    ****************************/
    public static void onAfterDelete(Map<Id,Account> oldAccountMap) {

        if (fuseit_t4s.TrimCustomSettingHelper.isTriggerEnabled) {
            deleteOrphanTrimRecords(oldAccountMap.keySet());
        }
    }

    /*****************************
    * @method  createNewAccountContainers
    * @description  Create new Account Containers if T4S Setting is a match
    * @param  newAccountsList
    ****************************/
    private static void createNewAccountContainers(List<Account> newAccountsList) {

        List<fuseit_t4s__Trim_Setting__c> settings = new List<fuseit_t4s__Trim_Setting__c>();

        String prefix = '001'; //Account

        try {

            settings = [Select Id, Name From fuseit_t4s__Trim_Setting__c
                Where fuseit_t4s__Auto_Create_Sub_Container__c = true And  
                fuseit_t4s__sObject_Prefix__c =: prefix];
            
            if(settings.size() > 0) {

                Id settingId = settings[0].Id;
   
                if(newAccounts .size() > 0) {
                    fuseit_t4s.TrimConnector.createBulkContainers(newAccounts, settingId);
                }
            }
        }catch(Exception ex) {
            throw ex;
        }

    }


    /*****************************
    * @method  checkAccountContainers
    * @description  If the Person Account name has changed, then their TRIM Container title must also change.
    * @param  accountsList
    * @param  oldAccountMap
    ****************************/
    private static void checkAccountContainers(List<Account> accountsList, Map<Id,Account> oldAccountMap) {

        Map<Id, Map<String, String>> requestsMap = new Map<Id, Map<String, String>>();
        Set<Id> accountIds = new Set<Id>();

        for (Account a : accountsList) {
            //If Person Accounts enabled in the Org
            if(a.IsPersonAccount && String.isNotBlank(a.Trim_URI__c)) {
                if(a.FirstName != oldAccountMap.get(a.Id).FirstName || a.LastName != oldAccountMap.get(a.Id).LastName) {

                    Map<String, String> containerMap = new Map<String, String>();
                    containerMap.put('RecordTitle', a.Trim_Container_Name__c); //Formula field to create the Container Name
                    containerMap.put('Uri', a.Trim_URI__c); //Custom field to hold each Account Container URI

                    requestsMap.put(a.Id, containerMap);

                    accountIds.add(a.Id);
                }
            }
        }

        if(requestsMap.size() > 0) {
            List<fuseit_t4s__Trim__c> trims = [Select Id from fuseit_t4s__Trim__c WITH SECURITY_ENFORCED];
            if(!trims.isEmpty()) {
                fuseit_t4s.TrimConnector.modifyBulkContainers(requestsMap, trims[0].Id);
            }
        }
    }



    /*****************************
    * @method  checkAccountContainerssynchroniseAccounts
    * @description  When called, find all Account Trim Records that are scheduled to be synced into TRIM
    * and call the T4S sync method.
    ****************************/
    public static void synchroniseAccounts() {

        fuseit_t4s__Trim_Custom_Settings__c setting = fuseit_t4s__Trim_Custom_Settings__c.getInstance('Default');
        Integer count = Integer.valueOf(setting.fuseit_t4s__Max_Bulk_Sync_Count__c);

        List<fuseit_t4s__Trim_Record__c> trimRecords = [Select Id, fuseit_t4s__Logging__c, fuseit_t4s__Trim_Status__c From fuseit_t4s__Trim_Record__c Where fuseit_t4s__Trim_Status__c = 'Scheduled' And fuseit_t4s__Parent_ID__c  like '001%'  WITH SECURITY_ENFORCED limit :count];

        if(trimRecords.size() > 0) {
            for(fuseit_t4s__Trim_Record__c record : trimRecords) {

                record.fuseit_t4s__Logging__c = 'Accounts bulk sync at ' + String.valueOf(DateTime.now());
                record.fuseit_t4s__Trim_Status__c = 'Processing';
            }
    
           if (Schema.sObjectType.fuseit_t4s__Trim_Record__c.isUpdateable()){   
               update trimRecords;
           }
    
            fuseit_t4s.TrimConnector.synchroniseBulkRecords(trimRecords);
        }

    }

    /*****************************
    * @method  deleteOrphanTrimRecords
    * @description  Delete orphan Trim Records using deleted Account Ids
    * @param  accountIds
    ****************************/
    private static void deleteOrphanTrimRecords(Set<Id> accountIds) {

        if(accountIds.size() > 0) {
            List<fuseit_t4s__Trim_Record__c> records = [Select Id, fuseit_t4s__Parent_ID__c from fuseit_t4s__Trim_Record__c Where fuseit_t4s__Parent_ID__c in: accountIds WITH SECURITY_ENFORCED];

            if(records.size() > 0) {

                if (Schema.sObjectType.fuseit_t4s__Trim_Record__c.isDeletable()){   
                    delete records;
                }
            }
        }
    }

}

...