Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Screenshot showing the code snippet for programmatically closing a Salesforce Dispatcher Console custom action window
LWC

Close your Dispatcher Console custom action programmatically

Build a custom action for the Dispatcher Console and the modal tends to sit there after your logic finishes. Standard JavaScript won't close it, though one postMessage call to the parent frame will.

The short answer

Run parent.postMessage('closeLightbox', '*') from your Visualforce page to close a Field Service Dispatcher Console custom action modal. window.close() fails because the page sits inside an iframe and has to talk across frames to dismiss the container.

Key takeaways Call parent.postMessage('closeLightbox', '*') from your Visualforce page to close the Dispatcher Console lightbox. Skip window.close() and page redirects. The custom action runs in an iframe that cannot shut its parent container. Finish your DML and record updates before you send the close message, or you lose the unsaved data. Pass the string 'closeLightbox' exactly. The listener the Dispatcher Console uses is case sensitive.

Closing your Dispatcher Console custom action programmatically

If you build for Field Service, you have probably had to write a Dispatcher Console custom action for some piece of business logic. Most of us reach for a Visualforce page because it gives us room to move. Then the logic finishes and the modal hangs there, staring back at the dispatcher, who now has to close it by hand.

You cannot use a standard window.close() here. The Visualforce page runs inside an iframe within the soql-in-loops-security-review-impact-for-managed-packages/" class="auto-link">managed package, so it has no authority to close its own container. You have to talk to the parent frame.

That is what postMessage is for. Your page sends a signal to the Dispatcher Console telling it to pack up the lightbox, and the lightbox goes away. One line does it.

parent.postMessage('closeLightbox', '*');

Common pitfalls with a Dispatcher Console custom action

The first time I worked on one of these I tried everything to get that window to shut. I've seen teams redirect to a "success" page or build out window hierarchies, and the dispatcher still ends up clicking the X. Most teams get this wrong because they treat the modal like a standard browser popup.

The Dispatcher Console is a large application, and it is listening for very specific events. Send anything other than the exact string 'closeLightbox' and nothing happens. It works the way LWC component communication does elsewhere in Salesforce, even though we are technically in a Visualforce context here.

Pro tip: save your data before you send the close message. There's nothing worse than a dispatcher thinking a job is done when the record never actually updated because the window closed too fast.

This matters because dispatchers are handling dozens of service appointments at once. An extra click on every custom action is friction you are adding to their day for no reason.

When to trigger the close event

  • After a successful DML operation in your controller.
  • When the user clicks a "Cancel" or "Done" button on your page.
  • Once a specific navigation step is completed.

Why postMessage is the right tool

postMessage is the safe way to handle cross window communication. Your Visualforce page and the Dispatcher Console may sit on different subdomains, and browsers block direct script access across them for security reasons. postMessage gets around the Same-Origin policy by sending a signal the parent frame is already waiting for.

If you are weighing code against standard tools for extensions like this, it helps to think about the complexity you are taking on. I usually check the current Apex vs Flow guidelines, though for the Dispatcher Console, Visualforce and custom actions are still the standard for deep UI work.

Key takeaways

  • Standard window.close() does nothing inside the Dispatcher Console lightbox.
  • Use parent.postMessage('closeLightbox', '*') to trigger the close event.
  • Finish your background processing before you send the message.
  • The string 'closeLightbox' is case-sensitive and has to be exact.
  • Test the custom action to be sure the UI refreshes once the modal disappears.

Your job is to make the dispatcher's day easier. Try it on your next FSL project. It is one line, and the dispatcher stops closing your window by hand.

Frequently asked questions

How do you close a Dispatcher Console custom action modal programmatically?

Call parent.postMessage('closeLightbox', '*') from the script on your Visualforce page. That sends the event the parent Dispatcher Console frame needs to dismiss the lightbox.

Why does window.close() not work in a Dispatcher Console custom action?

The Visualforce page runs inside an iframe within the managed package, so it has no authority to close its parent container. Browser Same-Origin policies also block direct script access across differing subdomains, which is why postMessage is the way through.

When should you trigger the closeLightbox message in a custom action?

After a successful DML operation in your controller, once a navigation step finishes, or when the user clicks Cancel or Done. Let every backend save finish before you send the event.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment