0

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());
        }
  • I think it is a Salesforce limit error. thats why you cannot handle it using try catch. – Tushar Sharma Jan 19 '16 at 11:28
2

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

0

Database.update(listUserDeactivate,false);

false here means "all or none" but also "don't throw errors". You can get them from the result though.

0

with your code scenario, You can not enter into catch block. because as you are using database.update() method, you will not get any exception even updation of record fails in code.

methods for the database class will give the errors also as results.

there will be a object named as "Error" in Database.SaveResult.

from that error object you will get exceptions message and its details.

And I think.. instead of using List, it is best practice to use Database.SaveResult[]. please follow the signature. Signature

public static Database.SaveResult[] update(sObject[] recordsToUpdate, Boolean allOrNone)

if this is not the issue you are facing, then you are getting issue in this snippet based on the code that is available above this code snippet.

  • I used Database.update(listUserDeactivate) and it worked.I was able to catch exception.Thanks for all help. – Jack vardy Jan 19 '16 at 18:08

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.