Show only DML exception message from parent exception
In below code we just put account name is blank as we know it is required field so when DML will be execute then it will throw an exception.
***
public static void showDMLException(){
Account acctRecord = new Account();
acctRecord.Name = '';
try{
INSERT acctRecord;
}catch(Exception ex){
System.debug('exception$$ ' + ex);
if(ex.getTypeName() == 'System.DMLException' || ex.getTypeName() == 'System.MailException'){
System.debug('dml exception$$ ' + ex.getDMLMessage(0));
}
}
}
***
System.debug('exception$$ ' + ex);
This statement will show complete exception message. But we need only DML exception message so this message we can get from below conditions.
if(ex.getTypeName() == 'System.DMLException' || ex.getTypeName() == 'System.MailException'){
System.debug('dml exception$$ ' + ex.getDMLMessage(0));
}
Comments
Post a Comment