SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Friday 1 May 2020

How to display warnings in Salesforce ? | Become an #AwesomeAdmin using Platform Event Toast LWC

Hello Trailblazers,

I recently created a new generic toast component named as "Platform Event Toast LWC" which can be used to display warnings in Salesforce. I open sourced it on github and it can be accessed on this link:- https://github.com/rahulmalhotra/Platform-Event-Toast-LWC

Have you ever faced a requirement where you need to show warning message to the user who is trying to save a record but you also want that the record should be saved successfully without any restriction ?

In this post, we're going to learn how we can show warnings in Salesforce using Platform Event Toast LWC and take one step forward to become an #AwesomeAdmin.

The Platform Event Toast can be installed as an unmanaged package in any developer/sandbox/production environment by clicking on the links below:-

  1. Developer/Production Environment:- https://login.salesforce.com/packaging/installPackage.apexp?p0=04t7F000005ZBvB
  2. Sandbox Environment:- https://test.salesforce.com/packaging/installPackage.apexp?p0=04t7F000005ZBvB

Use Case

Let's say you wanna show a warning to the user who is updating the Annual Revenue of the account. The new annual revenue is >= $50,000 but the user forgot to update the rating to "Hot". In this scenario, the client doesn't want to restrict the user from saving the record but the user should know that the preferred rating for this account is "Hot"

Our AWESOME Salesforce Admin can fulfill this requirement using Platform Event Toast. All he has to do is to create a process builder on Account as shown below:-

Process Builder will run when an account record is created or edited as we want to show warning when a record is updated by the user.


We're setting the criteria that all of the conditions are met and we've added two conditions:-

  1. Annual Revenue >= $50,000
  2. Rating != Hot

We'll execute the process builder only when the specified changes are made to the record as we want to show warning only once.

AccountProcessCriteria.jpg (1366×628)

At the end, we added an action where we're creating a Toast Event record by setting up properties of toast as shown below:-



Note:- Key is a required field and must be filled. A key is used to uniquely identify a toast event when it's displayed on a page. Moreover, it'll help you as an admin to debug and find the process builder very easily if you're setting up the key as:- ProcessBuilderName_ConditionName as a key should be unique.

Once your process builder is ready and activated, it's time to add our component to the lightning page. We just have to make sure that we're adding the same key here as we specified in the process builder:-



We can leave the other fields empty for this toast as we've setup everything from the process builder. In case, we haven't specified value of other fields in the process builder, we can fill those fields here. Note that if you've added values in the process builder and in the component as well the values from process builder have higher priority and will override the values set in the component.

Once you've setup the toast successfully, you'll see the below result when Account's Annual Revenue >= $50,000 and Rating != Hot



This is just a single use case. However, there can be multiple use cases where you can use this toast. You also have an option as:- Run in System Mode which will allow a toast to run in system mode i.e. the toast will be visible to all users of the org if the toast exists on the page that is opened by the user. This option can be used to create Org Wide Announcements by adding the toast as a part of utility bar so that it's available at application level no matter which page is opened by the user.

If you want to know in detail about the Run in System Mode feature, let me know in the comments down below and I'll write another blog on the same.

Considerations while using Platform Event Toast

Let's have a look at linkedin post below that I published when I created this Platform Event Toast:-



As you can see linkedin post above, there is a comment by René Winkelmeyer @muenzpraeger who is an Architect, Developer Evangelism at Salesforce. He highlighted that the Platform Event Toast will work for small/medium size orgs, but may cause issue in large organizations where we have a large number of users or we're using Platform Events at various places for different purposes. As platform events work in real time, they've limitations on the number of event notifications, concurrent subscribers in a 24 hour period. You can have a detailed look at the limits here:- https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_event_limits.htm

Thanks René for highlightning this 😊

You can also have a look at the video for the same below:-



If you liked this post, make sure to share it in your network and let me know your feedback in the comments down below.

Happy Trailblazing..!!

22 comments:

  1. I tried to implement this for community page, my scenario is to display custom toast message whenever user creates a Case. Looks like it is not working for community users.

    ReplyDelete
    Replies
    1. Hi Hemanth, have you placed the lightning component on the community page?

      Delete
  2. Hi Rahul, I am using it to detect duplicate Opportunity record. my process+flow is working fine and creating platform event record successfully. But I am not able to see toast message on Opportunity record. I have one more question, what if 2 Opportunity records are open in 2 tabs. how it will detect on which Opportunity record page to show toast or not? thanks

    ReplyDelete
    Replies
    1. To answer your question, the platform event toast by default runs in user mode, so, if you've two opportunity records in two different tabs and you've the component embedded on the opportunity detail page, you'll see both the tabs with toast, if you want to display only on a particular record page, you can send the record id as "key" from the process builder and you need to update the platform event toast js file a little so that it checks for that particular key and compare it with the current record id. Although, it's a good use case, I'll try to publish an update with record id included asap.

      Delete
  3. In continuation to my last comment : this is error I see on console :
    "Error in Platform Event Toast" which is from lwc component. trying to find root cause.

    ReplyDelete
    Replies
    1. Hi, you must be getting the error details as well along with the error in platform event toast. Can you share those?

      Delete
    2. My bad, as i have to create Platform event manually, api name was wrong for event. Its fixed. working as expected. Thanks for prompt reply.

      Delete
    3. Ohh..okay, you can install the unmanaged package otherwise so that everything is created and ready for use.

      Delete
  4. HOW TO TRIGGER: A user friendly validation error message from record triggered flow?

    ReplyDelete
    Replies
    1. It'll follow the same process as we're doing in case of process builder

      Delete
  5. This is fantastic!!! 😍 *cries of nerd happiness* 😭
    And it was actually easy to set up and worked on the first try!
    I just wonder, is there any way to make the warning last longer? They disappear so quickly!

    ReplyDelete
    Replies
    1. Hi Merethe, so happy to know that you liked it. You can choose the toast mode as sticky and it'll persist until the user clicks close button.

      Hope it helps!

      Delete
    2. Aaah, that's beautiful!! 😍 Is there any documentation on the different possibilities here? (If there are any other ways to modify the message, like maybe placement, size or color. Although that's pretty perfect as is! I'm just curious.)

      Delete
    3. Platform event toast is using lwc toast event. You can find the documentation for the same here: https://developer.salesforce.com/docs/component-library/bundle/lightning-platform-show-toast-event/documentation

      Delete
    4. Great! I will read up on that (thought it seems a bit too difficult for my level).
      I have some input for you, if you want to make your instructions clearer for the self-taught people with no IT background like me :P
      I added everything to my object, BUT it is a child object usually edited via a Contact. And when you do that, the warning doesn't trigger until you've edited directly on the child object first. And then for some reason it works via Contact also. But I tried to add the Platform even toast to the Lightning page layout of Contact also, and now it seems to work consistently! 😁 Which makes sense, but it took me a while to think of it.

      Delete
    5. Thank you for the input Merethe, also the doc I shared is pretty simple. You can just have a look at the details of mode and variant parameter under the parameters table and that should answer your queries.

      Delete
    6. Hi again :) I got some feedback from my teammates that a PB is not ideal for this use, as we're fazing out PBs. Is there any way to do this via a flow? I tried making a flow with the same setup as the PB, and Debug shows it triggers. But there is no toast message popping up. Is it just me who has done something wrong, or can't Toast events be created via Flow?

      Delete
    7. Toast events can be created via flow. I am just not getting enough time to update the documentation. You must be missing something, can you email me the details? I can help there: rahul@sfdcstop.com

      Delete
    8. Thanks for offering to help! 😊 I actually think I figured it out on my own! I used an Assign and a Create element originally, and then realized I can do the assigning inside the Create element. And for some reason that worked (even though I thought that was supposed to be same same).

      Delete
    9. I agree, maybe you were missing something small but anyways, I am glad it worked!

      Delete
  6. This is fantastic!!! 😍*Cries of nerd happiness* 😭
    And it was really easy to implement! It worked on my first try!
    I just have one question: Is there any way to make the warning stay longer so the users have time to read properly?

    ReplyDelete
    Replies
    1. Hope you found out the sticky mode. I'm replying for everyone: you can choose the toast mode as 'sticky' instead of 'dismissible' which will make the toast appear until you (user) clicks the close icon.

      Delete