Create Containers for a Salesforce sObject

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

The most common business rules require a new Content Manager Container created after a new Salesforce record has been inserted.
i.e. A new Account is created in Salesforce, so a new Container for the Account should be created in Content Manager and mapped to the new Account.

To achieve this you will need to create or extend your own Salesforce Apex trigger so that the T4S API is called to create the Container automatically.

If automated Container creation is not required, the T4S Admin pages will allow a Salesforce Admin to create multiple Content Manager Containers from a button click.
When an sObject has been configured in the T4S Admin, an ordered list is displayed where records without a Container can be selected to have a Container created. Or a record with a mapped Container can be re-mapped to a different Container.

It is also possible to have a Content Manager Container created from a button click on a Salesforce page.

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

 

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.

 

NOTE: Within the example code below there is a method to syncrhonise Account files.
T4S installs a default scheduler that when run will transfer all waiting Salesforce Files & Attachments to Content Manager no matter which Salesforce sObject the waiting files belong to.


Automated Container Example Code:

Create a trigger or update an existing 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; } } } } }