How to enforce CRUD and FLS in bulk trigger?
Clash 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;
trigger exception fls crud adderror
add a comment |Â
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;
trigger exception fls crud adderror
add a comment |Â
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;
trigger exception fls crud adderror
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
trigger exception fls crud adderror
edited 35 mins ago
asked 2 hours ago


gpoluch
1238
1238
add a comment |Â
add a comment |Â
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.
Thanks @sfdcfox but I wonder how can I useaddError()
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
 |Â
show 1 more comment
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.
Thanks @sfdcfox but I wonder how can I useaddError()
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
 |Â
show 1 more comment
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.
Thanks @sfdcfox but I wonder how can I useaddError()
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
 |Â
show 1 more comment
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.
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.
answered 1 hour ago


sfdcfox
229k10176390
229k10176390
Thanks @sfdcfox but I wonder how can I useaddError()
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
 |Â
show 1 more comment
Thanks @sfdcfox but I wonder how can I useaddError()
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
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password