1

I haven't been able to figure out how to access a Custom Field on a Platform Event within an Aura Component js callback handler, does anyone know how I can do this, or where it exists in the documentation? I haven't been able to find anything.

Thanks in advance.

3
  • 1
    Can you please edit your question to include the code you've tried so far? Even if it's not correct/working, it would help us diagnose the problem with you.
    – sfdcfox
    Apr 22, 2019 at 14:39
  • I haven't tried any code because I flat out don't know how to do it, I haven't been able to find any documentation anywhere.
    – nicolevy
    Apr 22, 2019 at 16:07
  • @nicolevy I have the details added as an answer as how you can achieve this if you subscribe to events in an aura component as in the documentation. See if that helps.
    – Jayant Das
    Apr 22, 2019 at 16:34

1 Answer 1

1

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 => {
    ...
});
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .