Skip to content

Commit eade911

Browse files
committed
feat: custom platform event template
1 parent 9a73b81 commit eade911

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/main/default/classes/StreamingMonitorController.cls

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ public abstract class StreamingMonitorController {
3232
'UriEventStream'
3333
};
3434

35+
private final static List<String> PLATFORM_EVENT_STANDARD_FIELDS = new List<String>{
36+
'replayid',
37+
'createddate',
38+
'createdbyid',
39+
'eventuuid'
40+
};
41+
42+
private final static Map<Schema.SOAPType, Object> DEFAULT_FIELD_VALUES = new Map<Schema.SOAPType, Object>{
43+
Schema.SOAPType.STRING => '',
44+
Schema.SOAPType.DATE => '2025-01-01',
45+
Schema.SOAPType.DATETIME => '2025-01-01T00:00:00Z',
46+
Schema.SOAPType.BOOLEAN => false,
47+
Schema.SOAPType.DOUBLE => 0
48+
};
49+
3550
@AuraEnabled
3651
public static void publishStreamingEvent(
3752
String eventType,
@@ -85,6 +100,31 @@ public abstract class StreamingMonitorController {
85100
];
86101
}
87102

103+
@AuraEnabled
104+
public static Map<String, Object> getBlankPlatformEvent(String eventName) {
105+
// Get object fields
106+
Schema.DescribeSobjectResult[] results;
107+
try {
108+
results = Schema.describeSObjects(new List<String>{ eventName });
109+
} catch (Exception e) {
110+
throw new StreamingException(
111+
'Unknown platform event type: ' + eventName
112+
);
113+
}
114+
Map<String, Schema.SObjectField> fields = results[0].fields.getMap();
115+
// Remove standard fields
116+
Set<String> fieldNames = fields.keySet();
117+
fieldNames.removeAll(PLATFORM_EVENT_STANDARD_FIELDS);
118+
Map<String, Object> event = new Map<String, Object>();
119+
// Add default values for fields
120+
for (String fieldName : fieldNames) {
121+
Schema.DescribeFieldResult dfr = fields.get(fieldName)
122+
.getDescribe();
123+
event.put(fieldName, DEFAULT_FIELD_VALUES.get(dfr.getSOAPType()));
124+
}
125+
return event;
126+
}
127+
88128
private static void publishPlatformEvent(
89129
String eventName,
90130
String eventPayload

src/main/default/lwc/actions/actions.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
getChannelPrefix
1515
} from 'c/streamingUtility';
1616

17+
import getBlankPlatformEvent from '@salesforce/apex/StreamingMonitorController.getBlankPlatformEvent';
18+
1719
import subscribeAll from './subscribeAll.html';
1820
import subscribe from './subscribe.html';
1921
import publish from './publish.html';
@@ -130,10 +132,21 @@ export default class Actions extends LightningElement {
130132
this.pubPayload = undefined;
131133
}
132134

133-
handlePubEventNameChange(event) {
135+
async handlePubEventNameChange(event) {
134136
this.pubEventName = event.detail.value;
135137
this.pubChannel =
136138
getChannelPrefix(this.pubEventType) + this.pubEventName;
139+
// Load blank event for platform event
140+
if (this.pubEventType === EVT_PLATFORM_EVENT) {
141+
try {
142+
const blankEvent = await getBlankPlatformEvent({
143+
eventName: this.pubEventName
144+
});
145+
this.pubPayload = JSON.stringify(blankEvent, null, 2);
146+
} catch (error) {
147+
console.error(error);
148+
}
149+
}
137150
}
138151

139152
handlePubPayloadChange(event) {

0 commit comments

Comments
 (0)