1

When an order is created in Salesforce we need to send the order and the order items to the ERP system via the middleware. We are planning on doing this using a platform event.

How can we send both the order and the orderitems (Up to 30) in a single platform event message or do we have to send these as separate messages?

Thanks

2 Answers 2

3

So, the Platform Event is defined as an SObject so if you want to send Order + lineItems in a single event, you really only have one option

Order__e
 with fields
 Date orderDate
 Text company
 ... other fields from Order object
 Text Area (long, 128K length) lineItems

where lineItems is a string that is a serialized JSON of all of the OrderItems for the Order. Your ERP system would have to deserialize this.

Caveats:

  • Orders with a ton of orderItems will exceed the 128K field size.

If you use multiple events (one for header, one for each line item), then ...

  • Include in the order event the count of the number of line items so ERP will know when all line items have been received
  • The ERP system can't expect to always receive the header event before receiving some or all of the line items
-1

The above solution will not work.

It is not possible with Platform Events or any other Salesforce standard to publish a custom event structure with a valid JSON inside a custom text field. The problem is that the streaming API serializes the event itself before publishing. So if you serialize an object in the custom field once, the event will be serialized after publishing and your custom JSON will be serialized again.

Example: serialized sObject in custom field "Message__c"

Case c = [SELECT Id FROM Case LIMIT 1]; 
new customEvent__e(Message__c = JSON.serialize(c));

will result in:

{
   "ReplayId":"value",
   "CreatedDate":"value",
   "CreatedById":"value",
   "EventUuid":"value",
   "Message__c":"{\"attributes\":{\"type\":\"Case\",\"url\":\"/services/data/v57.0/sobjects/Case/Id\"},\"Id\":\"value\",\"RecordTypeId\":\"value\"}",
   "_ObjectType":"customEvent__e",
   "_EventType":"value"
}

If the event-consumer is not able to build a valid json, do a callout via REST.

3
  • Disagree here. The consumer can deserialize and then deserialize the Message__c field.
    – Keith C
    Commented Mar 16, 2023 at 16:04
  • As I have said before: only if the consumer is able to do that. If not, you can’t go ahead with this approach.
    – SfdevfS
    Commented Mar 17, 2023 at 17:09
  • OK, but buried as a negative statement in the last line of the post.
    – Keith C
    Commented Mar 17, 2023 at 18:05

You must log in to answer this question.

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