I have after update trigger on Agent work and that fires when agent is declines the case in the omni channel by choosing the declining reason = 'Closed'. I have done development and everything is working fine.

When am writing test class for this trigger unable to create test data as below.

   AgentWork awork = new AgentWork();
    awork.WorkItemId = caseObj.Id;
    awork.ServiceChannelId = Label.Case_Service_Channel_Id;
    awork.UserId = Userinfo.getUserId();
    awork.DeclineReason = 'Closed';
    insert awork;   
    update awork;  

Saying Field is not writeable: AgentWork.DeclineReason error. Any idea how could i cover my apex class by writing the proper test data?


It's most likely that DeclineReason is a formula field based on the case object. Instead of trying to set it directly, make sure that the fields on the case are filled out correctly, insert the case and then insert the AgentWork Record.

There's a fair chance you don't even need to create a test AgentWork record. Chances are you can get SFDC to create them automatically by creating a case, assigning it to a rep, and then updating the case to closed with a given reason. SFDC should then create and insert the record thus firing your trigger and getting you coverage.

| improve this answer | |

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.