+ Start a Discussion
D VelD Vel 

Database.SaveResult for reteriving all the validation error

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
 
Maharajan CMaharajan C
Hi Vel,

Please try to use the AuraHandlerException in Class . And refer the below code also refer the below link also for referrence:

Apex Class:
 
@AuraEnabled
public static String passCase(String caseId){
	try{
		Case case_rec = [SELECT id,Route_Case_Flow_Executing__c,Status from Case WHERE Id = :caseId];
		case_rec.Status = 'Transferred';
		case_rec.Route_Case_Flow_Executing__c = true;
		update case_rec;
	}	
	catch(Exception ex){
		throw new AuraHandledException('Oops!!! Something went wrong: ' + ex.getMessage());
	}
}

Lightning JS:
 
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')
            {
			    var errors = response.getError();
				var errorMsg = errors[0].message;
				console.log(errorMsg);
				var toastEvent = $A.get("e.force:showToast");
				toastEvent.setParams({
					title: 'Error',
					type: 'error',
					mode: 'pester',
					message: errorMsg
				});
				toastEvent.fire();			
           }

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

https://blogs.absyz.com/2018/05/25/handling-exceptions-in-lightning-using-aurahandledexceptions/
https://medium.com/@subaa.6/aurahandledexception-lightning-components-f6d3fb4ab57c

Thanks,
Maharajan.C
sachinarorasfsachinarorasf
Hi D Vel,

You should look into the following link that will help you out.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_saveresult.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com