Salesforce Technical Guides

Capturing Real-time Record Updation Using LWC
Introduction
In modern CRM ecosystems, real-time Salesforce integration and seamless user experiences are no longer optional but fundamental for driving operational efficiency. Imagine your sales reps making important Opportunity changes, but the ERP remains out of sync, leading to confusion and data errors. We understood the necessity to bridge this data gap and implemented a quick action-driven integration between Salesforce and an external ERP system via MuleSoft (Microsoft Business Central). Continue reading to know how we leveraged Platform Events, Lightning Web Components (LWC), and a lightweight Salesforce polling mechanism to ensure sales representatives stay informed directly on the record page – eliminating the need for manual refreshes.
The Business Challenge: Keeping Sales Data in Sync
The sales representatives frequently make changes to Opportunity records in Salesforce, which need to be promptly reflected in the external ERP system, Microsoft Business Central. This made our objectives clear:
- Allow users to initiate sync manually via a Quick Action.
- Notify the ERP system (via MuleSoft) of the Opportunity sync request using Platform Events in Salesforce.
- Provide transparency by updating a Salesforce Sync Status Updates field based on the external system’s response.
- Offer Rich text Component displaying sync feedback.
- Ensure the sales representative remains on the same page without any reloads or redirection.
Our Technical Solution: Real-Time Updates with LWC and Smart Polling
To meet these needs, we built a Lightning Web Component (SyncOpportunityToBC) that performs the following steps:
- Quick Action Initiation: A user-initiated Quick Action in Salesforce invokes the LWC, which then sends a Platform Event in Salesforce. This signals MuleSoft that a specific Opportunity record needs to be synchronised with Microsoft Business Central. This Quick Action has a conditional visibility which is controller by the Sync_Status__c. The button is only visible for values such as Not Synced, Out of Sync and Failed and gets hidden if the Sync is In Progress.
- Real-Time Status Polling: Once the sync request is initiated, the LWC starts a Salesforce polling mechanism on a custom field (Sync_Status__c). This polling periodically checks for status updates from the external system, which can be “In Progress”, “Completed”, or “Failed”.
- Visual Feedback via Rich Text Component: Based on the response from MuleSoft, the LWC dynamically updates a rich text component on the Opportunity record page, providing immediate feedback to the user about the real-time data sync in the Salesforce process.
- Seamless UI Refresh Without Page Reloads in Salesforce: Using notifyRecordUpdateAvailable() and @wire(getRecord), within the LWC, we ensure that the data displayed on the record is refreshed in place, giving a real-time user experience.
Polling Mechanism: Real-Time Without Streaming
While Salesforce offers options like Change Data Capture and Streaming API for instant Salesforce integration, these can often involve setup overhead or licensing constraints. For this specific, user-initiated sync flow, we chose a controlled polling strategy. This approach is simple to implement and highly effective for short-term, user-initiated sync flows.
How Polling Works
As soon as the Opportunity sync with ERP Systems request is sent via the Platform Event in Salesforce, the LWC starts polling the backend every 2 seconds (customizable via pollingInterval). It uses an Apex method (getOpportunitySyncStatus) to efficiently retrieve the current value of the Sync_Status__c field.
Here’s a simplified version of the polling logic:
Reasons Why This Works Well
- Lightweight Implementation: The process utilizes a straightforward Apex call, minimizing overhead.
- Near Real-time Updates: It achieves timely updates within a 6-minute max window (customizable via maxRetries).
- Cost-Effective: By this method, there’s no need for expensive external messaging or push-based architecture.
- Controlled and Robust: The polling process cleanly terminates on success or timeout.
Achieving Real-Time UI Refresh with notifyRecordUpdateAvailable()
Once a terminal sync status is reached (“Completed” or “Failed”), we use:
await notifyRecordUpdateAvailable([{ recordId: this.recordId }]);
This line of code notifies Salesforce’s Lightning Data Service (LDS) that the specific Opportunity record has been updated. As a result, all components wired to that record (e.g., using @wire(getRecord)) automatically re-fetch the latest data.
In our case, the rich text field that displays sync status is directly bound to the Sync_Status__c field. This ensures a seamless update in the user interface, without a page refresh.
- No manual intervention required
- No disruptive page reloads
- Just real-time updates
The Outcome: Tangible Benefits for Sales Teams
By implementing this real-time Salesforce integration solution, significant benefits were delivered:
- Seamless User Experience: Reps stay on the record page throughout the sync.
- Real-Time Visibility: Sync progress and results are shown almost instantly.
- Enhanced Productivity: No manual refreshing allows reps to focus on other important tasks.
- Decoupled and Efficient Integration: The combination of Platform Events in Salesforce and our controlled Salesforce polling mechanism enables clean, asynchronous operations between Salesforce and MuleSoft.
Conclusion
The implementation of a near real-time integration between Salesforce and an external ERP system underscores the essential role of efficient data synchronization in modern CRM ecosystems. By strategically leveraging Lightning Web Components, Platform Events, and a controlled polling mechanism, organizations can achieve significant improvements in operational efficiency, data accuracy, and user experience.
To explore the potential of implementing similar real-time integration solutions tailored to your organization’s specific requirements and technology landscape, connect with our experts today!