+ Start a Discussion
SF Beginner 2019SF Beginner 2019 

DML Error Problem

I am experiencing DML Error 10001 ,

can you spot on which is needed to be optimize more in here.


public with sharing class UpdateStatus {
    public static final String ClsName  = 'UpdateStatus';
    public static boolean isExecuting=false;
    public static boolean isInvoked = false;
    public static boolean isCnclGSChkd = false;
    public static final String UpdateActiveStatus  = 'ActiveStatus' ;
    public static final String UpdateCancelStatus  = 'CancelStatus' ;
    public static final String UpdateRecType = 'RecTypeStat' ;
    public static final String UpdateCancelBff = 'CancelStats';
    public static final String  UpdateEndDate = 'UpdateEndDate';
    public static final string acctClient =Schema.SObjectType.Account.getRecordTypeInfosByName().get('Account - Client').getRecordTypeId();
    public static final string acctClientInt =  Schema.SObjectType.Account.getRecordTypeInfosByName().get('Account - InactiveClient').getRecordTypeId();
    public static final String  actv='Active';
    public static final String  cancl='Canceled';
    public static final String pendng =  'Pending';
    public static final String  accntCst= 'Account_Client';
    public static final String  cncled= 'Canceled';
    public static final String  inactv= 'Inactive';
    public static final Date enddateFuture = Date.newInstance(2100, 12, 31);
    public static  List<Id> cnclgmList = new List<Id>();
    public static final string profIntUser='Integration User';
    public static final string categNet='Verizon';
    

    public static void ActiveStatus(List<Group_Members> gmList)
    {

            Date EndDateFr;
            map<id,integer> prcOpn=new map<id,integer>();
            list<id> gpToProc=new list<id>();
            String profileName = [Select Name from Profile where Id = :UserInfo.getProfileId() limit 1].Name;
            for(Group_Members objGM : gmList){
                
                if(objGM.Status__c==pendng ){
                    gpToProc.add(objGM.Id);
                }
            }
            
            list<aggregateResult> PrcssLst=[select GM_ID,count(Id) OpenGM from WF_GM__c where GM_ID in:gpToProc 
                                              and (GA_Status__c='Closed') and GM_NS__c=null and GM_Sid__c !=null  group by GM_ID  limit 2000];
            for(aggregateResult agRst:PrcssLst)
            {
                prcOpn.put(string.valueof(agRst.get('GM_ID')),integer.valueof(agRst.get('OpenGM')));   
            }
            
            for(Group_Members objGM : gmList){
                if(objGM.EffDate__c<=system.today() && 
                   objGM.cancelDate__c==null){
                       EndDateFr = enddateFuture;
                   }
                else{
                    EndDateFr = objGM.cancelDate__c;
                }
                if(objGM.Status__c==pendng  && objGM.Network__c==categNet){
                    if(prcOpn.get(objGM.Id)>0)
                        objGM.Status__c=actv;
                }
                if(objGM.Status__c==pendng  && (objGM.Trans__c || objGM.Post__c)){
                    if(prcOpn.get(objGM.Id)>0)
                        objGM.Status__c=actv;
                }  
                if(profileName==profIntUser){
                    if(objGM.Status__c==cncled && EndDateFr>system.today() 
                       && objGM.EffDate__c<=system.today())
                    {
                        objGM.Status__c=actv;
                    }   
                }  
            }

    }

    public static void RecTypeStat(List<Group_Members> gmList)
    {
            set<id> accIds=new set<id>();
            Date gmEndDateFr;
            boolean isChnge=false;
            String profileName = [Select Name from Profile where Id = :UserInfo.getProfileId() limit 1].Name;
            Map<Id, Date> gmACCMap = new Map<Id, Date>();
            for(Group_Members objGM : gmList){
                gmACCMap.put(objGM.Account_c,objGM.EffDate__c);
                if(objGM.Status__c==actv && objGM.Network__c==categNet)
                {
                    accIds.add(objGM.Account_c);
                }
                if(objGM.Status__c==actv && (objGM.Trans__c || objGM.Post__c))
                {
                    accIds.add(objGM.Account_c);
                }
                
                if(profileName==profIntUser){
                    if(objGM.Status__c==cncled && gmEndDateFr>system.today() 
                       && objGM.EffDate__c<=system.today())
                    {
                        accIds.add(objGM.Account_c);
                    }
                }
                
            } 
            List<Account>  accLst =[Select id,RecordType.DeveloperName from Account WHERE ID IN :accIds Limit 50000] ;
            List<Account>  updtAccLst = New   List<Account>();
            
            if(!accIds.isEmpty()){
                for(Account objAcc : accLst){
                    if(objAcc.RecordType.DeveloperName != accntCst){
                        objAcc.RecordTypeId = acctClient;
                        objAcc.Account_Status__c=actv;
                        objAcc.CovCancelDate__c=null;
                        objAcc.EnrollDate__c = gmACCMap.get(objAcc.Id);
                        updtAccLst.add(objAcc);
                    }
                }
            }  
            
            if(!updtAccLst.isEmpty()){
                Database.update(updtAccLst);
            }
            isInvoked= true; 
        }
        
    }

    public static void CancelStats(List<Group_Members> grpmemList,map<id,Group_Members> grpmemListOld)
    {
            for(Group_Members grp:grpmemList)
            {
                boolean CnclChk=false;
                boolean CnclDteChk=false;
                if(grpmemListOld!=null)
                {
                    Group_Members grpOld=grpmemListOld.get(grp.Id);  
                    if(grp.cancelDate__c <> grpOld.cancelDate__c)
                        CnclChk=true;
                    if((grp.Status__c <> grpOld.Status__c) && grp.Status__c==cancl)
                    {
                        CnclDteChk=true;
                    }
                }
                else
                {
                    CnclChk=true;
                    if(grp.Status__c==cancl)
                    {
                        CnclDteChk=true; 
                    }
                }
                
                if(grp.cancelDate__c<=system.today() && CnclChk)
                {
                    grp.Status__c=cancl;
                    cnclgmList.add(grp.Id);
                    
                }
            }
            isCnclGSChkd = true;
        }
        
    }

    public static void CancelStatus(List<Group_Members> grpmemList,Map<ID,Group_Members> grpOldMap)
    {
            set<id> cnclAccIdsToPrcss=new set<id>();
            boolean isChnge=false;
            for(Group_Members grp:grpmemList)
            {
                if(grpOldMap !=null)
                {
                    Group_Members grpOld=grpOldMap.get(grp.Id);
                    if(grpOld.cancelDate__c !=grp.cancelDate__c  
                       || grpOld.Status__c !=grp.Status__c){
                           isChnge=true;     
                       }
                }
                
                else
                {
                    isChnge=true;
                }
                if(grp.cancelDate__c<=system.today() && isChnge)
                {
                    cnclAccIdsToPrcss.add(grp.Account_c);
                }
            }

            if(cnclAccIdsToPrcss.size() > 0){
                
                List<Account> accLst = [Select Id,Account_Status__c,RecordType.DeveloperName ,CovCancelDate__c,
                                        (select Status__c,cancelDate__c from Group_Member__r ORDER BY LastModifiedDate ASC)
                                        from Account where id in:cnclAccIdsToPrcss Limit 50000];
                List<Account>  acntIdsToPrcs = new List<Account>();
                List<Account>  finalacntIdsToPrcs = new List<Account>();
                Set<Id> processAccIds = New Set<Id>();
                boolean flag = false;
                Set<Id> accActv = New Set<Id> ();
                
                for(Account objAcc :accLst){
                    
                    if(!objAcc.Group_Member__r.isEmpty()){
                        for(Group_Members objGM : objAcc.Group_Member__r){
                            if(objGM.Status__c.equals(actv)){
                                flag = true;   
                                accActv.add(objAcc.id);
                                break;
                            }
                            else{
                                if(cnclgmList.contains(objGM.Id) && !processAccIds.contains(objAcc.id)  ){
                                    objAcc.CovCancelDate__c=objGM.cancelDate__c;
                                    objAcc.RecordTypeId =acctClientInt;
                                    objAcc.Account_Status__c=inactv;
                                    objAcc.EnrollDate__c=null;
                                    acntIdsToPrcs.add(objAcc); 
                                    processAccIds.add(objAcc.id);
                                }
                            }
                        }
                    }
                } 

                if(accActv.size() > 0){
                    processAccIds.removeAll(accActv);
                }

                for(Account accCus : acntIdsToPrcs){
                    if(processAccIds.contains(accCus.id)){
                        finalacntIdsToPrcs.add(accCus);
                    }
                }
                
                if(finalacntIdsToPrcs.size()>0 ){
                    
                    Database.update(acntIdsToPrcs);
                }
                
            }
            
            isExecuting=true;
      
    }
  
    public static void UpdateEndDate(List<Group_Members> grpmemList,map<id,Group_Members> grpmemListOld){
            List<id> gsIdLst = new List<id>();
            List<History__c> TCList = new List<History__c>(); 
            for(Group_Members grp:grpmemList)
            {
                if(grpmemListOld!=null)
                {
                    Group_Members grpOld=grpmemListOld.get(grp.Id);
                    
                    if(grp.History__c <> grpOld.History__c){
                        gsIdLst.add(grpOld.History__c);
                        
                    }
                } 
            }
            if(!gsIdLst.isEmpty()){
                for(History__c tp : [select id,EndDateTC__c,GA_TPB_Start_Date__c from History__c
                                                           where ID IN : gsIdLst Limit 50000]){
                                                               tp.EndDateTC__c = system.today()-1;
                                                               TCList.add(tp);                                                      
                                                           }
                Database.update(TCList);
            }
        }   
        
    }
}

 
@DEVS@DEVS
It seems like you are picking up 50000 records and updating all of them.
SF DML governor limits allows only 1000 record to be processed in a single transacttion.

Invoke a Future Method/ an APEX batch to process more records.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
Total number of records processed as a result of DML statements, Approval.process, or database.emptyRecycleBin10,000
SF Beginner 2019SF Beginner 2019
@DEVS, where should I invoke the @future, or can this still be optimized?