When running SOQL (Salesforce Object Query Language) aggregate queries in Apex — such as those using COUNT(), SUM(), MIN(), MAX(), or AVG() — the results are returned as an AggregateResult object, not a directly typed numeric value. The AggregateResult.get('alias') method returns an untyped Object.
To use the numeric result in further Apex logic — such as displaying it in a message, comparing it to a threshold, or passing it to another method — it must first be cast to a String using String.valueOf(), and then optionally to an Integer using Integer.valueOf(). This article provides a sample code pattern for this two-step conversion.
Example use case: A developer needs to count the total number of Contact records created after a certain date and compare that count against a limit to determine whether to send an alert notification.
Use String.valueOf() and Integer.valueOf() to convert aggregate results to a string or integer format.
The following Apex code demonstrates how to:
AggregateResult object using the alias cntString, then to an Integer for use in further processingpublic list<AggregateResult> countContacts = new list<AggregateResult>();
countContacts = [Select count(Id)cnt from Contact where createddate>=2013-06-03T12:54:40.000Z ];
String str = String.valueOf(countContacts[0].get('cnt')) ;
Integer I = Integer.valueOf(str) ;
system.debug('String Value - '+str);
system.debug('Integer Value - '+I);
countContacts[0].get('cnt'): Retrieves the value aliased as cnt from the first row of the aggregate result. The alias cnt corresponds to count(Id)cnt in the SOQL query. The return type of .get() is Object.String.valueOf(countContacts[0].get('cnt')): Converts the untyped Object to a String representation of the number (e.g., "42").Integer.valueOf(str): Parses the String value into an Integer (e.g., 42) that can be used in numeric comparisons or arithmetic.AggregateResult.get() method always returns an untyped Object, and Apex does not allow direct casting from Object to Integer without going through a String intermediary.Integer or String parameter000386033

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.