Exception in Database.save result in trigger context

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












1) In my opportunityLineitemtrigger, I have a method which updates the opportunity.



system.debug('######');
database.saveresult rst = database.update(oppList,true);
system.debug('######'+rst);


While updating the opportunity, it calls the before update trigger event of opportunity. In that before update trigger method, I have used an add error method to throw an error message for some of the records which met the criteria



for(Opportunity opp : newList)
if(opp.RecordTypeId != SEUtility.getRecordTypeId(SEUtility.OPP_RECTYPE))
opp.addError(Label.Record_type_not_allowed_to_change);




Once the before update triggers fire with adderror , I dont get anything in database.saveresult in the above code , the debug line system.debug('######'+rst); doesn't gets executed , on checking the logs I see the adderror exception.



Is this is as per salesforce guidelines, since I know when we used database.saveresult it will show the records which got failed and which got succeeded. If for adderror some records failed why cant I get that details in the saveresults.



2) Secondly I see a big error message in RED as




Review all error messages below to correct your data. Apex trigger
OpportunityLineItem caused an unexpected exception, contact your
administrator: OpportunityLineItem: execution of AfterUpdate caused
by: System.DmlException: Update failed. First exception on row 0 with
id 0061p0000iuy7AAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION,
Fill the values as per the standards: :
Class.OpportunityLineItemTriggerHandler.updatesalesvalue: line 95,
column 1




If I am using adderror , it should have been the single line custom error message which i had defined but why so many details shown on UI ?










share|improve this question





























    up vote
    1
    down vote

    favorite












    1) In my opportunityLineitemtrigger, I have a method which updates the opportunity.



    system.debug('######');
    database.saveresult rst = database.update(oppList,true);
    system.debug('######'+rst);


    While updating the opportunity, it calls the before update trigger event of opportunity. In that before update trigger method, I have used an add error method to throw an error message for some of the records which met the criteria



    for(Opportunity opp : newList)
    if(opp.RecordTypeId != SEUtility.getRecordTypeId(SEUtility.OPP_RECTYPE))
    opp.addError(Label.Record_type_not_allowed_to_change);




    Once the before update triggers fire with adderror , I dont get anything in database.saveresult in the above code , the debug line system.debug('######'+rst); doesn't gets executed , on checking the logs I see the adderror exception.



    Is this is as per salesforce guidelines, since I know when we used database.saveresult it will show the records which got failed and which got succeeded. If for adderror some records failed why cant I get that details in the saveresults.



    2) Secondly I see a big error message in RED as




    Review all error messages below to correct your data. Apex trigger
    OpportunityLineItem caused an unexpected exception, contact your
    administrator: OpportunityLineItem: execution of AfterUpdate caused
    by: System.DmlException: Update failed. First exception on row 0 with
    id 0061p0000iuy7AAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION,
    Fill the values as per the standards: :
    Class.OpportunityLineItemTriggerHandler.updatesalesvalue: line 95,
    column 1




    If I am using adderror , it should have been the single line custom error message which i had defined but why so many details shown on UI ?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      1) In my opportunityLineitemtrigger, I have a method which updates the opportunity.



      system.debug('######');
      database.saveresult rst = database.update(oppList,true);
      system.debug('######'+rst);


      While updating the opportunity, it calls the before update trigger event of opportunity. In that before update trigger method, I have used an add error method to throw an error message for some of the records which met the criteria



      for(Opportunity opp : newList)
      if(opp.RecordTypeId != SEUtility.getRecordTypeId(SEUtility.OPP_RECTYPE))
      opp.addError(Label.Record_type_not_allowed_to_change);




      Once the before update triggers fire with adderror , I dont get anything in database.saveresult in the above code , the debug line system.debug('######'+rst); doesn't gets executed , on checking the logs I see the adderror exception.



      Is this is as per salesforce guidelines, since I know when we used database.saveresult it will show the records which got failed and which got succeeded. If for adderror some records failed why cant I get that details in the saveresults.



      2) Secondly I see a big error message in RED as




      Review all error messages below to correct your data. Apex trigger
      OpportunityLineItem caused an unexpected exception, contact your
      administrator: OpportunityLineItem: execution of AfterUpdate caused
      by: System.DmlException: Update failed. First exception on row 0 with
      id 0061p0000iuy7AAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION,
      Fill the values as per the standards: :
      Class.OpportunityLineItemTriggerHandler.updatesalesvalue: line 95,
      column 1




      If I am using adderror , it should have been the single line custom error message which i had defined but why so many details shown on UI ?










      share|improve this question















      1) In my opportunityLineitemtrigger, I have a method which updates the opportunity.



      system.debug('######');
      database.saveresult rst = database.update(oppList,true);
      system.debug('######'+rst);


      While updating the opportunity, it calls the before update trigger event of opportunity. In that before update trigger method, I have used an add error method to throw an error message for some of the records which met the criteria



      for(Opportunity opp : newList)
      if(opp.RecordTypeId != SEUtility.getRecordTypeId(SEUtility.OPP_RECTYPE))
      opp.addError(Label.Record_type_not_allowed_to_change);




      Once the before update triggers fire with adderror , I dont get anything in database.saveresult in the above code , the debug line system.debug('######'+rst); doesn't gets executed , on checking the logs I see the adderror exception.



      Is this is as per salesforce guidelines, since I know when we used database.saveresult it will show the records which got failed and which got succeeded. If for adderror some records failed why cant I get that details in the saveresults.



      2) Secondly I see a big error message in RED as




      Review all error messages below to correct your data. Apex trigger
      OpportunityLineItem caused an unexpected exception, contact your
      administrator: OpportunityLineItem: execution of AfterUpdate caused
      by: System.DmlException: Update failed. First exception on row 0 with
      id 0061p0000iuy7AAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION,
      Fill the values as per the standards: :
      Class.OpportunityLineItemTriggerHandler.updatesalesvalue: line 95,
      column 1




      If I am using adderror , it should have been the single line custom error message which i had defined but why so many details shown on UI ?







      apex trigger database adderror






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      Sebastian Kessel

      8,33651735




      8,33651735










      asked 2 hours ago









      Quddus

      608




      608




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          1. You're passing true as the second parameter (allOrNone) to Database.update(). What you're observing is the documented behavior for that situation:


          ... using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. You can do so by passing an additional second Boolean parameter. If you specify false for this parameter and if a record fails, the remainder of DML operations can still succeed. Also, instead of exceptions, a result object array (or one result object if only one sObject was passed in) is returned containing the status of each operation and any errors encountered. By default, this optional parameter is true, which means that if at least one sObject can’t be processed, all remaining sObjects won’t and an exception will be thrown for the record that causes a failure.




          (See DML Statements vs. Database Class Methods)



          When you pass true, you get an exception thrown if any record fails, rather than a set of Database.SaveResult records.



          1. The error message essentially gets wrapped in a DmlException which yields this rather user-unfriendly stack trace message. If you want to fix that, you'd need to wrap your DML code at the top level (in your page controller) with a try/catch block and generate a more friendly page message. Alternately, pass false for allOrNone as above, and synthesize a message to the user from the result messages you get back. (See Database.SaveResult)





          share|improve this answer
















          • 1




            Damn you're fast. I was halfway through writing the answer. :)
            – Sebastian Kessel
            2 hours ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "459"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f234692%2fexception-in-database-save-result-in-trigger-context%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote













          1. You're passing true as the second parameter (allOrNone) to Database.update(). What you're observing is the documented behavior for that situation:


          ... using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. You can do so by passing an additional second Boolean parameter. If you specify false for this parameter and if a record fails, the remainder of DML operations can still succeed. Also, instead of exceptions, a result object array (or one result object if only one sObject was passed in) is returned containing the status of each operation and any errors encountered. By default, this optional parameter is true, which means that if at least one sObject can’t be processed, all remaining sObjects won’t and an exception will be thrown for the record that causes a failure.




          (See DML Statements vs. Database Class Methods)



          When you pass true, you get an exception thrown if any record fails, rather than a set of Database.SaveResult records.



          1. The error message essentially gets wrapped in a DmlException which yields this rather user-unfriendly stack trace message. If you want to fix that, you'd need to wrap your DML code at the top level (in your page controller) with a try/catch block and generate a more friendly page message. Alternately, pass false for allOrNone as above, and synthesize a message to the user from the result messages you get back. (See Database.SaveResult)





          share|improve this answer
















          • 1




            Damn you're fast. I was halfway through writing the answer. :)
            – Sebastian Kessel
            2 hours ago














          up vote
          4
          down vote













          1. You're passing true as the second parameter (allOrNone) to Database.update(). What you're observing is the documented behavior for that situation:


          ... using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. You can do so by passing an additional second Boolean parameter. If you specify false for this parameter and if a record fails, the remainder of DML operations can still succeed. Also, instead of exceptions, a result object array (or one result object if only one sObject was passed in) is returned containing the status of each operation and any errors encountered. By default, this optional parameter is true, which means that if at least one sObject can’t be processed, all remaining sObjects won’t and an exception will be thrown for the record that causes a failure.




          (See DML Statements vs. Database Class Methods)



          When you pass true, you get an exception thrown if any record fails, rather than a set of Database.SaveResult records.



          1. The error message essentially gets wrapped in a DmlException which yields this rather user-unfriendly stack trace message. If you want to fix that, you'd need to wrap your DML code at the top level (in your page controller) with a try/catch block and generate a more friendly page message. Alternately, pass false for allOrNone as above, and synthesize a message to the user from the result messages you get back. (See Database.SaveResult)





          share|improve this answer
















          • 1




            Damn you're fast. I was halfway through writing the answer. :)
            – Sebastian Kessel
            2 hours ago












          up vote
          4
          down vote










          up vote
          4
          down vote









          1. You're passing true as the second parameter (allOrNone) to Database.update(). What you're observing is the documented behavior for that situation:


          ... using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. You can do so by passing an additional second Boolean parameter. If you specify false for this parameter and if a record fails, the remainder of DML operations can still succeed. Also, instead of exceptions, a result object array (or one result object if only one sObject was passed in) is returned containing the status of each operation and any errors encountered. By default, this optional parameter is true, which means that if at least one sObject can’t be processed, all remaining sObjects won’t and an exception will be thrown for the record that causes a failure.




          (See DML Statements vs. Database Class Methods)



          When you pass true, you get an exception thrown if any record fails, rather than a set of Database.SaveResult records.



          1. The error message essentially gets wrapped in a DmlException which yields this rather user-unfriendly stack trace message. If you want to fix that, you'd need to wrap your DML code at the top level (in your page controller) with a try/catch block and generate a more friendly page message. Alternately, pass false for allOrNone as above, and synthesize a message to the user from the result messages you get back. (See Database.SaveResult)





          share|improve this answer












          1. You're passing true as the second parameter (allOrNone) to Database.update(). What you're observing is the documented behavior for that situation:


          ... using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. You can do so by passing an additional second Boolean parameter. If you specify false for this parameter and if a record fails, the remainder of DML operations can still succeed. Also, instead of exceptions, a result object array (or one result object if only one sObject was passed in) is returned containing the status of each operation and any errors encountered. By default, this optional parameter is true, which means that if at least one sObject can’t be processed, all remaining sObjects won’t and an exception will be thrown for the record that causes a failure.




          (See DML Statements vs. Database Class Methods)



          When you pass true, you get an exception thrown if any record fails, rather than a set of Database.SaveResult records.



          1. The error message essentially gets wrapped in a DmlException which yields this rather user-unfriendly stack trace message. If you want to fix that, you'd need to wrap your DML code at the top level (in your page controller) with a try/catch block and generate a more friendly page message. Alternately, pass false for allOrNone as above, and synthesize a message to the user from the result messages you get back. (See Database.SaveResult)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          David Reed

          20.8k31640




          20.8k31640







          • 1




            Damn you're fast. I was halfway through writing the answer. :)
            – Sebastian Kessel
            2 hours ago












          • 1




            Damn you're fast. I was halfway through writing the answer. :)
            – Sebastian Kessel
            2 hours ago







          1




          1




          Damn you're fast. I was halfway through writing the answer. :)
          – Sebastian Kessel
          2 hours ago




          Damn you're fast. I was halfway through writing the answer. :)
          – Sebastian Kessel
          2 hours ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f234692%2fexception-in-database-save-result-in-trigger-context%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          Installing NextGIS Connect into QGIS 3?

          One-line joke