2

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;
    }
}
  • I am also facing the similar Issue . Did you get any solution for this ? – Divya Gorantla May 8 '18 at 9:46
1

This is related an open bug scheduled to be fixed with Summer'19. Behavior of this field OriginalQueueId is very strange for now. It is not accessible on AgentWork instance, but it is present. I serialized the Trigger.new list in AgentWork trigger.

System.debug(JSON.serialize(Trigger.new));

Output is this,

[
  {
    "attributes": {
      "type": "AgentWork",
      "url": "/services/data/v45.0/sobjects/AgentWork/0Bz6C000000EUEQSA4"
    },
    "LastModifiedDate": "2019-03-28T15:23:02.000+0000",
    "ShouldSkipCapacityCheck": false,
    "RoutingModel": "LeastActive",
    "AssignedDateTime": "2019-03-28T15:23:02.000+0000",
    "Name": "00085887",
    "IsConference": false,
    "OwnerId": "0053400000CKLXTAA5",
    "CreatedById": "0053400000CKLXTAA5",
    "RequestDateTime": "2019-03-28T15:22:34.000+0000",
    "WorkItemId": "5006C00000489MBQAY",
    "RoutingType": "QueueBased",
    "Status": "Assigned",
    "IsDeleted": false,
    "StatusSequence": 0,
    "OriginalQueueId": "00G6C0000019JaeUAE",
    "CapacityWeight": 2.00,
    "LASessionId": "6dce0431-97b3-460e-8512-5cc3fe84d31a",
    "CurrencyIsoCode": "USD",
    "SystemModstamp": "2019-03-28T15:23:02.000+0000",
    "PushTimeout": 120,
    "UserId": "0053400000CKLXTAA5",
    "ServiceChannelId": "0N96C000000GmdISAS",
    "IsTransfer": false,
    "CreatedDate": "2019-03-28T15:23:02.000+0000",
    "Id": "0Bz6C000000EUEQSA4",
    "LastModifiedById": "0053400000CKLXTAA5",
    "PendingServiceRoutingId": "0JR6C000000CwYqWAK"
  }
]

But any attempt to access the OriginalQueueId field value on AgentWork fails at runtime as reported in the issue link provided above. Following code,

for(AgentWork aw : Trigger.new)
{
     Id originalQueueId = aw.OriginalQueueId;
}

Compiles but fails at runtime with this error message,

Variable does not exist: OriginalQueueId

Though not very happy with the approach, this is how I could access the value for now,

String originalQueueId = String.valueOf(((Map<String, Object>)JSON.deserializeUntyped(JSON.serialize(aw))).get('OriginalQueueId'));

This executes just fine and works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.