We can not delete Open (active AgentWork) AgentWork record from omni routing directly. So to delete this active AgentWork record we need first change workItem (Case,Lead) status from omni active status to inactive status and remove workitem from omni and delete it.
Here we change case as workitem status from active status (InProgress) to inactive status (new) by updating case record.
List<Case> caseListToUpdate = new List<Case>();
Set<Id> caseIdSet = new Set<Id>();
for(AgentWork agw : [Select Id,WorkItemId,Status from AgentWork where WorkItemId=' ']){
if(agw.Status == 'Opened'){
Case cse = new Case(Id = agw.WorkItemId);
cse.status = 'New';
caseListToUpdate.add(cse);
}
caseIdSet.add(agw.WorkItemId);
}
//Remove Case from omni and
if(!caseListToUpdate.isEmpty()){
database.update(caseListToUpdate,false) ;
}
if(!caseIdSet.isEmpty()){
// delete PendingServiceRouting record
database.delete(new List<PendingServiceRouting>([Select Id,WorkItemId from PendingServiceRouting where WorkItemId In:caseIdSet]),false);
// delete AgentWork
database.delete(new List<AgentWork>([Select Id,WorkItemId from AgentWork where WorkItemId In:caseIdSet]),false);
}
No comments:
Post a Comment