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){ AgentWorkTriggerHandler.updateCases(trigger.new); } }
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') { mapCaseIdToAcceptedBy.put(aw.WorkItemId,aw.UserId); } } if(!mapCaseIdToAcceptedBy.keySet().isEmpty()){ 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
@IsTest public class AgentWorkTriggerHandlerTest { static String prospectRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Prospect').getRecordTypeId(); static String mdmTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('DOC').getRecordTypeId(); @isTest private static void agentHandlerTest(){ AgentWorkTriggerHandlerTest.setup(); Test.startTest(); // System.assertEquals([Select ID,WorkItemId,ServiceChannelId FROM AgentWork ],null); Test.stopTest(); } 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; caseRec.Quotes_Required__c=2; caseRec.Existing_Quotes__c='test'; insert caseRec; ID SChannelId= [SELECT Id FROM ServiceChannel Where DeveloperName='Service_Case_Channel' Limit 1].Id; User user1 = new User(); user1.id = UserInfo.getUserId(); System.runAs(user1){ 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 !!
Answers:
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; break; }catch(Exception e) { System.debug(LoggingLevel.INFO, 'Service channel with id '+sc.Id+' is invalid'); } }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0