5

I've written the following trigger on AgentWork:

trigger AgentWork_BIBUAIAU on AgentWork (before insert, before update, after insert, after update) {

if(Trigger.isInsert) {

    if(Trigger.isBefore) {

    }else if(Trigger.isAfter) {

    }

} else if(Trigger.isUpdate) {

    if(Trigger.isBefore) {

    }else if(Trigger.isAfter) {
        updateLastSupportManagerOnCase(Trigger.New, Trigger.OldMap);
    }

}

private void updateLastSupportManagerOnCase(List<AgentWork> newAgentWorkList, Map<Id, AgentWork> oldAgentWorkMap) {

    // Detect agentWork records that switched to status Opened and map their CaseId to the support manager id.
    Map<Id, Id> caseIdToSupportManagerIdMap = new Map<Id, Id>();
    for(AgentWork aw : newAgentWorkList) {
        AgentWork oldAw = oldAgentWorkMap.get(aw.Id);
        if(oldAw.Status != 'Opened' && aw.Status == 'Opened') caseIdToSupportManagerIdMap.put(aw.WorkItemId, aw.UserId);
    }

    // Query the cases whose AgentWork record have changed to open.
    List<Case> casesToUpdate = new List<Case>();
    if(!caseIdToSupportManagerIdMap.isEmpty())
        casesToUpdate = new List<Case>([SELECT Last_Support_Manager__c FROM Case WHERE Id IN :caseIdToSupportManagerIdMap.keySet()]);

    // Update the last support manager field.
    for(Case c : casesToUpdate) c.Last_Support_Manager__c = caseIdToSupportManagerIdMap.get(c.Id);
    update casesToUpdate;

}

}

Now the problem is that I can't find any way to succesfully test this, not even to get 1% of coverage.

Manually inserting an AgentWork record would achieve at least some percentage of coverage which would allow me to at least deploy the trigger. But to do that I have to set myself programatically to 'Online' so that the insertion of the AgentWork record does not throw the following exception:

 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]

But since dml is not permitted on UserServicePresence that is currently not possible.

The second issue with this approach is that updating the AgentWork record is also not permitted which would then not allow me to test an 'before update' trigger.

Another approach that I have thought of is to let salesforce create the AgentWork record when I assign a case to my test user but when I do that manually via apex, there is no AgentWork record being created.

Any ideas on how the test could be done?

Thanks in advance.

  • see comments in following answer : I think comment made by Jelle Hendriks will help you out in some way : developer.salesforce.com/forums/?id=906F00000005KVYIA2 – Salesforce Steps Feb 22 '16 at 15:34
  • 2
    a general way to work around these issues is to test the handler method above by either (1) creating SObjects with Json.deserialize or (2) having your PROD code fetch SObjects from interfaces (the PROD interface gets them from the database, the test implementation of the interface gets them from instantiated sobjects. – cropredy Feb 22 '16 at 16:40
  • @NileshMendhe, Thanks for the link, but from what I understood they are stuck at the same point where you can't set yourself online/available programatically. – feddus Feb 23 '16 at 9:50
  • @crop1645, thanks! that's an interesting approach, I will look into it and give an update if I find a solution. – feddus Feb 23 '16 at 9:51
0

I solved this issue by adding a dummy field to the Object AgentWork then in the test class I selected a random one and update only that field. This will action will fire the trigger.

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.