Spara segmentet till ett eget objekt
Spara segmentresultaten till ett eget CG-objekt. Använd en Apex klass för att ansluta till en API för att hämta segmentinformation och skapa eller uppdatera relaterade poster.
Versioner som krävs
Innan du sparar segmentet till ett eget objekt, se till att segmentet har en framgångsstatus och att det finns minst en aktivering tillgänglig för segmentet.
| Tillgängliga i: Lightning Experience i Enterprise och Unlimited Editions som har Consumer Goods Cloud aktiverat |
Exempel Här är ett exempel på en Apex klass SegmentApiForAccount som innehåller metoden getSegmentFromConnectAPI. Denna metod gör en GET-begäran till en segment-API-slutpunkt, hämtar data och bearbetar dem för att uppdatera kontouppsättningsposter.
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;
}
}
}Löste denna artikel ditt problem?
Berätta för oss vad vi kan förbättra!

