AGG_COUNT_DISTINCT

Description

Performs a dynamic aggregation on a specified Salesforce object and returns the count of unique (distinct) values in a selected field for the matching records.

Syntax

AGG_COUNT_DISTINCT(aggregate_object_name, aggregate_field_name, ...[group_field_name, group_field_value], additional_filters_optional) -> Integer

Parameters

ParameterData TypeRequiredDescription
aggregate_object_nameStringYesThe name of the object containing records to evaluate.
aggregate_field_nameStringYesThe name of the field whose distinct values will be counted.
… group_field_nameStringNoThe name of the field used to filter records.
… group_field_valueObjectNoThe field value used as filtering criteria.
additional_filters_optionalStringNoAdditional conditions to filter records.

Examples

AGG_COUNT_DISTINCT(
		"Opportunity", 
		"Type", 
		"AccountId", Id, 
		"StageName = 'Closed Won'"
)
-- Returns the count of distinct non-null Opportunity Type values grouped by AccountId, filtered by StageName = "Closed Won", where Opportunity.AccountId matches the Id from the source records.

-- At runtime, DSP generates the following SOQL query:
SELECT AccountId groupField0, COUNT_DISTINCT(Type) agg0 
FROM Opportunity 
WHERE AccountId IN ('0018K00000kPuYXQA0', '0018K00000kPuYYQA0', '0018K00000kPuYZQA0', '0018K00000kPuYaQAK', '0018K00000kPuYbQAK') AND StageName = 'Closed Won' 
GROUP BY AccountId
AGG_COUNT_DISTINCT(
		"Opportunity", 
		"Type", 
		"AccountId", Id, 
		"OwnerId", OwnerId, 
		"StageName = 'Closed Won'"
)
-- Returns the count of distinct non-null Opportunity Type values grouped by AccountId and OwnerId, filtered by StageName = "Closed Won", where Opportunity.AccountId and Opportunity.OwnerId match values from the source records.

-- At runtime, DSP generates the following SOQL query:
SELECT AccountId groupField0, OwnerId groupField1, COUNT_DISTINCT(Type) agg0 
FROM Opportunity 
WHERE (AccountId IN ('0018K00000kPuYXQA0', '0018K00000kPuYYQA0', '0018K00000kPuYZQA0', '0018K00000kPuYaQAK', '0018K00000kPuYbQAK') AND OwnerId IN ('005D2000006JY2bIAG', '0058K0000047WdvQAE', '0055e000007rpICAAY')) AND StageName = 'Closed Won'
GROUP BY AccountId, OwnerId

Related information