If you follow the example in the lightning:empApi
documentation, then you will observe that when you are subscribed to an event, the event data is stringified as JSON.
// Subscribe to an event
empApi.subscribe(channel, replayId, $A.getCallback(eventReceived => {
// Process event (this is called each time we receive an event)
console.log('Received event ', JSON.stringify(eventReceived));
}))
.then(subscription => {
...
});
The above snippet prints as following, where you will observe that any Custom Fields defined on your Platform Event are present in the payload
attribute.
{
"data": {
"schema": "xxxx",
"payload": {
"My_Custom_Field1__c": "value",
"My_Custom_Field2__c": "value",
"CreatedById": "xxx",
"CreatedDate": "xxx"
},
"event": {
"replayId": 11
}
},
"channel": "/event/My_Event__e"
}
Once you have this JSON, you can always retrieve the custom field values defined on your Platform Event from there. Your subscribe()
function will then look like as:
empApi.subscribe(channel, replayId, $A.getCallback(eventReceived => {
// Process event (this is called each time we receive an event)
console.log('Received event ', JSON.stringify(eventReceived));
// parse the JSON
var jsonRes = JSON.parse(JSON.stringify(eventReceived));
// retrieve the custom field values now
var myCustomField1 = jsonRes.data.payload.My_Custom_Field1__c;
}))
.then(subscription => {
...
});