+ Start a Discussion
Tolga SunarTolga Sunar 

Test Class for Apex Trigger defined on User Service Presence object

I have created an apex trigger that is defined on User Service Presence object, but I have no idea how to get even one line of it covered for deployment. This trigger simply detects when an user logins or logouts to a specific omni-channel presence, and updates a field on User object. Since User Service Presence object does not support DML calls and -as far as I know- it is not possible to simulate omni-channel presence login / logout event in a testmethod, I cannot cover any line of this trigger.

Any information about a workaround or a trick is greatly appreciated.
Sandeep PatwardhanSandeep Patwardhan
can you share your trigger code?
Tolga SunarTolga Sunar
Trigger's below:
 
trigger PGS_CTISignal on UserServicePresence (after insert, after update) {
    
    List<ServicePresenceStatus> spsTelefon = [SELECT Id, MasterLabel FROM ServicePresenceStatus WHERE MasterLabel='Telefon'];
    string spsTelefonId;
    if (!spsTelefon.isEmpty()) {
      spsTelefonId = spsTelefon[0].Id;
    }
    
    User u = [SELECT Id FROM User WHERE Id=:system.UserInfo.getUserId()];
    
    for (UserServicePresence usp : Trigger.New) {
        if (Trigger.isInsert) {
            if (usp.ServicePresenceStatusId == spsTelefonId) {
                u.Is_Current_State__c = true;
                u.Service_Presence_Status__c = 'Telefon';
            }
        }
        if (Trigger.isUpdate) {
            if (usp.ServicePresenceStatusId == spsTelefonId && usp.StatusEndDate!=null && Trigger.OldMap.get(usp.Id).StatusEndDate==null) {
                u.Is_Current_State__c = false;
            }
        }
        update u;
        
    }
}

 
Sandeep PatwardhanSandeep Patwardhan
see if this helps..
https://developer.salesforce.com/trailhead/en/apex_testing/apex_testing_triggers
 
Tolga SunarTolga Sunar
Thanks for your help, but I'm already quite proficient in test classes. I need help in how to "trigger" the Apex trigger that is defined on an object that does not support any DML operations, and mocking it seems impossible.
Keith WordKeith Word
Tolga,
Were you ever able to to get the code working and appropriate test class created?  I have a similiar need.  My original intent was to create a workflow on the User Presence object, however, it appears that it is unavailable.  I am now attempting to write a trigger to accomplish the same actions.  I would appreciate any knowledge \ code that you are willing to share.  Thanks.
Tolga SunarTolga Sunar
Unfortunately I couldn't manage to prepare a testclass for such trigger. I did not push it too far since the plan that required this trigger was scrapped.