Skip to content

Commit 05bd657

Browse files
authored
feat: Custom Platform Event template (#60)
* build: ignore cursor * feat: upgrade to API v65.0 * fix: SDLS minor warnings * feat: custom platform event template * feat: PE template tests
1 parent 6e0009c commit 05bd657

31 files changed

+148
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Dev tools
2+
.cursor
23
.sf
34
.sfdx
45
.vscode

sfdx-project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
],
88
"namespace": "",
99
"sfdcLoginUrl": "https://login.salesforce.com",
10-
"sourceApiVersion": "64.0"
10+
"sourceApiVersion": "65.0"
1111
}

src/main/default/classes/StreamingMonitorController.cls

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ 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+
@TestVisible
43+
private final static Map<Schema.SOAPType, Object> DEFAULT_FIELD_VALUES = new Map<Schema.SOAPType, Object>{
44+
Schema.SOAPType.STRING => '',
45+
Schema.SOAPType.DATE => '2025-01-01',
46+
Schema.SOAPType.DATETIME => '2025-01-01T00:00:00Z',
47+
Schema.SOAPType.BOOLEAN => false,
48+
Schema.SOAPType.DOUBLE => 0
49+
};
50+
3551
@AuraEnabled
3652
public static void publishStreamingEvent(
3753
String eventType,
@@ -85,6 +101,31 @@ public abstract class StreamingMonitorController {
85101
];
86102
}
87103

104+
@AuraEnabled
105+
public static Map<String, Object> getBlankPlatformEvent(String eventName) {
106+
// Get object fields
107+
Schema.DescribeSobjectResult[] results;
108+
try {
109+
results = Schema.describeSObjects(new List<String>{ eventName });
110+
} catch (Exception e) {
111+
throw new StreamingException(
112+
'Unknown platform event type: ' + eventName
113+
);
114+
}
115+
Map<String, Schema.SObjectField> fields = results[0].fields.getMap();
116+
// Remove standard fields
117+
Set<String> fieldNames = fields.keySet();
118+
fieldNames.removeAll(PLATFORM_EVENT_STANDARD_FIELDS);
119+
Map<String, Object> event = new Map<String, Object>();
120+
// Add default values for fields
121+
for (String fieldName : fieldNames) {
122+
Schema.DescribeFieldResult dfr = fields.get(fieldName)
123+
.getDescribe();
124+
event.put(fieldName, DEFAULT_FIELD_VALUES.get(dfr.getSOAPType()));
125+
}
126+
return event;
127+
}
128+
88129
private static void publishPlatformEvent(
89130
String eventName,
90131
String eventPayload
@@ -307,7 +348,7 @@ public abstract class StreamingMonitorController {
307348
public void execute(QueueableContext context) {
308349
String restAPIURL =
309350
URL.getOrgDomainUrl().toExternalForm() +
310-
'/services/data/v64.0/sobjects/StreamingChannel/' +
351+
'/services/data/v65.0/sobjects/StreamingChannel/' +
311352
channelId +
312353
'/push';
313354
HttpRequest httpRequest = new HttpRequest();

src/main/default/classes/StreamingMonitorController.cls-meta.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
xmlns="urn:metadata.tooling.soap.sforce.com"
44
fqn="StreamingMonitorController"
55
>
6-
<apiVersion>64.0</apiVersion>
6+
<apiVersion>65.0</apiVersion>
77
<status>Active</status>
88
</ApexClass>

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) {

src/main/default/lwc/actions/actions.js-meta.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
xmlns="http://soap.sforce.com/2006/04/metadata"
44
fqn="actions"
55
>
6-
<apiVersion>64.0</apiVersion>
6+
<apiVersion>65.0</apiVersion>
77
<isExposed>false</isExposed>
88
</LightningComponentBundle>

src/main/default/lwc/actions/publish.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<lightning-card title="Publish a streaming event">
3-
<div class="slds-p-horizontal_large">
3+
<div class="slds-var-p-horizontal_large">
44
<lightning-combobox
55
label="Event type"
66
value={pubEventType}
@@ -31,7 +31,9 @@
3131
disabled={isPublishDisabled}
3232
field-level-help={pubPayloadHelp}
3333
></lightning-textarea>
34-
<div class="slds-align_absolute-center slds-m-top_medium">
34+
<div
35+
class="slds-align_absolute-center slds-var-m-top_medium"
36+
>
3537
<lightning-button
3638
variant="brand"
3739
label="Publish"

src/main/default/lwc/actions/register.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<template>
22
<lightning-card title="Register a streaming event source">
3-
<div class="slds-p-horizontal_large">
3+
<div class="slds-var-p-horizontal_large">
44
<lightning-combobox
55
label="Event type"
66
name="regEventType"
77
value={regEventType}
88
options={regEventTypes}
99
onchange={handleValueChange}
1010
placeholder="Select type"
11-
class="slds-m-bottom_medium"
11+
class="slds-var-m-bottom_medium"
1212
></lightning-combobox>
1313

1414
<template if:true={isPushTopicReg}>
15-
<p class="slds-m-bottom_small">
15+
<p class="slds-var-m-bottom_small">
1616
Customize and execute the following code in Apex to register
1717
a new PushTopic:
1818
</p>
@@ -21,7 +21,7 @@
2121
pushTopic.Name = 'ClosedCaseUpdates';<br />
2222
pushTopic.Query = 'SELECT Id, Subject FROM Case WHERE
2323
Status=\'Closed\'';<br />
24-
pushTopic.ApiVersion = 64.0;<br />
24+
pushTopic.ApiVersion = 65.0;<br />
2525
pushTopic.NotifyForOperationCreate = true;<br />
2626
pushTopic.NotifyForOperationUpdate = true;<br />
2727
pushTopic.NotifyForOperationUndelete = true;<br />

src/main/default/lwc/actions/subscribe.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<lightning-card title="Subscribe to a channel">
3-
<div class="slds-p-horizontal_large">
3+
<div class="slds-var-p-horizontal_large">
44
<form onsubmit={handleSubscribe}>
55
<lightning-combobox
66
label="Event type"
@@ -41,7 +41,7 @@
4141
onchange={handleValueChange}
4242
required
4343
></lightning-input>
44-
<div class="slds-align_absolute-center slds-m-top_medium">
44+
<div class="slds-align_absolute-center slds-var-m-top_medium">
4545
<lightning-button
4646
variant="brand"
4747
label="Subscribe"

src/main/default/lwc/actions/subscribeAll.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<lightning-card title="Subscribe to multiple streaming channels">
3-
<div class="slds-p-horizontal_large">
4-
<p class="slds-m-vertical_small">
3+
<div class="slds-var-p-horizontal_large">
4+
<p class="slds-var-m-vertical_small">
55
Custom Change Data Capture channels cannot be automatically
66
discovered. Supported channels: PushTopic, Generic, Platform
77
events, Change Data Capture events and Monitoring events.
@@ -21,7 +21,7 @@
2121
onchange={handleValueChange}
2222
options={replayOptions}
2323
></lightning-combobox>
24-
<div class="slds-align_absolute-center slds-m-top_medium">
24+
<div class="slds-align_absolute-center slds-var-m-top_medium">
2525
<lightning-button
2626
variant="brand"
2727
label="Subscribe"

0 commit comments

Comments
 (0)