3

I am using UserServicePresence object in Apex to retrieve the user status on Omni-Channel and I get the users to perform further logic. It works fine.

Apex Class code snippet:

//Get the users who have the Omni Channel Status as online
List<UserServicePresence> onlineStatusList = [ select Id,UserId from UserServicePresence where IsCurrentState = true  and ServicePresenceStatus.DeveloperName = 'online' ];

if(onlineStatusList != null && onlineStatusList.size() > 0)
{
    // do some stuff here
}

However, I am not able to find a way to setup the test data for the UserServicePresence object in my test class in order to get the required test coverage. The UserServicePresence object is not writeable and it won't allow us to insert any test records in to it. So, whenever I test the class, the onlineStatusList would always have 0 rows. And there is a major portion of my code inside the if block which I am not able to cover using the test class.

Please let me know if there is a way to test out the Omni-Channel status in the Apex test classes.

  • 1
    I don't have the answer but I suggest you put some code here so somebody else might be able to help (relevant parts from both Class and Test class would be even better) – Sebastian Kessel Feb 15 '16 at 22:52
  • Thanks for the suggestion Sebastian. I have added a code snippet for the Apex class. As I am not sure how to set the test records for the Omni-Channel objects ( as those are not writeable ), I am not able to provide the code for the test class. – Karthik Feb 16 '16 at 4:37
  • have you tried using System.runAs() method? – Axaykumar Varu Feb 16 '16 at 7:29
  • Thanks for the comment @AxaykumarVaru. However, I am not sure how that would help in this scenario, as the data returned by the SOQL query would always be same ( no records ) irrespective of the user running it. There would be data only if a user is currently logged in to Omni-Channel ( which sets the IsCurrentState to true) and set their status as 'online' ( online in this case, as I am looking for that status in the SOQL). – Karthik Feb 23 '16 at 1:26
1

I've got the same problem and I solved it using Test.isRunningTest() in the if condition. So you can do something like this:

if((onlineStatusList != null && onlineStatusList.size() > 0) || 
Test.isRunningTest())

It's not a "clean" solution but it works.

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.