[SalesForce] Not able to catch exception using try catch block

I am using try-catch block to catch exceptions.code is executing inside try block.Exceptions happens which i am thinking i am handling in catch block.When i debug whole code after exception code is not going inside catch block.I am getting below error logs.

16:39:41.0
(917233674)|VARIABLE_ASSIGNMENT|[98]|results|{"s":1,"v":[{"success":false,"errors":[{"status":"UNKNOWN_EXCEPTION","message":"web-to-case
user can (18 more)
…"}]},{"success":true,"id":"00524000001IEr4AAG"},{"success":false,"errors":[{"status":"DEPENDENCY_EXISTS","message":"Cannot
complete this (106 more) …"}

Code:

try{
            System.debug(':::::::inside try block ::::::::');
            List<Database.SaveResult> results= Database.update(listUserDeactivate,false);
            System.debug(':::::::inside try block 2 ::::::::');
        }catch(exception e) {
            System.debug('value of ids of deactivation users ::::: '+listUserDeactivate);
            Set<Id> resultIds = (new Map<Id,User>(listUserDeactivate)).keySet();
            System.debug('value of ids of exception users ::::: '+resultIds);
            freezeUserList = [SELECT Id,IsFrozen,UserId FROM UserLogin where UserId in :resultIds];
            for (UserLogin freezeuser : freezeUserList) {
                freezeuser.IsFrozen = true;
              System.debug('inside catch with freeze ::::: '+freezeuser);  
            }
            update freezeUserList;
            System.debug('error details in catch block::::: '+e.getTypeName() + ' - ' + e.getCause() + ': ' + e.getMessage());
        }

Best Answer

You have the following line inside the try block.`

List<Database.SaveResult> results= Database.update(listUserDeactivate,false);

The database.update method takes in two parameters as in your code:

  1. The sobject or collection of sobjects to perform the update on
  2. allOrNone - This parameter is set to false to allow partial success

The second parameter called allOrNone is exactly what differentiates the database.update from update. While using the update method and performimg a DML like

update listUserDeactivate;

Then even when the update of any one of the records in the list fails - the whole update process fails and a DML exception is thrown. This would have been in your case too have you written the code as

List<Database.SaveResult> results= Database.update(listUserDeactivate,true);

This sets the allOrNone parameter to true which implies system either updates all the records(i.e. when no individual record update fails) or updates no record(i.e. the scenario where at least the update of one record in the list fails). So what is happening in you case is that because allOrNone is set to false, partial success of the update operation is allowed and that is what exactly happened. Not all records in the list were updated successfully and due to your way of using database.upsert NO EXCEPTION was thrown.

But logs capture everything in finer detail, including the DML update situation of every individual record. Hence the failure is logged in the debug logs. Thanks

Related Topic