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
Rahul123Rahul123 

How to convert an AggregateResult to a String

Hi All,

 

    I have a requirement where in I need to convert an AggregateResult to a String in Apex class, so that I can display it in the VisualForce page.  Could any one please suggest a solution for this problem.

 

 

 

 

Regards

 

Rahul

Ankit AroraAnkit Arora

Hi Rahul,

 

Here is code which hopefully will resolve your problem :

 

 

List<AggregateResult> AG = [select name N from account group by name] ;
String NameString = '' ;
for(AggregateResult A : ag)
{
    if(NameString == '')
         NameString += A.get('N') ;
   else
         NameString += ' , ' + A.get('N') ;
}
System.Debug('NameString :::::::::::::::: ' + NameString) ;

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

arun kumar.ax887arun kumar.ax887

Hi Ankit,

 

    Thanks for sending the code.  The Code which you have send is getting complied successfully in Apex class, but when I am trying to display the Plot_Area field Venture object on the Visualforce page I am getting the following error message.

 

 

Attempt to de-reference a null object

 

 

An unexpected error has occurred. Your development organization has been notified.     

 

 

I am posting the code, could you please look into it and provide a better solution.

 

 

 

 

List<AggregateResult> AG = [select sum(plot_area__c) N from plot__c where plot_status__c = 'sold'];

String NameString = '' ;
for(AggregateResult A : ag)
{
    if(NameString == '')
         NameString += A.get('N') ;
   else
         NameString += ' , ' + A.get('N') ;
}
System.Debug('NameString :::::::::::::::: ' + NameString) ;
v.Plot_Area__c=NameString;
update v;

 

 

 

Actually I have a requirement, where in I need to calculate the sum of the Plot_Area field from the Plots Object and convert it to text in Plot_area field of Venture Object.  Could you please send the solution.    

 

 

Thanks and Regards

 

Rahul

 

Ankit AroraAnkit Arora

Please let me know if you are getting proper values in debug System.Debug('NameString :::::::::::::::: ' + NameString) ; ?

Also error occurs where in code , when you update the object? If error occur where you updating the object then there must be some problem within the creation of instance of object.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

goabhigogoabhigo

Dude post your questions under appropraite boards(atleast in future).

Your post should be under Apex Code Development