function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ryan Avent 14Ryan Avent 14 

unique row response from SOQL?

Using a SOQL query (no additonal post processing using apex) i am trying to return a unique list of products (Product2).

select Id, ProductCode from Product2 where ProductCode IN ('111123')

The issue is that ProductCode is not techunicly a unique field, and i only want to retrun one Product2 for every ProductCode in that list (only showing one as an example).

for this query i get two records

{
	"totalSize": 2,
	"done": true,
	"records": [
		{
			"attributes": {
				"type": "AggregateResult",
				"url": "/services/data/v55.0/sobjects/AggregateResult/01t03000005XVebAAG"
			},
			"ProductCode": "111123",
			"Id": "01t03000005XVebAAG"
		},
		{
			"attributes": {
				"type": "AggregateResult",
				"url": "/services/data/v55.0/sobjects/AggregateResult/01t03000005XVegAAG"
			},
			"ProductCode": "111123",
			"Id": "01t03000005XVegAAG"
		}
	]
}

 

I can get a unique list via post processing the reponse list using APEX but i want to preform this SOQL call via API and retrieve a uniqe list  without the need to do additional calls (as maximum response size is 2000) or via post processing. 

I require the SF id of one of the records (do not care which one), i have tried to use GROUP BY on ProductCode but am not seeing the result i exspect.

Is there anyway to achive what im looking for?

AshwiniAshwini (Salesforce Developers) 
Hi Ryan,
Have you tired using Max(Id) along with Group by ProductCode?

Please check below links which can be helpful for your scenario:
https://medium.com/@paustint/level-up-your-apex-skills-by-using-aggregate-soql-84e58e5bbde2 
https://salesforce.stackexchange.com/questions/5321/how-can-i-get-the-id-of-the-max-record 

If this information helps, please mark the answer as best. Thank you 

Important Update - what you need to do
User-added image
As a reminder, on December 4, 2023 the Salesforce Discussion Forums will be removed from the Salesforce Developers website. These forums will shut down and all relevant discussions will migrate to the Trailblazer Community digital platform.

During the week starting November 20, you can view discussions, but not post new questions or responses. On December 4, you will no longer be able to access the discussion forums from the Salesforce Developers website.

Please take these steps before November 30, 2023, 11:59 p.m. PT to ensure your contributions follow you to the Trailblazer Community:
  1. If you’re not already a member of the Trailblazer Communitysign up for a Trailblazer account using the same login email address associated with your Developer Discussion Forums account. This is crucial.
  2. If you already have a Trailblazer account, and it’s using a different email address from your Developer Discussion Forums account, we recommend that you log in to the Trailblazer Community and connect your forums email address to your Trailblazer account.
Find more info & support
We know that moving platforms can be a hassle, so we created a special forums users group, the Migration Support Hub, in the Trailblazer Community where you can get training videos, information, and assistance.

We are here to help you through this transition!

Sincerely,
The Forums Support Team