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
Sangeeth R 5Sangeeth R 5 

the object 'A' consists of a custom field 'Quantity' ,the average should be calculated for the 'Quantity' fields of all the records, that average should be updated in another Object's field which is not related to Object 'A'. Is this requirement possible?

Deepak Kumar SharmaDeepak Kumar Sharma
Hi Sangeeth, 
Yes, you can use aggregate function avg() for that like:-
AggregateResult result= [select avg(Quantity) avgq from Object A];
Double avgquantity = result.get('avgq ');
then finally you can use update dml operation for storing that value to another object's field.
If it really helped you please select this as best answere.
Thanks & Regards,
Deepak
Sangeeth R 5Sangeeth R 5
@Deepak Kumar Sharma, Can i use this Aggregate function in Batch apex?
Deepak Kumar SharmaDeepak Kumar Sharma
Hi Sangeeth,
it should work for batch apex too if you have some trouble then please share a piece of code where you get stuck and in the above code please make slight change 
AggregateResult result= [select avg(AnnualRevenue) avgq from Account];
Double avgquantity = Double.valueOf(result.get('avgq'));

If it solved your problem please mark the answere as solved or please select it as best if you could. :)
Thanks & Regards,
Deepak
 
 
Sangeeth R 5Sangeeth R 5
@Deepak Kumar Sharma

This is the Apex class

public class ProductMaintananceHelper {

    public Decimal avg;
 
    
    public void Quantity(){
            
        AggregateResult[] result= [select ProductESB__c ,avg(Quantity__c)aver from Order_Product__c group by ProductESB__c];
    
         for( AggregateResult ar : result){
           avg=(Decimal)ar.get('aver');
           
            System.debug('avg is '+avg);           
}
}
}

 
Sangeeth R 5Sangeeth R 5
And this is the batch class. The problem is it calculates average for the entire Order_Product__c and updates the field, I want to calculate average for every product separately


global class BatchapextoUpdateQuantity implements Database.Batchable<SObject>,Schedulable {
        
        global Decimal avg;
        
        global Database.querylocator start(Database.Batchablecontext bc){
           string  query='select id from Product_maintanance__c';
            return database.getQueryLocator(query);
        }
        global void execute(Database.BatchableContext BC, list<Product_maintanance__c>prolst){
            
            ProductMaintananceHelper pmh=new ProductMaintananceHelper();
      pmh.Quantity();
       list<Product_maintanance__c> pmlst= new list<Product_maintanance__c>() ;
            
        for(Product_maintanance__c pmc : prolst){
        pmc.Quantity_del__c= pmh.avg;
            pmlst.add(pmc);
        }
        if(pmlst.size()>0){
            try{
            update pmlst;
            }
            catch(Exception e){
                System.debug('Exception is '+e.getMessage());
            }
            
        }
​
        }
        
        
        global void finish(Database.Batchablecontext bc){
            
        }
        global void execute(SchedulableContext sc) {
            BatchapextoUpdateQuantity ba=new BatchapextoUpdateQuantity();
            Database.executeBatch(ba);
             //tring sch = '0 00 00 * * ?';
         //ystem.schedule ('Batch', sch, new BatchapextoUpdateQuantity());
        }
        
    }