You need to sign in to do that
Don't have an account?
Eric Baniqued
Please help me in writing a test code for this
I'm no expert in writing apex codes and I've just got this code below to work in the sandbox environment. What this does is that it changes the case status to In Progress when an agent accepts a case from omni channel. I've tried to deploy it in production but its not letting me as I am getting an error for code coverage. I dont have any idea on how to write a test class for AgentWork as I can't find many detailed examples that I can relate my code and study to write it from scratch.
trigger Omni_Work_Status on AgentWork (after update) {
for ( AgentWork newAW : Trigger.new){
AgentWork oldAW = Trigger.oldMap.get(newAW.Id);
Boolean acceptedWork = oldAW.AcceptDatetime != newAW.AcceptDatetime;
if ( acceptedWork ){
String workitemId = newAW.WorkItemId;
try{
Case c = [SELECT Status FROM Case WHERE Id =: workitemId ];
c.Status = 'In Progress';
update c;
} catch (exception e){
//do nothing
System.debug('error message: '+ e.getMessage());
}
}
}
}
trigger Omni_Work_Status on AgentWork (after update) {
for ( AgentWork newAW : Trigger.new){
AgentWork oldAW = Trigger.oldMap.get(newAW.Id);
Boolean acceptedWork = oldAW.AcceptDatetime != newAW.AcceptDatetime;
if ( acceptedWork ){
String workitemId = newAW.WorkItemId;
try{
Case c = [SELECT Status FROM Case WHERE Id =: workitemId ];
c.Status = 'In Progress';
update c;
} catch (exception e){
//do nothing
System.debug('error message: '+ e.getMessage());
}
}
}
}
Create a test class
1)Insert a case
2)Insert a Omni_Work_Status by giving values in AcceptDatetime field and also WorkItemId field.Assign inserted case id to WorkItemId field.
3)update this Omni_Work_Status record by changing the value of AcceptDatetime.
4)Also cover exception by repating all the 3 steps and not assign any caseid to WorkItemId field ,So that exception block will be covered.
Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.
Thanks and Regards
Let me get back to you. Work calls. But Devi alludes to the basics.
Regards
Andrew
I found the below trailhead link which explains you what to do in the sense you would be able to write with a test class for a trigger after having a read.
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers
Also please have a look at the below articles:
>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
Kindly let me know if this is useful and also please close the thread marking a best answer so that it would be useful and it would help keep the community clean.
Regards,
Anutej Poddaturi
Regards
Andrew
Thank you for the example and for the revision of my code. I was able to understand it clearly along with the concept from Devi. They were all helpful.
I did follow your code example on the test class and made a few updates on it but I am getting a compile error message
Compile Error: Field is not writeable: AgentWork.AcceptDateTime at line 13
What does this mean?
Thanks!
Eric
It seems like AcceptDateTime field is a standard system field where it is automatically populated during record creation or if it is a formula field you cannot edit it ,Its value will be populated based on other field value.
If it is not a standard field or a formula field,Please check field level security of the field and make you have edit access to the field.
Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.
Thanks and Regards
Yep, Devi's response regarding that compile error is correct.
If you can check out that field an determine the FLS / editability it would be great.
Regards
Andrew
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_agentwork.htm
Usage
AgentWork records can only be deleted if they have the status Closed, Declined, or Unavailable. They can’t be deleted if their status is Assigned or Opened because they’re active in Omni-Channel.
AgentWork records have the status Assigned when they’re created. Once created, the record is automatically pushed to the assigned agent.
While the metadata for AgentWork indicates support for upsert() and update(), these calls aren’t used with AgentWork because none of its fields can be updated.
Apex triggers are supported with AgentWork.
So this issue may need some further thought.
Regards
Andrew
1.) What would be the syntax for creating AgentWork? seems that new AgentWork(); is not the correct way to create an AgentWork
2.) Would system.assertEquals also work in place of AcceptDateTime? value of dt for the positive test and null for the negative test?
This is basically my first apex code that worked in the sandbox and its saddening that I can't deploy it in the production environment due to code coverage.
Thanks!
Eric
I ran the test and it failed with an error message below:
ClassAgent: WorkTriggerTest
Method Name: test_update_positive
Pass/Fail: Fail
Error Message: 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]
Stack Trace: Class.AgentWorkTriggerTest.setup: line 14, column 1
looks like I need to specify the ServiceChannelId for this error
I think to save you some angst, the way around this is to perhaps try a Process Builder.
If I read the intent of your code, it is that once a piece of work has been accepted, update the related Case status to "In Progress"
First caveat, i don't have a lot of experience with Omni Channel - well, none, only what I have quickly read this last hour or so. And I don't have an environment with OmniChannel enabled.
So, create a Process Builder if you can on the Agent Work object.
Criteria would be that 1. Is Not Null for AcceptDateTime and 2. WorkItem starts with 005 (prefix for Case object). And set the advanced setting for "Do you want to execute the actions only when specified changes are made to the record?"
IA would be to update the related Case Status to "In progress"
note, this answer does hinge on being able to do a process builder on the Agent Work Object
This will alleviate the need for a test class in Apex.
Regards
Andrew
I'm working on the same topic and i'm facing the same error on the test class. Did you find any solution?
thanks
youssef