1

I am receiving this error on Update DML inside while loop

I have marked the error line in code

Code

public class LeadFuture_TradingAccUpdate {
    @future(callout=true)
    public static void updateTradingAccounts(Set<Id> ids) {
        System.debug('called');
        List<Lead> leadList = [Select id,ConvertedAccountId,Owner_Name__c,Owner_Email__c,ConvertedContactId,OwnerId,IsConverted from Lead where ID IN:ids];
        
        Map<Id,Lead> mapLead = new Map<Id,Lead>();
        if(leadList.size()>0){
            for(Lead lea : leadList){
                if(lea.IsConverted == true){
                    mapLead.put(lea.Id,lea);
                }
            }
        }
        
        List<MT4_Accounts__c> mt4List = [Select id,Lead__c,Account__c,Contact__c from MT4_Accounts__c  where Lead__c IN:ids];
        Set<Id> mtIds = new Set<Id>();
        for(MT4_Accounts__c mt4 : mt4List){
            mtIds.add(mt4.Id);
        }
        
        List<Transaction_DB__c> transactionDbList = [Select id,Trading_Account_Login__c from Transaction_DB__c where Trading_Account_Login__c IN: mtIds];
        Map<Id,List<Transaction_DB__c>> tbdMap = new Map<Id,List<Transaction_DB__c>>();
        if(transactionDbList != null){
            for(Transaction_DB__c sales : transactionDbList){
                List<Transaction_DB__c> l = tbdMap.get(sales.Trading_Account_Login__c);
                
                if (l == null) {
                    l = new List<Transaction_DB__c>();
                    tbdMap.put(sales.Trading_Account_Login__c, l);
                }
                l.add(sales);
            }
        }
        
        List<Mt4_Accounts__c> updatemt4List = new List<Mt4_Accounts__c>();
        List<Transaction_DB__c> updatetbdList = new List<Transaction_DB__c>();
        
        System.debug('mt4List+'+mt4List.size());
        for(Mt4_Accounts__c mt : mt4List){
            if(mt.Lead__c != null && mt.Account__c == Label.Account && mt.Contact__c == Label.Contact){
                if(mapLead.size()>0){
                    Lead lea = mapLead.get(mt.Lead__c);
                    if(lea != null){
                        Mt4_Accounts__c addMt = new Mt4_Accounts__c();
                        if(lea.ConvertedAccountId != null){
                            addMt.Account__c = lea.ConvertedAccountId;
                        }
                        
                        if(lea.ConvertedContactId != null){
                            addMt.Contact__c = lea.ConvertedContactId;
                        }
                        
                        addMt.Id = mt.Id;
                        
                        if(lea.OwnerId != null){
                            addMt.Change_Trading_Account_Owner__c = lea.OwnerId;
                        }
                        
                        addMt.Lead__c = null;
                        
                        updatemt4List.add(addMt);
                        
                        if(tbdMap != null){
                            List<Transaction_DB__c> updlist = tbdMap.get(mt.Id);
                            if(updlist != null){
                                for(Transaction_DB__c tbd : updlist){
                                    Transaction_DB__c tbUpdae = new Transaction_DB__c();
                                    tbUpdae.Id = tbd.Id;
                                    if(lea.OwnerId != null){
                                        tbUpdae.Trading_Acount_owner_email__c = lea.Owner_Email__c ;
                                        tbUpdae.Trading_Account_Owner_Name__c = lea.Owner_Name__c ;
                                        tbUpdae.Trading_Account_Owner_ID__c  = lea.OwnerId ;
                                        
                                        updatetbdList.add(tbUpdae);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
        System.debug('updatemt4List+'+updatemt4List.size());
        
        if(updatemt4List.size()>0){
            Boolean mt4Success = false;
            integer count = 0;
            integer maxTries = 3;
               
            final Integer SLEEP_MSTime = 500;
            while(count<3) {
                try{
                    System.debug('test');
                    Database.SaveResult[] results = Database.update(updatemt4List,false);    //receivg DML error on this line
                    
                    for(Integer index = 0, size = results.size(); index < size; index++) {
                        if(results[index].isSuccess()) {
                            mt4Success = true;
                            System.debug(results[index].id +' was updated');
                            count=3;
                            
                                                   } 
                    }
                    if(mt4Success == true) {
                         if(updatetbdList.size()>0){
                                Database.SaveResult[] results2 = Database.update(updatetbdList,false);
                            }

                    }
                } catch(Exception e){
                    if (!e.getMessage().contains('UNABLE_TO_LOCK_ROW')){
                        count=3;
                        throw e;
                    } else {
                        if (++count == maxTries){
                            throw e;
                        } else {
                            sleep(SLEEP_MSTime);
                        }
                    }
              
                    System.debug('Error Message:'+e.getMessage()+'- Cause:'+e.getCause()+ ' -LineNumber -'+e.getLineNumber()+'- StackTraceString-'+
                             e.getStackTraceString()+' - TypeName-'+e.getTypeName()); 
                }
            }
        }
        
    }
    
    public static void sleep(Integer ms) {

        Long start = System.currentTimeMillis();
        while (System.currentTimeMillis() < start + ms) {
        }
    }
}

Anyone please help me, how I can solve this?

Thanks in Advance!!

0

1 Answer 1

1

If all records have error, it will not be catched, because you use Database.update(updatemt4List, false). It will just try to update until the limit is reached. You should consider this case separately.
You Also you set mt4Success=true if one record succeeded (from 100 for example), is it okey?

1
  • I don't think the false is the problem as such - but you're right in that if no records suceed (so all of them in the case of using false or any if OP used true) then it'll never increment count and loop indefinitely.
    – Matt Lacey
    Commented Jan 24, 2023 at 10:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .