You need to sign in to do that
Don't have an account?
Afroz Khan 8
First error : Too many DML rows:10001
Whenever trying to execute the below code I am getting the error:
global class batchclass implements Database.Batchable<sObject>,Database.Stateful{
global integer countopp;
global Decimal sumamount;
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query='Select id, Customer_segment__c ,(select accountid,amount,Stagename,Createddate from Opportunities) from Account Limit 500';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> lstacc)
{
AggregateResult[] gr= [SELECT Accountid,SUM(Amount) optyamt,Count(Id) cid FROM Opportunity Where Accountid IN:lstacc Group by Accountid];
for(AggregateResult ag:gr){
sumamount = (Decimal)ag.get('optyamt');
countopp=(integer)ag.get('cid');
}
for(account acc:lstacc){
if(sumamount<50000 && countopp>1){
acc.Customer_segment__c = 'Hot';
}
else if(sumamount>=50000 && countopp>1){
acc.Customer_segment__c = 'Medium';
}
else{
acc.Customer_segment__c = 'Low';
}
update lstacc;
}
}
global void finish(Database.BatchableContext BC){
system.debug('finish');
}
}
global class batchclass implements Database.Batchable<sObject>,Database.Stateful{
global integer countopp;
global Decimal sumamount;
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query='Select id, Customer_segment__c ,(select accountid,amount,Stagename,Createddate from Opportunities) from Account Limit 500';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> lstacc)
{
AggregateResult[] gr= [SELECT Accountid,SUM(Amount) optyamt,Count(Id) cid FROM Opportunity Where Accountid IN:lstacc Group by Accountid];
for(AggregateResult ag:gr){
sumamount = (Decimal)ag.get('optyamt');
countopp=(integer)ag.get('cid');
}
for(account acc:lstacc){
if(sumamount<50000 && countopp>1){
acc.Customer_segment__c = 'Hot';
}
else if(sumamount>=50000 && countopp>1){
acc.Customer_segment__c = 'Medium';
}
else{
acc.Customer_segment__c = 'Low';
}
update lstacc;
}
}
global void finish(Database.BatchableContext BC){
system.debug('finish');
}
}
you don't need to add limit
and What batch size are you using, means batch size should not be bigger ,it's approx 100 to 200
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Hi Mukesh,
Thank you for the response.
but still getting the same error
Hi Afroz,
There are few mistakes I see in your code.
1. You are updating the same list which you are interating on.
2. You are updating the same list in for loop.
Please find below code. This will solve your issue. Kindly mark my asnwer as best answer if this resolves the issue.
Regards,
Shiraz Hoda
Hi @shiraz,
Still not getting the desirable result
I Think the sum amount and count (Id) aren't mapped with the account's Id
Can you help me with that?
Thanks,
Afroz khan
Please find the required code below: Give it a thumsup if you are happy with solution.
Hi Afroz,
Did my answer solve yoour issue?
Regards,
Shiraz
Hii Shiraz,
Sorry mate, It is only updating the (customer segment)field value as 'Low'
Regards,
Afroz
Check for the data if you have opportunities total amount is 50000 + or not and put some debugs to get value of countopp and sumamount and see what is coming. That will lead you to get some answers.
Regards,
SHiraz