您位於此處:
將區段儲存至自訂物件
將區段結果儲存至 CG 自訂物件。使用 Apex 類別連線至 API 以 ⁇ 取區段資訊,並建立或更新相關記錄。
必要版本
在您將區段儲存至自訂物件之前,請確定該區段具有成功狀態,且區段至少有一個可用的啟用。
| 提供版本:已啟用 Consumer Goods Cloud 的 Lightning Experience Enterprise 和 Unlimited Edition |
範例 以下是 Apex 類別 SegmentApiForAccount 的範例,其中包含 getSegmentFromConnectAPI 方法。此方法會向區段 API 端點提出 GET 要求、 ⁇ 取資料並處理資料以更新帳戶集記錄。
public class SegmentApiForAccount {
public static void getSegmentFromConnectAPI(){
String segmentApiName = 'SegmentApiName'; // Segment API Name with which the object will be created in Account Set Account
String instance = System.URL.getOrgDomainUrl().toExternalForm();
// Creating and configuring the http request
HttpRequest req = new HttpRequest();
req.setEndpoint(instance + '/services/data/v58.0/ssot/segments/' + segmentApiName + '/members');
req.setMethod('GET');
req.setTimeout(10000); // 100s 120 is the max
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
Http http = new Http();
// Sending the request
HTTPResponse res = http.send(req);
Set<String> accountIds = new Set<String>();
// Checking if the response is successful or not.
if (res.getStatusCode() == 200) {
Map<String, Object> resultMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
List<Object> dataList = (List<Object>)resultMap.get('data');
Integer len = dataList.size();
// Getting the accountIds.
for (Integer i=0; i<len; i++) {
Map<String, Object> dataMp = (Map<String, Object>)dataList[i];
accountIds.add((String)dataMp.get('id'));
}
}
// If the accountIds are not empty
if (!accountIds.isEmpty()) {
// Getting existing Account Set with Segment API Name
List<cgcloud__Account_Set__c> listAccountSet = [SELECT Id FROM cgcloud__Account_Set__c WHERE cgcloud__Description_Language_1__c = :segmentApiName];
if(listAccountSet.isEmpty()) {
// If not present, create a new one
cgcloud__Account_Set__c accountSet = new cgcloud__Account_Set__c(cgcloud__Description_Language_1__c=segmentApiName, cgcloud__Sales_Org__c='0001');
insert accountSet;
listAccountSet.add(accountSet);
}
// Getting Existing Account Set Account with Account Set Id.
List<cgcloud__Account_Set_Account__c> accountSetAccountAlreadyPresent = [SELECT cgcloud__Account_Set_Account__c FROM cgcloud__Account_Set_Account__c WHERE cgcloud__Account_Set__c =:listAccountSet[0].Id];
// Getting Existing Accounts present in Account Set.
Set<String> accountSetAccountIds = new Set<String>();
if (!accountSetAccountAlreadyPresent.isEmpty()) {
for (cgcloud__Account_Set_Account__c accountSetAccount : accountSetAccountAlreadyPresent) {
accountSetAccountIds.add(accountSetAccount.cgcloud__Account_Set_Account__c);
}
}
// Creating new Account Set Account for new records
List<cgcloud__Account_Set_Account__c> listAccountSetAccount = new List<cgcloud__Account_Set_Account__c>();
for(String accountId : accountIds) {
if (!accountSetAccountIds.contains(accountId)) {
listAccountSetAccount.add(
new cgcloud__Account_Set_Account__c(
cgcloud__Account_Set__c = listAccountSet[0].Id,
cgcloud__Account_Set_Account__c = accountId
)
);
}
}
insert listAccountSetAccount;
// Performing deletion of the Account Set Account which are not longer needed.
List<cgcloud__Account_Set_Account__c> accountSetAccountNeedsToBeDeleted = new List<cgcloud__Account_Set_Account__c>();
for (cgcloud__Account_Set_Account__c accountSetAccountAP : accountSetAccountAlreadyPresent) {
String accountSetAccountId = accountSetAccountAP.cgcloud__Account_Set_Account__c;
if (!accountIds.contains(accountSetAccountId)) {
accountSetAccountNeedsToBeDeleted.add(accountSetAccountAP);
}
}
delete accountSetAccountNeedsToBeDeleted;
}
}
}此文章是否解決您的問題?
請讓我們知道,以便我們改進!

