---
schema_version: "1"
id: acbc28b7345ff1b508d5fe85910e15835fd42d7a6cebf5db0355a35c5dfaadaf
title: Save the Segment to a Custom Object
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/ind/264-0-0/cgcloud_data_cloud_save_custom_object.md
canonical_url: https://help.salesforce.com/s/articleView?id=ind.cgcloud_data_cloud_save_custom_object.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: fc9b0973e6f24896314b48ef34c00733058f88472bc35867852074e051497de8
description: 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.
products:
  - Consumer Goods
release: "264"
---
# 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.

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: Lightning Experience in <strong>Enterprise</strong> and <strong>Unlimited</strong> Editions that have Consumer Goods Cloud enabled</td></tr></tbody></table>

![Note](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ind-consumer_goods_retail_admin-264-0-0-production-enus/5bb7d7f5-119d-42d0-8596-95e762837019/images/icon_note.png)

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](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ind-consumer_goods_retail_admin-264-0-0-production-enus/5bb7d7f5-119d-42d0-8596-95e762837019/images/icon_example.png)

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;
        }
    }
}
```