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
SFDC 18SFDC 18 

Need to update a field with sum : error

I need to update a field on zone called total sales which display the total sales on agent object in that region

 
public class TotalSales {
    
     public TotalSales(){
         
         for(zone__c z : [SELECT Id,Total_Sales__C FROM Zone__C WHERE Name__c = 'North']){
             
             z.Total_Sales__C = [SELECT Sum() FROM Agent__C WHERE Total_Sales__c ='0' ];
            
            upsert z;

}
     }
}


 
Best Answer chosen by SFDC 18
Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
Hi

You need to write VF page to run Apex Class. or execute in anonymous window or you can schedule the Apex.

A developer can run the code in anonymous window and if you schedule users cant see the results immediatelly.

In your case i suggest you to write VF page with extension, where user can see the results immediatelly.

Regards,
Bala.
 

All Answers

arpit vijayvergiyaarpit vijayvergiya
Hi SFDC!8

You are running soql and also performing DML inside the for loop. That is not a good habit. Can you please post the screenshot of the error you are getting.

Thanks,
Arpit vijayvergiya
 
SFDC 18SFDC 18
Hi Arpit, please find the errors screenshot
Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
Hi Sai,
you can use sum of a pirticular field. for example Sum(Amount) or Sum(Total_Sales__c). I little understood your requirement. You can mention Count() insted of sum. and importantly when ever you use dml in loop, beware of governer limits.

Ragards,
Bala
SFDC 18SFDC 18
Hi Bala, I got this error when I tried to compile with Sum(Total_Sales__C): Invalid bind expression type of String for column of type Decimal
Ishita sfdcIshita sfdc
1. You can sum a particular field by using SOQL Aggregate Functions - SUM(<Field To Be Summed>). The datatype of the field to be summed must be Numeric.
2. Never use DML queries and DML updates inside a for loop. Instead, you can use Map for getting Total Sales related to a particular zone and then update List of Zone records. Please refer the link below for salesforce best practices -
'https://developer.salesforce.com/page/Apex_Code_Best_Practices'
3. If "Total_Sales__c" is an Integer value, there is no need to put it in single quote
Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
Try this
list<AggregateResult> TS = [SELECT Sum(Total_Sales__c)aver  FROM Agent__C WHERE Total_Sales__c = 0 ];
    for (AggregateResult ts1 : TS){	
    Integer TotalSales1 = Integer.ValueOf(ts1.get('aver'));
    }

 
SFDC 18SFDC 18
Ok. Tanks Bala

I'll try this. I have a doubt regarding the execution part. Do we need to execute the code in the anonymous window everytime, like in schedule apex class it upates for every 1 hr. Can you help me out with this.

I'm new to salesforce, currently i work on sap 
Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
Hi

You need to write VF page to run Apex Class. or execute in anonymous window or you can schedule the Apex.

A developer can run the code in anonymous window and if you schedule users cant see the results immediatelly.

In your case i suggest you to write VF page with extension, where user can see the results immediatelly.

Regards,
Bala.
 
This was selected as the best answer
SFDC 18SFDC 18
OK. Thank You Regards, Poojitha