-1

Elaborating the Question:- We have 3 Objects, Branch, Account and Branch Accounts, Branch Accounts is a junction between former 2.

Now we are trying to insert Branch Account records via Batch, and in the After Insert of Branch Account, Account field gets updated. We have 5 Accounts and only 1 of them have validation exception. I am using Database.Upsert with AllOrNone=false, so my assumption is that only 1 Branch Account should not get added and rest 4 should get added. I am getting the same validation exception for all 5 records, even when the master Accounts are exception free.

Debug Logs:-- 07:22:51.341 (6970055831)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 07:22:51.341 (6970061564)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 07:22:51.341 (6970083031)|HEAP_ALLOCATE|[78]|Bytes:4176 07:22:51.341 (6970125212)|HEAP_ALLOCATE|[78]|Bytes:4201 07:22:51.341 (6970162659)|USER_DEBUG|[78]|ERROR|MVK ur Result in batch-->(Database.UpsertResult[getErrors=(Database.Error[getFields=();getMessage=BranchTerAfterInsert: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 5 with id 0011l00000FU074AAD; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Shipping Address is mandatory on an account, please verify that no one of the following fields is blank : Shipping Country, Shipping State, Shipping Street, Shipping City and Postal Code [ACC_VR41]: []

Class.AP38_BranchAccount.copyBranchIdsOnAccountTechFields: line 296, column 1 Trigger.BranchTerAfterInsert: line 23, column 1;getStatusCode=CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY;]);getId=null;isCreated=true;isSuccess=false;], Database.UpsertResult[getErrors=(Database.Error[getFields=();getMessage=BranchTerAfterInsert: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 5 with id 0011l00000FU074AAD; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Shipping Address is mandatory on an account, please verify that no one of the following fields is blank : Shipping Country, Shipping State, Shipping Street, Shipping City and Postal Code [ACC_VR41]: []

Class.AP38_BranchAccount.copyBranchIdsOnAccountTechFields: line 296, column 1 Trigger.BranchTerAfterInsert: line 23, column 1;getStatusCode=CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY;]);getId=null;isCreated=true;isSuccess=false;], Database.UpsertResult[getErrors=(Database.Error[getFields=();getMessage=BranchTerAfterInsert: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 5 with id 0011l00000FU074AAD; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Shipping Address is mandatory on an account, please verify that no one of the following fields is blank : Shipping Country, Shipping State, Shipping Street, Shipping City and Postal Code [ACC_VR41]: []

Class.AP38_BranchAccount.copyBranchIdsOnAccountTechFields: line 296, column 1 Trigger.BranchTerAfterInsert: line 23, column 1;getStatusCode=CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY;]);getId=null;isCreated=true;isSuccess=false;], Database.UpsertResult[getErrors=(Database.Error[getFields=();getMessage=BranchTerAfterInsert: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 5 with id 0011l00000FU074AAD; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Shipping Address is mandatory on an account, please verify that no one of the following fields is blank : Shipping Country, Shipping State, Shipping Street, Shipping City and Postal Code [ACC_VR41]: []

Class.AP38_BranchAccount.copyBranchIdsOnAccountTechFields: line 296, column 1 Trigger.BranchTerAfterInsert: line 23, column 1;getStatusCode=CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY;]);getId=null;isCreated=true;isSuccess=false;], Database.UpsertResult[getErrors=(Database.Error[getFields=();getMessage=BranchTerAfterInsert: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 5 with id 0011l00000FU074AAD; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Shipping Address is mandatory on an account, please verify that no one of the following fields is blank : Shipping Country, Shipping State, Shipping Street, Shipping City and Postal Code [ACC_VR41]: []

Class.AP38_BranchAccount.copyBranchIdsOnAccountTechFields: line 296, column 1 Trigger.BranchTerAfterInsert: line 23, column 1;getStatusCode=CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY;]);getId=null;isCreated=true;isSuccess=false;], Database.UpsertResult[getErrors=(Database.Error[getFields=();getMessage=BranchTerAfterInsert: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 5 with id 0011l00000FU074AAD; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Shipping Address is mandatory on an account, please verify that no one of the following fields is blank : Shipping Country, Shipping State, Shipping Street, Shipping City and Postal Code [ACC_VR41]: []

Class.AP38_BranchAccount.copyBranchIdsOnAccountTechFields: line 296, colu

I want to add just those accounts which doesn't have any validation exceptions.

And in my list of 5 Accounts, there is only 1 Account having custom validation Exception.

I am using Database.Upsert with AllOrNone=false. However all the accounts are being errored out.

Here is my code:-

global override void upsertObjects(List<sObject> tobeProcessedList){
    try{
        if(!tobeProcessedList.isEmpty() && Utils_Methods_Batch.checkDMLGovernorLimits(batchName, tobeProcessedList.size(), basicLog.Id)){

            nbProcessingRecord += tobeProcessedList.size();

            Schema.SObjectType sObjectType = tobeProcessedList[0].getSObjectType();
            Schema.sObjectField field;

            // Check if the sObject List in attribute contains the sObjectType "ACT_AccTerritory__c"
            if(sObjectType == BRT_BraTerritory__c.sObjectType){

                // Create a copy of the List in attribute with a concrete sObjectType
                List<BRT_BraTerritory__c> upsertList = tobeProcessedList;

                // Assign the externalIdField that will be used in the Upsert process
                field = BRT_BraTerritory__c.Fields.TECH_ExternalId__c;

                // Upsert the List in attribute by using the externalIdField
                handleUpsertResult(Database.upsert(upsertList, field, false));

            }
        }
    }

public void handleUpsertResult(Database.UpsertResult[] urList){
    System.debug(LoggingLevel.ERROR,'Starting MVK handleUpsertResult urList--->'+urList);
    // Iterate through each returned result to check 
    for (Database.UpsertResult ur : urList) {
        System.debug(LoggingLevel.ERROR,'MVK handleUpsertResult urList--->'+urList);
        if (ur.isSuccess()) {
            nbSuccess++;
            System.debug(LoggingLevel.ERROR,'MVK handleUpsertResult nbSuccess--->'+nbSuccess);
        }else{
            // Operation failed, so get all errors
            System.debug(LoggingLevel.ERROR,'MVK handleUpsertResult nbErrors--->'+nbErrors);
            for(Database.Error err : ur.getErrors()) {
                // Increment the error counter
                nbErrors++;
                System.debug(LoggingLevel.ERROR,'MVK handleUpsertResult nbErrors--->'+nbErrors);
                // Add the error into the Log object
                LOG_Log__c detailledLog = new LOG_Log__c(RecordTypeId = Utils_Methods_Batch.getRecordTypeLogDetailledLog(),
                                                         Log__c = basicLog.Id,
                                                         Error_Type__c = 4,
                                                         Source__c = batchName,
                                                         Details__c = err.getMessage());
                if(ur.getId() == null){
                    detailledLog.Related_Object_Ids__c = Utils_Methods_Batch.getStringFromFieldList(err.getFields());
                }else{
                    detailledLog.Related_Object_Ids__c = ur.getId();
                }
                detailledLogs.add(detailledLog);
               System.debug(LoggingLevel.ERROR,'MVK handleUpsertResult detailledLogs--->'+detailledLogs);
            }
        }
    }
}
  • Errored out how? – Adrian Larson Apr 30 at 13:36
  • Every DML is giving the same custom validation exception. – mvk1991 Apr 30 at 13:37
  • The Account is getting updated somewhere else, with just this Database.insert(accList) here there is no mention of AllOrNone.. but this on After Insert of my original object Branch transaction, kind of an internal dml on Account. So I suppose while returning to main DML it should return me Success and Failure records alright. – mvk1991 Apr 30 at 13:41
  • Still unable to understand what problem you are facing. Can you also put the debug logs here or data getting processed / validation you are referring? – Nishant Singh Panwar Apr 30 at 13:44
  • 4
    If I had to guess, your trigger is not written correctly. Everything here looks okay. – sfdcfox Apr 30 at 13:51
0

I got the issue.

Internally Accounts were getting updated without the Database.update, So I commented that and added the line.

Database.update(accToUpdateList,false); //update accToUpdateList;

And it ran as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.