自動建立自訂活動記錄
自動化容器記錄的建立和維護,以在使用者建立、更新或刪除自訂活動記錄時,讓記錄保持同步且在活動時程表上可見。雖然您可以使用流程、Apex 或其他支援的自動化工具來實作此自動化,但本主題提供使用 Apex 進行實作的指示。
必要版本
| 提供版本:Lightning Experience |
| 提供版本:具有 Life Sciences Cloud、Life Sciences Cloud for Customer Engagement 附加元件授權和 Life Sciences Customer Engagement 受管理封裝的 Enterprise 和 Unlimited Edition。 |
| 所需的使用者權限 | |
|---|---|
| 自動化容器記錄建立: | 「Life Sciences 商業管理員」權限集 |
自訂範例程式碼
更新範例程式碼以符合您的需求。
- 以您自訂子系物件的 API 名稱取代 Custom_Activity__c。例如,如果您已建立自訂物件來代表會議會議,請在適用的情況下使用 Congress_Meeting__c 取代 Custom_Activity__c。
- 以容器自訂物件的 API 名稱取代 Account_Activity__c。
- 以您要在時間表中顯示的組織中現有欄位取代範例欄位 (Status__c 和 Start_Time__c)。
- 確定容器物件中有所有必要欄位,且符合驗證規則。若否,則觸發執行期間容器記錄建立可能會失敗。
建立觸發服務 Apex 類別
建立觸發服務 Apex 類別,以產生父系自訂物件的記錄,每個記錄皆對應到子系自訂物件。
- 進入「設定」,選取「開發人員主控台」。
- 按一下 檔案 | 新增 | Apex 類別 。
- 針對「名稱」,輸入 CustomActivityTriggerService。
- 針對 sObject,選取自訂活動。
-
在 CustomActivityTriggerService.aptx 檔案中,輸入下列範例程式碼。視需要自訂程式碼。
public class CustomActivityTriggerService { // INSERT: create container rows public static void onAfterInsert(List<Custom_Activity__c> newList) { List<Account_Activity__c> toInsert = new List<Account_Activity__c>(); for (Custom_Activity__c r : newList) { toInsert.add(buildActivity(r)); } insert toInsert; } // UPDATE: update existing container rows (match by Entity_Id__c + Entity_Type__c) public static void onAfterUpdate(Map<Id, Custom_Activity__c> newMap) { List<Account_Activity__c> existing = [ SELECT Id, Entity_Id__c FROM Account_Activity__c WHERE Entity_Type__c = :SObjectType.Custom_Activity__c.name AND Entity_Id__c IN :newMap.keySet() ]; List<Account_Activity__c> toUpdate = new List<Account_Activity__c>(); for (Account_Activity__c a : existing) { Custom_Activity__c r = newMap.get((Id)a.Entity_Id__c); Account_Activity__c updated = buildActivity(r); updated.Id = a.Id; // important: turns it into an update toUpdate.add(updated); } if (!toUpdate.isEmpty()) update toUpdate; } // DELETE: delete container rows public static void onAfterDelete(Set<Id> deletedIds) { List<Account_Activity__c> toDelete = [ SELECT Id FROM Account_Activity__c WHERE Entity_Type__c = :SObjectType.Custom_Activity__c.name AND Entity_Id__c IN :deletedIds ]; if (!toDelete.isEmpty()) delete toDelete; } // Helper to build the container record consistently private static Account_Activity__c buildActivity(Custom_Activity__c r) { return new Account_Activity__c( Account__c = r.Account__c, Event_Date_Time__c = System.now(), // stored in UTC internally Title__c = r.Name, // safer default than hardcoded text Subtitle__c = null, Entity_Id__c = r.Id, //ensure that the custom activity name is also added to the 'Entity Type' picklist of Account Activity Entity_Type__c = SObjectType.Custom_Activity__c.name, Entity_Subtype__c = null, Event_Data__c = JSON.serialize(new ObjectEventData(r)) ); } private class ObjectEventData { public List<FieldDTO> fields; public ObjectEventData(Custom_Activity__c record) { fields = new List<FieldDTO>(); // Please note - the fields added here should EXIST on the custom object. These are the //fields that will show up on activity timeline. //Format should be (fieldAPIName, fieldValue) -> relatedRecordName is optional fields.add(new FieldDTO('Name', record.Name)); // OPTIONAL (doc should tell users to replace with real fields in their org): // fields.add(new FieldDTO('Status__c', String.valueOf(record.get('Status__c')))); } } private class FieldDTO { public String fieldAPIName; public String fieldValue; public String relatedRecordName; public FieldDTO(String fieldAPIName, String fieldValue) { this.fieldAPIName = fieldAPIName; this.fieldValue = fieldValue; } public FieldDTO(String fieldAPIName, String fieldValue, String relatedRecordName) { this(fieldAPIName, fieldValue); this.relatedRecordName = relatedRecordName; } } } - 儲存您的變更。
建立 Apex 觸發
建立 Apex 觸發,以確保在您為自訂活動建立記錄後,系統會自動建立相關的父系自訂活動記錄。
- 進入「設定」,選取「開發人員主控台」。
- 按一下 檔案 | 新增 | Apex 觸發 。
- 針對「名稱」,輸入 CustomActivityTrigger。
-
在 CustomActivityTrigger.aptx 檔案中,輸入下列範例程式碼並視需要進行自訂。
trigger CustomActivityTrigger on Custom_Activity__c (after insert, after update, after delete) { if (Trigger.isAfter && Trigger.isInsert) { CustomActivityTriggerService.onAfterInsert(Trigger.new); } if (Trigger.isAfter && Trigger.isUpdate) { CustomActivityTriggerService.onAfterUpdate(Trigger.newMap); } if (Trigger.isAfter && Trigger.isDelete) { CustomActivityTriggerService.onAfterDelete(Trigger.oldMap.keySet()); } } - 儲存您的變更。
- 範例 Apex 實作會為新來源活動事件建立容器記錄,但不會管理現有記錄的更新或刪除。若要支援這些情況,請擴充邏輯以符合您的業務需求。
- 此實作使用 Entity_Type__c 和 Entity_Id__c 欄位,在每個活動和容器記錄之間維持 1:1 關係。
此文章是否解決您的問題?
請讓我們知道,以便我們改進!
