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
Afroz Khan 8Afroz 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'); 
    }
}
mukesh guptamukesh gupta
Hi Afroz,

you don't need to add limit 
String query='Select id, Customer_segment__c ,(select accountid,amount,Stagename,Createddate from Opportunities) from Account Limit 500';

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
Afroz Khan 8Afroz Khan 8

Hi Mukesh,
Thank you for the response.
but still getting the same error

User-added image

Shiraz HodaShiraz Hoda

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.

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)
    {
		List<Account> lstAccounttoUpdate = new List<Account>();
         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';
        }
        lstAccounttoUpdate.add(acc);
    }
	if(!lstAccounttoUpdate.isEmpty()){
		update lstAccounttoUpdate;
	}
    }
    global void finish(Database.BatchableContext BC){
        system.debug('finish'); 
    }
}
 

Regards,
Shiraz Hoda

Afroz Khan 8Afroz Khan 8

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

Shiraz HodaShiraz Hoda
Hi Afroz,

Please find the required code below: Give it a thumsup if you are happy with solution.
global class batchclass implements Database.Batchable<sObject>,Database.Stateful{
	
    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)
    {
		Map<Id,AggregateResult> results = new Map<Id,AggregateResult>([SELECT Accountid,SUM(Amount) optyamt,Count(Id) cid 
		                                                 FROM Opportunity Where Accountid IN:lstacc Group by Accountid]);

        List<Account> lstAccounttoUpdate = new List<Account>();
        for(account acc:lstacc){
			integer countopp=0;
            Decimal sumamount=0;
			AggregateResult result = results.get(acc.Id);
			sumamount = (Decimal)result.get('optyamt');
            countopp=(integer)result.get('cid');
            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';
        }
        lstAccounttoUpdate.add(acc);
    }
	if(!lstAccounttoUpdate.isEmpty()){
		update lstAccounttoUpdate;
	}
    }
    global void finish(Database.BatchableContext BC){
        system.debug('finish'); 
    }
}

 
Shiraz HodaShiraz Hoda

Hi Afroz,

Did my answer solve yoour issue?

Regards,
Shiraz

Afroz Khan 8Afroz Khan 8

Hii Shiraz,

Sorry mate, It is only updating the (customer segment)field value as 'Low'

Regards,
Afroz

Shiraz HodaShiraz Hoda
Hi 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