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
thatheraherethatherahere 

Is there any special restriction on using OriginalQueueId field in AgentWork trigger?

We have written a before/after update trigger on AgentWork object to get information about the Owner Change from Queue to User on Case record acceptance from Omni Channel. To get this From and To owner information we tried to use UserId and OriginalQueueId fields from AgentWork object. As we tried to access and use OriginalQueueId field in our trigger's handler class, Our trigger stopped firing. We found no debug logs in developer console on case acceptance. As we commented or removed OriginalQueueId field from our trigger code, trigger started executing. In our findings we saw that "OriginalQueueId" field is not present within AgentWork object from trigger.new list.
Here is the log:

AgentWork: AgentWork:{Id=0Bz5B0000004LhuSAE, IsDeleted=false, Name=00004387, CreatedDate=2017-09-28 12:33:56, CreatedById=0055B000000W1hGQAS, LastModifiedDate=2017-09-28 12:34:01, LastModifiedById=0055B000000W1hGQAS, SystemModstamp=2017-09-28 12:34:01, UserId=0055B000000W1hGQAS, WorkItemId=5005B000002SIDGQA4, Status=Opened, ServiceChannelId=0N932000000TN1KCAW, LASessionId=2de1db2c-6ae1-429e-97b9-160592cae978, StatusSequence=0, CapacityWeight=1.00, CapacityPercentage=null, RequestDateTime=2017-09-28 12:28:48, AcceptDateTime=2017-09-28 12:34:01, DeclineDateTime=null, CloseDateTime=null, SpeedToAnswer=313, AgentCapacityWhenDeclined=null, PendingServiceRoutingId=0JR5B0000000Ke6WAE, PushTimeout=60, PushTimeoutDateTime=null, HandleTime=null, ActiveTime=null, DeclineReason=null, CancelDateTime=null, AssignedDateTime=2017-09-28 12:33:56}


As we did not find this field in AgentWork record from trigger.new list, We tried to do the following query on AgentWork record to get OriginalQueueId field value:

SELECT Id, OriginalQueueId FROM AgentWork WHERE Id in: setCurrentAgentWorkIds


and again this stopped our trigger from execution and found no debug logs were generated for our trigger statements.
We try to execute the same query in Developer console & eclipse and we were able to see OriginalQueueId field value in results. It is just not working in use with our trigger execution. I verified the isAccessible via Schema.DescribeFieldResult and it is returning "true" for OriginalQueueId field.

Can we get an explanation on this behavior? Is there any special restriction on using OriginalQueueId field in AgentWork trigger?
See Class Here:

public with sharing class AgentWorkTriggerHandler {

    public static void afterUpdate( list<AgentWork> lstNewAgentWorks, map<Id, AgentWork> mapNewAgentWorks, list<AgentWork> oldNewAgentWorks, map<Id, AgentWork> mapOldAgentWorks ){

        map<Id, AgentWork> mapAWorkByCaseId = new map<Id, AgentWork>();
        for( AgentWork aWork : lstNewAgentWorks ){
            System.debug('---> AgentWork: '+aWork);
            if( aWork.Status == 'Opened' && String.valueOf( aWork.WorkItemId ).startsWith( '500' ) ){
                mapAWorkByCaseId.put( aWork.WorkItemId, aWork );
            }
        }

        if( mapAWorkByCaseId.size() > 0 ){
            map<Id, Case> mapNewCase = new map<Id, Case>();
            map<Id, Case> mapOldCase = new map<Id, Case>();

            map<Id, Id> mapQueueIdByWorkId = getQueueIdByWorkId( mapNewAgentWorks.keyset() );

            for( Case caseObj : [SELECT Id, OwnerId, Status, CreatedDate FROM Case WHERE Id in:mapAWorkByCaseId.keySet()]){
                AgentWork aWork = mapAWorkByCaseId.get( caseObj.Id );
                mapNewCase.put( caseObj.Id, caseObj );

                Case oldCase = caseObj; 
                oldCase.OwnerId = mapQueueIdByWorkId.get( aWork.Id );
                mapOldCase.put( caseObj.Id, oldCase );
            }
            System.debug( '----> mapOldCase: '+mapOldCase);
            System.debug( '----> mapNewCase: '+mapNewCase);
            CaseTriggerHandler.updateCaseLifeCycleRecords( mapNewCase.values(), mapNewCase, mapOldCase.values(), mapOldCase );
        }
    }

    public static map<Id, Id> getQueueIdByWorkId( set<Id> workIds ){
        map<Id, Id> mapQueueIdByWorkId = new map<Id, Id>();

        // If I comment below loop statement, I can see debug logs from this handler class in Developer console.
        // If I leave below loop uncommented, I did not get any debug logs for this handler class in developer console. even no debugs from the trigger. 
        for( AgentWork aWork : [SELECT Id, OriginalQueueId FROM AgentWork WHERE Id in:workIds]){ 
            mapQueueIdByWorkId.put( aWork.Id, aWork.OriginalQueueId );
        }
        return mapQueueIdByWorkId;
    }
}
Johannes FischerJohannes Fischer
We ran into the same issue and ended up moving the logic to query and access the OriginalQueueId field in a separate transcaction using the @future annotation. This solved the issue for us, though the execution of the code is obviously delayed now. 

I am not sure what the restrictions on the field are when the trigger is executed in the context of a case routed through Omni, but it certainly did not help that there aren't any Apex errors. I know it's a bit late now, but I hope this workaround will still help. 

Cheers
HareHare
HI thatherahere /Johannes Fischer,

i am faceing same problem could you please help me how to achive this functinality .

Thanks,
Hare.