How to enforce CRUD and FLS in bulk trigger?

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
2
down vote

favorite












How to enforce CRUD and FLS in following situation (bulk trigger). Better idea is simply throw an exception or addError(errMsg) ?



trigger CaseCommentTrigger on CaseComment (after insert) 
List<Case> toUpdate = new List<Case>();
Map<Id, List<CaseComment>> caseComments = new Map<Id, List<CaseComment>>();

for (CaseComment caseComment : Trigger.new)
if (caseComments.containsKey(caseComment.ParentId))
caseComments.get(caseComment.ParentId).add(caseComment);
else
caseComments.put(caseComment.ParentId, new List<CaseComment> caseComment);



// Check FLS here and throw exception here?
List<Case> cases = [SELECT Id, Status FROM Case WHERE Id IN :caseComments.keySet()];
for (Case c : cases )
// Check FLS and addError() on CaseComment assigned to Case?
// or check FLS here and throw exception here?
c.Subject = 'Updated';
toUpdate.add(c);


// Check FLS here and throw exception here?
update toUpdate;



Update: Is it correct approach?



trigger CaseCommentTrigger on CaseComment (after insert) 
List<Case> toUpdate = new List<Case>();
Map<Id, List<CaseComment>> caseComments = new Map<Id, List<CaseComment>>();

for (CaseComment caseComment : Trigger.new)
if (caseComments.containsKey(caseComment.ParentId))
caseComments.get(caseComment.ParentId).add(caseComment);
else
caseComments.put(caseComment.ParentId, new List<CaseComment> caseComment);



List<Case> cases = [SELECT Id, Status FROM Case WHERE Id IN :caseComments.keySet()];
for (Case c : cases )

try
SecurityUtils.checkFieldIsUpdateable(Case.SObjectType, Case.Subject);
c.Subject = 'Updated';
toUpdate.add(c);
catch (SecurityUtils.FlsException flsException)
for (CaseComment cc : caseComments.get(c.Id))
cc.addError(flsException);




update toUpdate;










share|improve this question





























    up vote
    2
    down vote

    favorite












    How to enforce CRUD and FLS in following situation (bulk trigger). Better idea is simply throw an exception or addError(errMsg) ?



    trigger CaseCommentTrigger on CaseComment (after insert) 
    List<Case> toUpdate = new List<Case>();
    Map<Id, List<CaseComment>> caseComments = new Map<Id, List<CaseComment>>();

    for (CaseComment caseComment : Trigger.new)
    if (caseComments.containsKey(caseComment.ParentId))
    caseComments.get(caseComment.ParentId).add(caseComment);
    else
    caseComments.put(caseComment.ParentId, new List<CaseComment> caseComment);



    // Check FLS here and throw exception here?
    List<Case> cases = [SELECT Id, Status FROM Case WHERE Id IN :caseComments.keySet()];
    for (Case c : cases )
    // Check FLS and addError() on CaseComment assigned to Case?
    // or check FLS here and throw exception here?
    c.Subject = 'Updated';
    toUpdate.add(c);


    // Check FLS here and throw exception here?
    update toUpdate;



    Update: Is it correct approach?



    trigger CaseCommentTrigger on CaseComment (after insert) 
    List<Case> toUpdate = new List<Case>();
    Map<Id, List<CaseComment>> caseComments = new Map<Id, List<CaseComment>>();

    for (CaseComment caseComment : Trigger.new)
    if (caseComments.containsKey(caseComment.ParentId))
    caseComments.get(caseComment.ParentId).add(caseComment);
    else
    caseComments.put(caseComment.ParentId, new List<CaseComment> caseComment);



    List<Case> cases = [SELECT Id, Status FROM Case WHERE Id IN :caseComments.keySet()];
    for (Case c : cases )

    try
    SecurityUtils.checkFieldIsUpdateable(Case.SObjectType, Case.Subject);
    c.Subject = 'Updated';
    toUpdate.add(c);
    catch (SecurityUtils.FlsException flsException)
    for (CaseComment cc : caseComments.get(c.Id))
    cc.addError(flsException);




    update toUpdate;










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      How to enforce CRUD and FLS in following situation (bulk trigger). Better idea is simply throw an exception or addError(errMsg) ?



      trigger CaseCommentTrigger on CaseComment (after insert) 
      List<Case> toUpdate = new List<Case>();
      Map<Id, List<CaseComment>> caseComments = new Map<Id, List<CaseComment>>();

      for (CaseComment caseComment : Trigger.new)
      if (caseComments.containsKey(caseComment.ParentId))
      caseComments.get(caseComment.ParentId).add(caseComment);
      else
      caseComments.put(caseComment.ParentId, new List<CaseComment> caseComment);



      // Check FLS here and throw exception here?
      List<Case> cases = [SELECT Id, Status FROM Case WHERE Id IN :caseComments.keySet()];
      for (Case c : cases )
      // Check FLS and addError() on CaseComment assigned to Case?
      // or check FLS here and throw exception here?
      c.Subject = 'Updated';
      toUpdate.add(c);


      // Check FLS here and throw exception here?
      update toUpdate;



      Update: Is it correct approach?



      trigger CaseCommentTrigger on CaseComment (after insert) 
      List<Case> toUpdate = new List<Case>();
      Map<Id, List<CaseComment>> caseComments = new Map<Id, List<CaseComment>>();

      for (CaseComment caseComment : Trigger.new)
      if (caseComments.containsKey(caseComment.ParentId))
      caseComments.get(caseComment.ParentId).add(caseComment);
      else
      caseComments.put(caseComment.ParentId, new List<CaseComment> caseComment);



      List<Case> cases = [SELECT Id, Status FROM Case WHERE Id IN :caseComments.keySet()];
      for (Case c : cases )

      try
      SecurityUtils.checkFieldIsUpdateable(Case.SObjectType, Case.Subject);
      c.Subject = 'Updated';
      toUpdate.add(c);
      catch (SecurityUtils.FlsException flsException)
      for (CaseComment cc : caseComments.get(c.Id))
      cc.addError(flsException);




      update toUpdate;










      share|improve this question















      How to enforce CRUD and FLS in following situation (bulk trigger). Better idea is simply throw an exception or addError(errMsg) ?



      trigger CaseCommentTrigger on CaseComment (after insert) 
      List<Case> toUpdate = new List<Case>();
      Map<Id, List<CaseComment>> caseComments = new Map<Id, List<CaseComment>>();

      for (CaseComment caseComment : Trigger.new)
      if (caseComments.containsKey(caseComment.ParentId))
      caseComments.get(caseComment.ParentId).add(caseComment);
      else
      caseComments.put(caseComment.ParentId, new List<CaseComment> caseComment);



      // Check FLS here and throw exception here?
      List<Case> cases = [SELECT Id, Status FROM Case WHERE Id IN :caseComments.keySet()];
      for (Case c : cases )
      // Check FLS and addError() on CaseComment assigned to Case?
      // or check FLS here and throw exception here?
      c.Subject = 'Updated';
      toUpdate.add(c);


      // Check FLS here and throw exception here?
      update toUpdate;



      Update: Is it correct approach?



      trigger CaseCommentTrigger on CaseComment (after insert) 
      List<Case> toUpdate = new List<Case>();
      Map<Id, List<CaseComment>> caseComments = new Map<Id, List<CaseComment>>();

      for (CaseComment caseComment : Trigger.new)
      if (caseComments.containsKey(caseComment.ParentId))
      caseComments.get(caseComment.ParentId).add(caseComment);
      else
      caseComments.put(caseComment.ParentId, new List<CaseComment> caseComment);



      List<Case> cases = [SELECT Id, Status FROM Case WHERE Id IN :caseComments.keySet()];
      for (Case c : cases )

      try
      SecurityUtils.checkFieldIsUpdateable(Case.SObjectType, Case.Subject);
      c.Subject = 'Updated';
      toUpdate.add(c);
      catch (SecurityUtils.FlsException flsException)
      for (CaseComment cc : caseComments.get(c.Id))
      cc.addError(flsException);




      update toUpdate;







      trigger exception fls crud adderror






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 35 mins ago

























      asked 2 hours ago









      gpoluch

      1238




      1238




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Do not throw exceptions in a trigger. This can cause undesirable side effects. You should always use addError in a trigger, and never allow an exception to escape from a trigger. In recent releases, an uncaught exception would end up wrapping the exception in a DmlException, losing the original context, and in at least this release, doing so can can cause a fatal exception that aborts your entire transaction, even if you use try-catch blocks. FLS does not change from one record to the next, so if you need to check it, check it just once before trying to update anything, and add an error to all rows that would cause the error.






          share|improve this answer




















          • Thanks @sfdcfox but I wonder how can I use addError() when I update parent records on child object trigger. Is it possible, is it work as expected - rollback the transaction?
            – gpoluch
            1 hour ago











          • @gpoluch No. Use partial update syntax, and report errors to the related record, as mentioned here and demonstrated here. Unless, of course, the transaction is meant to be an allOrNone update, but generally speaking, using the code in the related answer allows the caller to decide if they use allOrNone or not.
            – sfdcfox
            1 hour ago











          • Nice example @sfdcfox but I wonder if I do not need to check CRUD and FLS for Case when I update it from CaseComment Trigger? Correct me if I am wrong? How to accomplish that?
            – gpoluch
            1 hour ago











          • @gpoluch It depends on context. If the business rule requires an update for any comment, then FLS/CRUD checks might not be appropriate. You cannot blindly apply a rule either way. Each unique situation must be considered.
            – sfdcfox
            1 hour ago










          • Thanks for explanation @sfdcfox, but I need to solve checkmarx violation similiar to provided exmple, so I need to check FLS/CRUD somehow anyway.
            – gpoluch
            58 mins 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%2f233545%2fhow-to-enforce-crud-and-fls-in-bulk-trigger%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
          2
          down vote



          accepted










          Do not throw exceptions in a trigger. This can cause undesirable side effects. You should always use addError in a trigger, and never allow an exception to escape from a trigger. In recent releases, an uncaught exception would end up wrapping the exception in a DmlException, losing the original context, and in at least this release, doing so can can cause a fatal exception that aborts your entire transaction, even if you use try-catch blocks. FLS does not change from one record to the next, so if you need to check it, check it just once before trying to update anything, and add an error to all rows that would cause the error.






          share|improve this answer




















          • Thanks @sfdcfox but I wonder how can I use addError() when I update parent records on child object trigger. Is it possible, is it work as expected - rollback the transaction?
            – gpoluch
            1 hour ago











          • @gpoluch No. Use partial update syntax, and report errors to the related record, as mentioned here and demonstrated here. Unless, of course, the transaction is meant to be an allOrNone update, but generally speaking, using the code in the related answer allows the caller to decide if they use allOrNone or not.
            – sfdcfox
            1 hour ago











          • Nice example @sfdcfox but I wonder if I do not need to check CRUD and FLS for Case when I update it from CaseComment Trigger? Correct me if I am wrong? How to accomplish that?
            – gpoluch
            1 hour ago











          • @gpoluch It depends on context. If the business rule requires an update for any comment, then FLS/CRUD checks might not be appropriate. You cannot blindly apply a rule either way. Each unique situation must be considered.
            – sfdcfox
            1 hour ago










          • Thanks for explanation @sfdcfox, but I need to solve checkmarx violation similiar to provided exmple, so I need to check FLS/CRUD somehow anyway.
            – gpoluch
            58 mins ago














          up vote
          2
          down vote



          accepted










          Do not throw exceptions in a trigger. This can cause undesirable side effects. You should always use addError in a trigger, and never allow an exception to escape from a trigger. In recent releases, an uncaught exception would end up wrapping the exception in a DmlException, losing the original context, and in at least this release, doing so can can cause a fatal exception that aborts your entire transaction, even if you use try-catch blocks. FLS does not change from one record to the next, so if you need to check it, check it just once before trying to update anything, and add an error to all rows that would cause the error.






          share|improve this answer




















          • Thanks @sfdcfox but I wonder how can I use addError() when I update parent records on child object trigger. Is it possible, is it work as expected - rollback the transaction?
            – gpoluch
            1 hour ago











          • @gpoluch No. Use partial update syntax, and report errors to the related record, as mentioned here and demonstrated here. Unless, of course, the transaction is meant to be an allOrNone update, but generally speaking, using the code in the related answer allows the caller to decide if they use allOrNone or not.
            – sfdcfox
            1 hour ago











          • Nice example @sfdcfox but I wonder if I do not need to check CRUD and FLS for Case when I update it from CaseComment Trigger? Correct me if I am wrong? How to accomplish that?
            – gpoluch
            1 hour ago











          • @gpoluch It depends on context. If the business rule requires an update for any comment, then FLS/CRUD checks might not be appropriate. You cannot blindly apply a rule either way. Each unique situation must be considered.
            – sfdcfox
            1 hour ago










          • Thanks for explanation @sfdcfox, but I need to solve checkmarx violation similiar to provided exmple, so I need to check FLS/CRUD somehow anyway.
            – gpoluch
            58 mins ago












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Do not throw exceptions in a trigger. This can cause undesirable side effects. You should always use addError in a trigger, and never allow an exception to escape from a trigger. In recent releases, an uncaught exception would end up wrapping the exception in a DmlException, losing the original context, and in at least this release, doing so can can cause a fatal exception that aborts your entire transaction, even if you use try-catch blocks. FLS does not change from one record to the next, so if you need to check it, check it just once before trying to update anything, and add an error to all rows that would cause the error.






          share|improve this answer












          Do not throw exceptions in a trigger. This can cause undesirable side effects. You should always use addError in a trigger, and never allow an exception to escape from a trigger. In recent releases, an uncaught exception would end up wrapping the exception in a DmlException, losing the original context, and in at least this release, doing so can can cause a fatal exception that aborts your entire transaction, even if you use try-catch blocks. FLS does not change from one record to the next, so if you need to check it, check it just once before trying to update anything, and add an error to all rows that would cause the error.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          sfdcfox

          229k10176390




          229k10176390











          • Thanks @sfdcfox but I wonder how can I use addError() when I update parent records on child object trigger. Is it possible, is it work as expected - rollback the transaction?
            – gpoluch
            1 hour ago











          • @gpoluch No. Use partial update syntax, and report errors to the related record, as mentioned here and demonstrated here. Unless, of course, the transaction is meant to be an allOrNone update, but generally speaking, using the code in the related answer allows the caller to decide if they use allOrNone or not.
            – sfdcfox
            1 hour ago











          • Nice example @sfdcfox but I wonder if I do not need to check CRUD and FLS for Case when I update it from CaseComment Trigger? Correct me if I am wrong? How to accomplish that?
            – gpoluch
            1 hour ago











          • @gpoluch It depends on context. If the business rule requires an update for any comment, then FLS/CRUD checks might not be appropriate. You cannot blindly apply a rule either way. Each unique situation must be considered.
            – sfdcfox
            1 hour ago










          • Thanks for explanation @sfdcfox, but I need to solve checkmarx violation similiar to provided exmple, so I need to check FLS/CRUD somehow anyway.
            – gpoluch
            58 mins ago
















          • Thanks @sfdcfox but I wonder how can I use addError() when I update parent records on child object trigger. Is it possible, is it work as expected - rollback the transaction?
            – gpoluch
            1 hour ago











          • @gpoluch No. Use partial update syntax, and report errors to the related record, as mentioned here and demonstrated here. Unless, of course, the transaction is meant to be an allOrNone update, but generally speaking, using the code in the related answer allows the caller to decide if they use allOrNone or not.
            – sfdcfox
            1 hour ago











          • Nice example @sfdcfox but I wonder if I do not need to check CRUD and FLS for Case when I update it from CaseComment Trigger? Correct me if I am wrong? How to accomplish that?
            – gpoluch
            1 hour ago











          • @gpoluch It depends on context. If the business rule requires an update for any comment, then FLS/CRUD checks might not be appropriate. You cannot blindly apply a rule either way. Each unique situation must be considered.
            – sfdcfox
            1 hour ago










          • Thanks for explanation @sfdcfox, but I need to solve checkmarx violation similiar to provided exmple, so I need to check FLS/CRUD somehow anyway.
            – gpoluch
            58 mins ago















          Thanks @sfdcfox but I wonder how can I use addError() when I update parent records on child object trigger. Is it possible, is it work as expected - rollback the transaction?
          – gpoluch
          1 hour ago





          Thanks @sfdcfox but I wonder how can I use addError() when I update parent records on child object trigger. Is it possible, is it work as expected - rollback the transaction?
          – gpoluch
          1 hour ago













          @gpoluch No. Use partial update syntax, and report errors to the related record, as mentioned here and demonstrated here. Unless, of course, the transaction is meant to be an allOrNone update, but generally speaking, using the code in the related answer allows the caller to decide if they use allOrNone or not.
          – sfdcfox
          1 hour ago





          @gpoluch No. Use partial update syntax, and report errors to the related record, as mentioned here and demonstrated here. Unless, of course, the transaction is meant to be an allOrNone update, but generally speaking, using the code in the related answer allows the caller to decide if they use allOrNone or not.
          – sfdcfox
          1 hour ago













          Nice example @sfdcfox but I wonder if I do not need to check CRUD and FLS for Case when I update it from CaseComment Trigger? Correct me if I am wrong? How to accomplish that?
          – gpoluch
          1 hour ago





          Nice example @sfdcfox but I wonder if I do not need to check CRUD and FLS for Case when I update it from CaseComment Trigger? Correct me if I am wrong? How to accomplish that?
          – gpoluch
          1 hour ago













          @gpoluch It depends on context. If the business rule requires an update for any comment, then FLS/CRUD checks might not be appropriate. You cannot blindly apply a rule either way. Each unique situation must be considered.
          – sfdcfox
          1 hour ago




          @gpoluch It depends on context. If the business rule requires an update for any comment, then FLS/CRUD checks might not be appropriate. You cannot blindly apply a rule either way. Each unique situation must be considered.
          – sfdcfox
          1 hour ago












          Thanks for explanation @sfdcfox, but I need to solve checkmarx violation similiar to provided exmple, so I need to check FLS/CRUD somehow anyway.
          – gpoluch
          58 mins ago




          Thanks for explanation @sfdcfox, but I need to solve checkmarx violation similiar to provided exmple, so I need to check FLS/CRUD somehow anyway.
          – gpoluch
          58 mins 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%2f233545%2fhow-to-enforce-crud-and-fls-in-bulk-trigger%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery