function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Erik BurtonErik 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:
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 = QUEUEIDSTRING
and 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!
 
Ashutosh GurjarAshutosh Gurjar
Trigger ReassignOCClosedCases on Case (before insert, before update) {
    set<ID> sOCQueues = new set<ID>();
    for(Group g : [SELECT ID, Name FROM Group WHERE QueueRoutingConfigID != '' ]) {
        sOCQueues.add(g.ID); 
    }
    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') {
            //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);
            }
        }
        
    }
}

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
Erik BurtonErik Burton
Hi Ashutosh, thanks for you reply!

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!
Ashutosh GurjarAshutosh Gurjar
Hi Erik Burton ,

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,,,
 
Trigger ReassignOCClosedCases on Case (before insert, before update) {

  if(Trigger.isBefore) {
       if(Trigger.isInsert || Trigger.isUpdate) {
set<ID> sOCQueues = new set<ID>();
    for(Group g : [SELECT ID, Name FROM Group WHERE QueueRoutingConfigID != '' ]) {
        sOCQueues.add(g.ID); 
    }
    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') {
            //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);
            }
        }
        
    }

       }

  }
    
}

you just check the even for best practice,,,
Thanks
Paul S.Paul S.
I think you might need to re-query for the same record before you perform your assertion so that you get the post-update version.