Not able to create agentWork record in test class

I am working on the test class where I stuck up on the creating the test record.

Trigger Code

trigger AgentWork on AgentWork (after insert, after update, before insert, 
before update) {
 if(trigger.isUpdate && trigger.isAfter){

Handler Code

public class AgentWorkTriggerHandler {
    public static void updateCases(List<AgentWork> newList){
        Map<Id,Id> mapCaseIdToAcceptedBy = new Map<Id,Id>();
        for(AgentWork aw : newList){
            String wiId = aw.WorkItemId;
            //if the object is case
            if (wiId.left(3) == '500' && aw.Status == 'Opened') {    
            Map<Id,User> mapUser = new Map<Id,User>([SELECT ID,Name FROM User WHERE ID IN: mapCaseIdToAcceptedBy.values()]);
            Map<Id,Case> mapIdToCase = new Map<Id,Case>([SELECT ID,Status,Case_Owner__c FROM Case WHERE ID IN: mapCaseIdToAcceptedBy.keySet()]);

            for(Id caseId : mapIdToCase.keySet()){
                Case c = mapIdToCase.get(caseId);
                c.Status = 'In Progress';
                c.Case_Owner__c = mapUser.get(mapCaseIdToAcceptedBy.get(caseId)).Name;
            update mapIdToCase.values();

Test Class

public class AgentWorkTriggerHandlerTest {
    static String prospectRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Prospect').getRecordTypeId();
    static String mdmTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('DOC').getRecordTypeId();

    private static void agentHandlerTest(){
        //   System.assertEquals([Select ID,WorkItemId,ServiceChannelId FROM AgentWork ],null);

    private static void setup(){
        Account acclstPrnt = Test_Util.createAccount(false,'AccountDEL', 'Lokhanwala' , 'Mumbai' , 'India' , null);
        acclstPrnt.RecordTypeId = prospectRecordTypeId;
        insert acclstPrnt; 
        Opportunity tstoppty=Test_Util.createOpportunity(acclstPrnt.ID);
        insert tstoppty;
        Case caseRec = new Case();
        caseRec.RecordTypeId = mdmTypeId;
        caseRec.Subject = 'testCase';
        caseRec.Description = 'TTB0333333';
        caseRec.OwnerId =  UserInfo.getUserId(); 
        caseRec.Status = 'Work-in-Progress';
        caseRec.Opportunity__c= tstoppty.ID;
        insert caseRec;
        ID SChannelId= [SELECT Id FROM ServiceChannel Where DeveloperName='Service_Case_Channel' Limit 1].Id;
        User user1 = new User(); = UserInfo.getUserId();
            AgentWork awork = Test_Util.createAgentWork(caseRec.Id,UserInfo.getUserId(),SChannelId,true);
            awork.WorkItemId = caseRec.Id;
            update awork;}

I am getting Error : System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The agent’s status is not associated with the channel for this work.: [ServiceChannelId]

Thanks !!


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It seems that the issue is when updating AgentWork.

AgentWork awork = Test_Util.createAgentWork(caseRec.Id,UserInfo.getUserId(),SChannelId,true);

The SChannelId that you query and set while creating AgentWork might not be valid for the case status that you specify.

Method 2

The error that you mentioned is also thrown when the UserId associated with the concerned AgentWork item belongs to someone who has Offline or Busy status at the moment. Adding Agentwork items means populating the Omni-Channel. As intuition suggests, you can’t populate a user’s Omni-Channel if the user is not online in the first place.

Hope this helps 🙂
Let me know if it does!

Method 3

Try this ugly code, it worked for me.

aw is an AgentWork instance created for the test.

    for (ServiceChannel sc : [select Id from ServiceChannel limit 10]){
        try {
            aw.ServiceChannelId = sc.Id;
            insert aw;
        }catch(Exception e) {
            System.debug(LoggingLevel.INFO, 'Service channel with id '+sc.Id+' is invalid');

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x