I have built LWC component to execute a batch job via apex controller. In the finish method, the batch is returning the id of a record inserted. I want to display that id on my LWC. How can I pass the id from finish method to the LWC?
2 Answers
Technically you can register a Platform Event on LWC using lighting EMP Api. And then in finish method fire a platform event with the record Id and then display that on UI.
But not sure of the use case, so this is just a suggestion
Also be sure to have a limits check, because only max of 2000 concurrent users can be subscribed to Streaming api. Check limits here
There are a few ways you can pass the id of the record inserted by the batch job to your LWC component. One approach is to use the @api decorator in your Apex controller to expose a public method that returns the id of the record. Then, in your LWC component, you can use the @wire decorator to call the Apex method and get the id of the record.
Here's an example of how you could implement this approach:
// Apex controller
public with sharing class MyController {
@AuraEnabled
@Api
public static Id getRecordId() {
// get the id of the record inserted by the batch job
return myRecord.Id;
}
}
// LWC component
import { LightningElement, wire } from 'lwc';
import getRecordId from '@salesforce/apex/MyController.getRecordId';
export default class MyComponent extends LightningElement {
@wire(getRecordId)
recordId;
// ... other component code goes here
}
In this example, the getRecordId Apex method is exposed as a public method using the @Api decorator. The LWC component uses the @wire decorator to call this method and get the id of the record inserted by the batch job. The recordId property of the LWC component is then automatically populated with the id of the record, which you can use to display it in your component.
Another approach is to use a custom event to pass the id of the record from the Apex controller to the LWC component. In your Apex controller, you can create and fire a custom event that contains the id of the record as a parameter. Then, in your LWC component, you can listen for the custom event and retrieve the id of the record from the event object.
Here's an example of how you could implement this approach:
// Apex controller
public with sharing class MyController {
@AuraEnabled
public static void finishMethod() {
// get the id of the record inserted by the batch job
Id recordId = myRecord.Id;
// create and fire a custom event
MyCustomEvent event = new MyCustomEvent();
event.setParam('recordId', recordId);
event.fire();
}
}
// LWC component
import { LightningElement, track } from 'lwc';
export default class MyComponent extends LightningElement {
@track recordId;
connectedCallback() {
// listen for the custom event
this.template.addEventListener('myCustomEvent', this.handleEvent);
}
handleEvent(event) {
// get the id of the record from the event object
this.recordId = event.detail.recordId;
}
// ... other component code goes here
}
In this example, the finishMethod Apex method creates and fires a custom event called myCustomEvent that contains the id of the record as a parameter. The LWC component listens for this event and retrieves the id of the record from the event object when it is fired. The recordId property of the LWC component is then updated with the id of the record, which you can use to display it in your component.