You need to sign in to do that
Don't have an account?
miguelguerreiro@live.com
Trigger on Case Comment
Hello,
I'm trying to create a trigger on Case Comments so that my Customer Portal users are not able to add comments to closed cases.
trigger CloseComment on CaseComment (before insert) { for (CaseComment t: Trigger.new) { Case c = new Case(Id = t.ParentId); c = [SELECT Status FROM Case WHERE Id = :c.Id]; if(c.Status == 'Closed' && System.Userinfo.getUserType()!= 'Standard') { t.addError('You cannot add comments to closed cases.'); } }
but it fails with bulk import. any ideas?
your code was not written by considering BULK load,
Try below code
Let me know does It rock for you :)
Cheers,
Bala
All Answers
It fails on bulk inserts because you have a query in the for loop of the trigger.
Read this post.
Reply if you need further help.
your code was not written by considering BULK load,
Try below code
Let me know does It rock for you :)
Cheers,
Bala
Thanks Bala, your code working fine fine :)
I also had tried the following code but it failed with BULK import:
Here's the error message:
Any idea why it's failing? I believe it follows the same logic as your code.
Thanks!
Hello, you are quering a SOQL inside the second for loop. Plz avoid any SOQL query in the loop.
Use maps or list for holding all Cases having ids in idSet and then iterate through all cases inside the list.
you can frame your code as:
Try this