0

This question already has an answer here:

    trigger ServiceEnquiryOwnershipTrigger on Service_Enquiry__c (before insert) {

       System.debug('~~~ Begin');

        Set<String> accset = new Set<String>();

        for(Service_Enquiry__c s : Trigger.new)
        {
            accset.add(s.Account__c);
        }


        Map<Id,Account> accMap = new Map<Id,Account>([SELECT ID,OwnerId FROM Account WHERE ID IN :accSet]); 
        for(Service_Enquiry__c s : Trigger.New)
        {
            System.debug('~~~ Inside Loop');

          try
          {
                      if(s.Account__c != null)
                      {
                           System.debug('~~~ Account Match Found');
                           s.OwnerId = accMap.get(s.Account__c).OwnerId;
                           System.debug('~~~ Owner Id = '+s.OwnerId);

                      }
                      else
                      {
                          System.debug('~~ Account Not Found');
//BELOW LINE THROWS EXCEPTION
                          s.OwnerId = SEMapping__c.getValues(s.Lead_Office__c).Manager__c;
                          System.debug('~~~ Owner Id = '+s.OwnerId);

                      }
          }
          catch(Exception e)
          {
             System.debug('~~~ Inside Exception');
              s.OwnerId = SEMapping__c.getValues('Default').Manager__c;
              System.debug('~~~ Owner Id = '+s.OwnerId);
              s.System_Comments__c = 'Exception occured while assigning ownership';
          }
        }

        System.debug('~~~ End');
    }

As commented in the code snippet I get an exception at the line

s.OwnerId = SEMapping__c.getValues(s.Lead_Office__c).Manager__c;

In that custom settings I have knowingly put an inactive user to trigger an exception. In the catch block i intended to get a default user (who will always be active) and keep that as owner whenever there is an exception. The exception is indeed getting thrown but for some reason it is not being caught by the "catch" block.

Let us assume inactive user as XXX and the default active user as YYY

The following occurs (obtained from the debug log)

~~~ Begin
~~~ Inside Loop
~~ Account Not Found
~~~ Owner Id = XXX
~~~ End

What I expected (when there is an exception)

~~~ Begin
~~~ Inside Loop
~~ Account Not Found
~~~ Owner Id = XXX
~~~ Inside Exception
~~~ Owner Id = YYY
~~~ End

I get the following exception

13:20:02.0 (280750383)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INACTIVE_OWNER_OR_USER, operation performed with inactive user [XXX] as owner of 01Ip0000000Ck3E: []

Can someone tell me what I am missing ?

marked as duplicate by Adrian Larson apex Nov 12 '17 at 14:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    found out what the issue was...actually the exception DOES NOT occur at the place mentioned in the trigger. All I do in that code snippet is ASSIGN the s.OwnerId but actually the SAVE operation does not happen at that step. So the exception does not even occur at this specific place but rather happens when the actually save happens..I hope I am not confusing – Aaron Wilfred Nov 1 '16 at 3:28
  • you are right Aaron. here's a similar thread which has bit more detail on this salesforce.stackexchange.com/questions/29451/… – Vamsi Krishna Nov 1 '16 at 3:37
  • Please answer your own question then, Aaron. – Renato Oliveira Nov 11 '17 at 21:11
2

According to Order of Execution. all before trigger run, then DML happens and then after trigger runs. Now in your case the DML happens after the execution of before insert trigger. Thus try catch block is useless here(It can catch only null ptr execption)

In Your code its asigning ownerID as Null/Inactive owner,but DML is yet to be performed thus the catch block wont run.

You should use try catch block from main code where the actual DML operation starts.

try{
  insert yourRecord;
}catch(Exception e){

//your code
}

DML exceptions should always be handled from place where the actual DML statement was called.

0

There is noting wrong with your code. The thing is we can not catch all exception for ex. governor limit or the one your getting.

The best way to handle your exception is to make sure all new owners are assigning are active. Ref: https://developer.salesforce.com/page/An_Introduction_to_Exception_Handling

  • Whole execution breaks when there is Governor Exception, as we can see the last debug gets printed, thus it isnt governor exception(Its DML exception), I feel its logical error. – Pranay Jaiswal Nov 1 '16 at 3:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.