Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

T4S will not create the TRIM Containers for each configured T4S Salesforce sObject or custom object, as there are too many different types.

When you set up the T4S Settings for a sObject, you can select the “Enable Sub Container“ checkbox, and then use this boolean value within your code to decide if a new Container should be created or not.

The “Trim sObject Container Uri“ field tells T4S where the new Container should be created.
This can be overridden by adding the specification property “RecordContainer“ and value, with the Container Specification Property Set.
This again can be overridden if you have requirements to create separate Container Specification Property Sets for each Salesforce Record Type for the configured sObject.

Example code:

Create a trigger for the sObject that you would like a Container created for on each insert or update.

/******************************
* Class : AccountTrigger
* Created By : FuseIT
-----------------------------------------------------------------------------------
* @description : After a new Account is created, call CreateNewAccountContainers method that 
* will create a new TRIM Server Container for the Account using T4S
*
-----------------------------------------------------------------------------------
*
****************************/
trigger AccountTrigger on Account (after insert, after update, after delete) {


    if(Trigger.isAfter && Trigger.isInsert) {
        AccountTrimHelper.onAfterInsert(Trigger.new);
    }

    if(Trigger.isAfter && Trigger.isUpdate) {
        AccountTrimHelper.onAfterUpdate(Trigger.new, Trigger.oldMap);
    }

    if(Trigger.isAfter && Trigger.isDelete) {
        AccountTrimHelper.onAfterDelete(Trigger.oldMap);
    }
}

Create a helper class to call from the trigger

/*****************************
* 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  synchroniseAccounts
    * @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;
                }
            }
        }
    }

}

  • No labels