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
Michael3.BrownMichael3.Brown 

How Can I Convert an SObject Count/Sum to an Integer?

Hello, I have an SQL statement returns the count of of the number of records in my object:

 

countrecord = [SELECT COUNT(Probability__c)totalcount FROM Pipeline_Tracker_Ver_2__c];

 

I want to convert this to an integer, and add the value 1, so that I can set this as a dynamic value for ROWSPAN on one of my tables. I figured that since I am returning a count, I should be able to easily convert this to an integer. I have been trying the formula:

 

public Integer count;
count = integer.valueof(countrecord) + 1;

 

However, I get the following error. It seems to be getting totalcount correct, which is 2, but I'm not sure why it's an invalid integer, or what they mean by external entry point.

 

System.TypeException: Invalid integer: [AggregateResult (totalcount:2)]

 

Class.MarginEscalatorsThermometerChart_Q1_2011.getCount: line 231, column 17 External entry point

 

Any insight would be great, as I'm not sure what else to try.

 

Thanks!

Mike

Best Answer chosen by Admin (Salesforce Developers) 
ForceBalaForceBala

Hi,

 

Try this,

AggregateResult ar = [SELECT COUNT(Probability__c)totalcount FROM Pipeline_Tracker_Ver_2__c];

 

public Integer count;
count = (integer)ar.get('totalcount');

 

 

Check  Whether it works.

 

Thanks,

BaLa

All Answers

ForceBalaForceBala

Hi,

 

Try this,

AggregateResult ar = [SELECT COUNT(Probability__c)totalcount FROM Pipeline_Tracker_Ver_2__c];

 

public Integer count;
count = (integer)ar.get('totalcount');

 

 

Check  Whether it works.

 

Thanks,

BaLa

This was selected as the best answer
kiranmutturukiranmutturu

 

what is the type of countrecord in your code....it should be of aggregate result object..?

 

 

me_helpme_help

hi kiran,

 

I am new to salesforce and have written the following trigger. But not able to write its test class.

 

can you help me with the code?? hoping for a +ve response..u can mail me at sahil.rajpal@accenture.com

 

TRIGGER

 

trigger increaseCount on Employee__c (after insert) 
{
List<Employee__c> EmployeeToUpdate = new List<Employee__c>{};
for(Employee__c emp : Trigger.new)
  {
  Company__c company =[ select Number_of_Employees__c from Company__c where Id= :emp.Company__c ];
    if (company.Number_of_Employees__c==Null)
   {
   company.Number_of_Employees__c=1;
   }
   else
   {company.Number_of_Employees__c =company.Number_of_Employees__c+1;}
   update company;
  }
}

Michael3.BrownMichael3.Brown

Thanks Bala,

 

That worked. It seems that I need to put my query into either a List or Aggregate Result.

 

I found that the following code worked as well.

 

public Integer getCountMed()
    {
               
        List<Pipeline_Tracker_Ver_2__c> totalrecords 
 = [SELECT Probability__c FROM Pipeline_Tracker_Ver_2__c WHERE Probability__c = 'Done'];            
        
        count = totalrecords.Size() + 1;
        return count;

     }