You need to sign in to do that
Don't have an account?
Erik Burton
record change made in "before update" trigger not applying after the update
I'm making my first trigger, and I think I must be missing something basic. This is a trigger that detects whether the owner of a closed case is an omni-channel queue, and if it is it reassigns it to the last person who modified the case instead. The ownership change happens successfully in the trigger, but isn't being applied as part of the update itself.
Simplified code:
What am I missing here, is there a reason the record change isn't being applied as part of the update? Thanks for your help!
Simplified code:
Trigger ReassignOCClosedCases on Case (before insert, before update) { for (Case c : Trigger.new) { //Check if the case is closed and owned by an omni-channel queue. system.debug('ReassignOCCC triggered for: ' + c.Subject); if (c.Status == 'Closed') { set<ID> sOCQueues = new set<ID>(); List<Group> gOCQueues = [SELECT ID, Name FROM Group WHERE QueueRoutingConfigID != '' ]; for(Group g : gOCQueues) { sOCQueues.add(g.ID); } //Check if the current queue is OC-controlled if(sOCQueues.contains(c.OwnerID)) { // if it is, reassign to the person who last modified the case c.OwnerID = c.LastModifiedByID; system.debug('ROCCC: OwnerID is now ' + c.OwnerID); } } } }And a much abbreviated version of the test class:
@isTest private class TestReassignOCClosedCases { static testMethod void myUnitTest() { //Creates a queue group called gOCTestQueue, with a routing configuration and queue attached [removed for length] case cTestA = new case(Status ='New', Priority = 'Medium', Origin = 'Email', Subject = 'Test1 starts open then is closed', OwnerId = gOCTestQueue.ID); insert cTestA; cTestA.status = 'Closed'; system.debug('About to update after closing. OwnerID = ' + cTestA.OwnerID); update cTestA; system.debug('Updated after closing. OwnerID = ' + cTestA.OwnerID); system.assertNotEquals(cTestA.OwnerID, gOCTestQueue.ID); }The debug log will then show:
|DEBUG|About to update after closing. OwnerID = QUEUEIDSTRING and testQueueID = QUEUEIDSTRING |DEBUG|ROCCC: OwnerID is now USERIDSTRING |DEBUG|Updated after closing. OwnerID = QUEUEIDSTRINGand the AssertNotEquals fails.
What am I missing here, is there a reason the record change isn't being applied as part of the update? Thanks for your help!
i just resuffle your code.
you have to follow some best practice like you can not put query in the for loop.
It will work. please check this. if my answer is correct than mark it best answer.
Thanks in advance
Unfortunately that didn't effect anything. As before, the trigger successfully changes c.OwnerID to c.LastModifiedByID, but that change doesn't last outside of the trigger itself to be reflected in the updated case.
Is there anything special I need to do to have that field actually updated? Normally I'd call update c; but that's obviously not an option in the before update trigger, where there's already a pending update.
Thanks again!
i think your trigger is ok. There may some other permission problem,,please change your sharing setting of case object to private,,,because if you made sharing rule on case than it may effect the result,,,
one more thing you do,,,
you just check the even for best practice,,,
Thanks