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
SfSharkSfShark 

About Aggrigate result assign to another field

hai friends,

 

   I have invoicenumber field in Opportunity object,  i want to query the max invoicenumber in that object and is assigned to another field.

 

How we can achieve this,Please help me in this scenario.

 

We can get Max value by group by a particular field.

 

like Select Name,Max(invoicenumber__c) from opportunity group by AccountName;

 

NOw i have max invoice number  for a particular account.

 

how i can assign this to another field.

 

 

Thanks in Advance

 SHARK

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Here is the way from which you can get the value from aggregate result :

 

List<AggregateResult> ag = [Select Name,Max(invoicenumber__c) invoice from opportunity group by AccountName] ;
for(AggregateResult a : ag)
{
    System.debug('Here is the value, use this as you like :::: ' + a.get('invoice')) ;
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

Ankit AroraAnkit Arora

Here is the way from which you can get the value from aggregate result :

 

List<AggregateResult> ag = [Select Name,Max(invoicenumber__c) invoice from opportunity group by AccountName] ;
for(AggregateResult a : ag)
{
    System.debug('Here is the value, use this as you like :::: ' + a.get('invoice')) ;
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
SfSharkSfShark

Thank u Ankit

 

Shark

SfSharkSfShark

Ankit, i have another doubt,

 

 How we can assign to a.get(invoice) to another integer.

 

Can you please helpme, i am getting error ,when i was trying to assign that maxvalue to another integer like,

 

Integer maxvalue =a.get(invoice);

 

please help me,

 

Thanks in Advance

Shark

Ankit AroraAnkit Arora

Sorry for bit late reply, try this :

 

List<AggregateResult> ag = [Select Name,Max(invoicenumber__c) invoice from opportunity group by AccountName] ;
String str = String.valueOf(ag[0].get('invoice')) ;
Integer I = Integer.valueOf(str) ;

Thanks

Ankit Arora

Blog | Facebook | Blog Page