0

I have apex AuraEnabled function which is inserting records for a junction object, however due to some validation rules, some of the records may get failed.

So I have a solution to maintain a list of failed records as below:-

try{
        Set<Id> accountIds = new Set<Id>(accIds);
        List<String> lstFailedRecords = new List<String>();
        for(Id id:accIds){
            accountIds.add(id);
        }
        for(AGC_AgrScope__c scopeAgr: [SELECT Id, Account__c, Agreement__c 
                                       FROM AGC_AgrScope__c 
                                       WHERE Agreement__c = :agreementId
                                       AND Account__c IN : accIds]) {
                                           accountIds.remove(scopeAgr.Account__c);
                                       }
        List<AGC_AgrScope__c> agrScopeList = new List<AGC_AgrScope__c>();

        for(Id accId:accountIds){
            AGC_AgrScope__c agrScope = new AGC_AgrScope__c();
            agrScope.Account__c = accId;
            agrScope.Agreement__c = agreementId;
            agrScopeList.add(agrScope);
        }
        //Database.insert(agrScopeList);
        insert agrScopeList;
        Database.SaveResult[] sr = Database.insert(agrScopeList,false) ;
        for(Integer i=0;i<sr.size();i++){
           if(!sr[i].isSuccess()){
                lstFailedRecords.add(agrScopeList[i].Account__C);
                system.debug(LoggingLevel.ERROR,'MVK Failure --->'+agrScopeList[i]); 
            }
        }
        lstFailedRecords.add('MVK Dummy Value');
        return lstFailedRecords;

Note: MVK Dummy Value is just for identifying if the list is getting populated. Now I am debugging the same on the helper as below:-

var action= component.get("c.createAgrScope");
    var recordId = component.get("v.pageReference").state.recordId;
    action.setParams({"accIds": accId,
                      "agreementId": recordId});
    action.setCallback(this, function(a) {
        console.log('MVK Status of Action--->'+a.getState());
        if(a.getState() === 'SUCCESS'){
            console.log('MVK Log--->'+a.getReturnValue());
            this.updateCentralPurchasing(component,event);
        }else{
            var errorMsg = a.getError();
            var message = errorMsg[0].message;
            component.set("v.useSpinner", false);
            //alert(message);
            this.handleErrors(errorMsg);
        } 
    });
    $A.enqueueAction(action);

Now I am trying to insert a record which successfully gets inserted (no validation exceptions), and I am expecting the list to be empty.

However, in the return value I am still getting the id of the successful record, how is it possible? can anyone help with the solution.

Thanks.

2

You're attempting to insert the records twice. The first insert happens successfully, while the second one fails (but doesn't throw an exception because allOrNone=false). You'll want to remove insert agrScopeList; from your code.

  • Thanks fox. I totally missed to comment that. Such a silly thing :) – mvk1991 Apr 19 at 11:58

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.