Save the Segment to a Custom Object

Save the segment results to a CG custom objects. Use an Apex class to connect to an API to retrieve segment information and create or update related records.

Required Editions

Before you save the segment to a custom object, make sure that the segment has a success status and that there is at least one activation available for the segment.

Available in: Lightning Experience in Enterprise and Unlimited Editions that have Consumer Goods Cloud enabled
Note
Note If your Apex code exceeds the governor limits, a runtime exception is thrown. These limits are enforced for each Apex transaction. To manage large datasets, consider running a batch job.
Example
Example Here is an example of an Apex class SegmentApiForAccount that contains the getSegmentFromConnectAPI method. This method makes a GET request to a segment API endpoint, retrieves data, and processes it to update account set records.
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;
        }
    }
}