0

I am trying to get all the validation error message from my apex class in to the lightning component. Earlier I was using the DMLException and only one 1 validation error was captured. So I tried to use the Database.SaveResultlike below is my apex Class

@AuraEnabled
public static String passCase(String caseId){
    List<Case> case_recs = [SELECT id,_Case_Flow__c,Status from Case WHERE Id = :caseId];
    String msg = '';
    for (Case rec : case_recs)
    {
        rec.Status = 'Transferred';
        rec.Route_Case_Flow_Executing__c = true;
        Database.SaveResult saves = Database.update(rec, false);

            if(!saves.isSuccess()) {
                msg = 'Error '+ saves.getErrors()[0].getMessage();
            }
           else {
               msg = 'Successfully Updated!';
           }
           return msg; 
        }          
    return msg;
}

And my lightning component looks like below

passCaseToCS_helper : function(c,e,h) {
    var set_action1 = c.get("c.passCase");
    set_action1.setParams({caseId: c.get('v.recordId')});
    set_action1.setCallback(this, function(result)
    {
        var resultFormApex = result.getReturnValue();
        if (result.getState() === 'SUCCESS')
            {               
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    message: resultFormApex ,
                    type: 'success',
                    mode: 'pester'
                });
                toastEvent.fire(); 
            }                
            else if(result.getState() === 'ERROR')
            {
                $A.get("e.force:showToast")
                        .setParams({
                            type: 'error',
                            mode: 'pester',
                            message: resultFormApex  }).fire();  
           }

        $A.get("e.force:refreshView").fire();
    });
    $A.enqueueAction(set_action1);       
}

When the record successfully gets updated with no Validation error I can see the Successfully Updated!in the toast for the records with Validation error I cannot see anything in the Toast. I tried to put a debug log to show what msg when the record update fails with Validation error I dont see anything on the logs either. I am not sure what I am missing here. Any help is greatly appreciated

1 Answer 1

0

There can be a possibility that your code is erroring out other you should have seen at least a green-coloured toast message(not red) or it can be that your query is not returning anything, so a blank msg is sent back, you can add a default value to confirm that

You can wrap your code in a try/catch block to make sure that your code is not throwing any exception. Throwing an AuraHandledException invokes the error handler on the client-side.

@AuraEnabled
public static String passCase(String caseId){
    try{
    List<Case> case_recs = [SELECT id,_Case_Flow__c,Status from Case WHERE Id = :caseId];
    String msg = 'DEFAULT VALUE';
    for (Case rec : case_recs)
    {
        rec.Status = 'Transferred';
        rec.Route_Case_Flow_Executing__c = true;
        Database.SaveResult saves = Database.update(rec, false);

            if(!saves.isSuccess()) {
                msg = 'Error '+ saves.getErrors()[0].getMessage();
            }
           else {
               msg = 'Successfully Updated!';
           }
           return msg; 
        }          
    return msg;
    } catch (Exception e){
         throw new AuraHandledException('Failed to execute or something else');
    }

}

There are couple of other issues like using a DML statement in for loop, because of this you will be returning only the first success or error. Add then to a list and send them back.

2
  • Can you confirm if I am using the Database.SaveResult correctly here?
    – user81642
    Apr 10, 2020 at 20:22
  • Yes, the statement is correct and you do not need the for loop.
    – manjit5190
    Apr 10, 2020 at 20:27

You must log in to answer this question.

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