Menu
subscribe our youtube channel popup

Messaging for InApp and Web (MIAW)

Salesforce provides many objects around the Messaging functionality related to the OmniChannel. Through this post, I will focus on only two objects to keep it simple in reaching the goal of this article. Join us to learn about Messaging for InApp and Web (MIAW) How to retrieve the logged in user details without ‘User Verification’.

  1. MessagingEndUser: Messaging end user records represent a link between a messaging channel and a user.
  2. MessagingSession: Represents a session on a Messaging channel.

(Source: Salesforce)

Messaging for InApp and Web (MIAW)

The OOTB behavior is that the MessagingEndUserId field will be populated with the Guest user Id, instead of the actual site user who initiated the chat message. The ‘User Verification’ option will not work with experience sites yet. For now, it is only for the external websites and the mobile apps.

Then how to retrieve the Id of the experience site user who initiated the chat?

Well, there is a simple and intuitive workaround. Salesforce provides a way to retrieve the logged-in user based on the component type the user is interacting with, such as VF page, Aura, and LWC. The MIAW is built on the Aura framework, Hence, we use ‘$SObjectType.CurrentUser.Id’  to retrieve the userId.

In this illustration, I am assuming that the MIAW is enabled in the experience site. If you are reading this, I assume that you already have knowledge on Messaging Settings, Embedded Service Deployments, Pre-chat and Hidden fields. If not, I will explain them in another write-up. Here are the steps to achieve the purpose of this article

Step 1: Add Custom Parameter

Setup – Messaging Settings – Your Messaging Channel – Custom Parameters. Create a custom parameter as shown below

Step 2: Add Parameter Mapping

The parameters mapped here are available in the Omni-channel flow

Step 3: Update the OmniChannel flow

Head to the Omni-channel flow which you are using for routing or any custom logic execution. Add a Get element and Select User object. The UserId field we setup in the above steps will be available here. Use this Id to update the associated MessagingSession and the MessagingEndUser records.

You might be wondering, how the UserId field is fetching the experience site logged in UserId. You will find the answer in next steps which fulfills the objective from the backend.

Step 4: Select the Hidden field

Setup – Embedded Service Deployments – PreChat

Under the available hidden field section you will find UserId which we just created. Add it to the Selected field section

Step 5: Add Custom JavaScript to the experience site

All Sites –  Builder – Settings – Advanced – Header Markup. Add the following code snippet

<script>
    var userId;

function callPrechatAPI() {
    // get UserId of the logged in Experience Site User
    userId = $A.get('$SObjectType.CurrentUser.Id');
    console.log('Passing UserId = userId (currently Logged In User Id, or ' + userId + ')');
    // Send it!
    embeddedservice_bootstrap.prechatAPI.setHiddenPrechatFields({
        "UserID": userId
    });
}
//just incase
function trapButtonClick() {
    console.log('Looking for embeddedMessagingConversationButton...');
    var b = document.getElementById('embeddedMessagingConversationButton');
    if (b != null) {
        console.log('Found it, attaching to onClick event');
        b.addEventListener('click', callPrechatAPI);
    } else {
        console.log('No button yet; wait a second and try again');
        setTimeout(trapButtonClick, 1000);
    }
}
window.addEventListener("onEmbeddedMessagingReady", () => {
    console.log('Received the onEmbeddedMessagingReady event...');
    callPrechatAPI();
});
window.addEventListener("load", trapButtonClick); 
</script>

Now, It makes sense why I discussed the ‘$SObjectType.CurrentUser.Id’  above.

Voila!!, log in to the experience site and verify the MessagingEndUserId value is populated.

Anand Dosapati
Anand Dosapati

Salesforce Solution Architect

Articles: 11

16 Comments

    • Hi Vinod,

      Yes, In the step 3 Omni Channel flow we already retrieving the user record meaning all the information is available.

      • Hi, I want that instead of guest user the messaging user shows the name of actual portal user who started the chat. I tried to update the ContactId and Name on the Messaging user object related to the messaging session record in the omni flow. But it still shows as Guest user and contactid is not being populated. Can you help?

  1. I am confused, does the Messaging Settings – Messaging option need to be enabled? I see all the sObjects that are supposed to be created with this enabled. I am in a developer org.

    • Yes, Darrell Gallegos,

      Yes, the Messaging Settings toggle button should be enabled and a New Channel should be created.

      Navigation: Setup – Messaging Settings – Toggle button ON
      New Channel button – Follow the On-screen instructions

  2. Hi,

    Would this only work if we have log in functionality enabled on our experience site? or can it work without it as well?
    Because we dont have it enabled and I am getting undefined when I click it.

  3. How can we embed MIAW on experience site using LWC ? In Salesforce documentation, it’s mentioned to drag and drop standard ‘Embedded Messaging’ component on page and put rest of the logic in head markup.
    My use case is that , I need to launch the chat on a case detail community page.

  4. I’m building a Agentforce on community where I need to fetch community user id and based on that ID, I need fetch list of oppurtunity and case based. How I’ll use userId in those flows?

    Please help

  5. This is excellent and thanks a ton. Thank you for the detail steps and explanation.
    This worked flawlessly. Greatly appreciate your help.

  6. This is excellent and thanks a ton. Thank you for the detail steps and explanation.
    This worked flawlessly. Greatly appreciate your help.

  7. BTW, i tried to update the MessagingEndUserId(Messaging User ID), on the messaging session record with the userid using the flow, but that field is not accessible in the flow. On the messaging session record it still shows as guest.
    Is it possible to update the MessagingEndUserId to the partner community user?

  8. Similar comments to Balaji Palakurthy and Neha, in that this is great but its not clear what we can do after the GetRecords so the chat does not show as “Guest User”?

    Assuming we set up the hidden field, do the parameter mapping, have the script in the head, and have the omni-flow structured correctly with the GetRecords element, what do we do next? How do we get the messagingenduser updated to reflect the logged-in community user whose information we just collected?

  9. I have an error: $A is not defined in the console log. If anyone finds a solution, please help me out.

Leave a Reply

Your email address will not be published. Required fields are marked *