Salesforce Messaging Session record in Apex Test Class

Salesforce Messaging Session record in Apex Test Class

Sample Apex Code:

@isTest
private class MessagingSessionTest {
    
    testMethod static void testMessagingSession() {                
        
        User thisUser = [
            SELECT Id 
            FROM User 
            WHERE Id = :UserInfo.getUserId()
        ];
        
        System.runAs ( thisUser ) {
            
            MessagingChannel objMsgChannel = new MessagingChannel();
            objMsgChannel.MasterLabel = 'TestChannel';
            objMsgChannel.DeveloperName = 'TestChannel';
            objMsgChannel.IsActive = true;
            objMsgChannel.MessageType = 'Text'; 
            objMsgChannel.MessagingPlatformKey = 'TestChannel';
            insert objMsgChannel;
            
            MessagingEndUser objMsgUser = new MessagingEndUser();
            objMsgUser.Name = 'Messaging User';
            objMsgUser.MessagingChannelId = objMsgChannel.Id;
            objMsgUser.MessageType = 'EmbeddedMessaging';
            objMsgUser.MessagingPlatformKey = 'TestChannel';
            insert objMsgUser;
            
            MessagingSession objSession = new MessagingSession();
            objSession.MessagingEndUserId = objMsgUser.Id;
            objSession.MessagingChannelId = objMsgChannel.Id;
            objSession.Status = 'New';
            insert objSession;
            System.debug(
                'Id of the Messaging Session is ' + 
                objSession.Id
            );
            
        }               
        
    }
    
}

Leave a Reply