Sample code for Push Notifications on Custom Objects
To send Push Notifications when a custom object's record is created or updated, you must create the following in the Salesforce org.
A custom Apex Trigger on the custom object.
A custom class called SFA_WrapperDef.
Apex Trigger Code
The custom Apex trigger is responsible for the following.
Identify the fields that are updated in a record.
Create a Platform Event that publishes the notification message from Salesforce.
In the following sample code, all the fields of the custom object are retrieved using the ObjectDescribe method. However, it is recommended to identify specific fields, whose change should result in a Push Notification and retrieve them using the ObjectDescribe method.
//Replace SVMXC__Installed_Product__c with the API name of the custom Object on which you are configuring SFA - Push Notifications

trigger Custom_Trigger_SFA on SVMXC__Installed_Product__c (after insert, after update) {
List<Sobject> lstOfNewRecord = trigger.new;
List<Sobject> lstOfOldRecord = trigger.old;
if (lstOfNewRecord != null && lstOfNewRecord.size() == 1) {
system.debug('Record updated: ' + lstOfNewRecord[0]);
SFA_WrapperDef.SFA_RecordWrapper new_Record = new SFA_WrapperDef.SFA_RecordWrapper();
SFA_WrapperDef.SFA_RecordWrapper old_Record = new SFA_WrapperDef.SFA_RecordWrapper();

// Here we do an object describe to get all the fields on the custom object. These fields have to be compared later to identify if the field value changed and then result in a notification.
// If there are a lot of fields on the custom objects, it is recommended to retrieve only specific fields, whose change must result in a Push Notifcation.

Schema.DescribeSObjectResult[] descResult = Schema.describeSObjects(new String[]{'SVMXC__Installed_Product__c'});
List<String> lstOfField = new List<String>();
List<SVMXC__SFA_Platform_Event__e> platformEventsToPublish = new List<SVMXC__SFA_Platform_Event__e>();
for(DescribeSObjectResult results : descResult) {
Map<String, SObjectField> fldsMap = results.fields.getMap();
for (String fld: fldsMap.keyset()) {
lstOfField.add(fldsMap.get(fld).getDescribe().getName());
}
System.debug('lstOfField: '+lstOfField);
}
for (Sobject sobj: lstOfNewRecord) {
for(String fieldName:lstOfField) {
new_Record.mapfieldNameValues.put(fieldName, String.valueOf(sobj.get(fieldName)));
if (Trigger.isUpdate) {
old_Record.mapfieldNameValues.put(fieldName, String.valueOf(lstOfOldRecord[0].get(fieldName)));
}
}
system.debug('Record wrapped used to report: ' + new_Record);

// Now, create a new Platform Event instance and prepare the JSON payload to be included in the platform event.

SVMXC__SFA_Platform_Event__e plEvent = new SVMXC__SFA_Platform_Event__e();
plEvent.SVMXC__New_Record_JSON__c = JSON.serialize(new_Record);
plEvent.SVMXC__Record_Id__c = String.valueOf(sobj.get('Id'));

// Set the Event Type of the Platform Event to either Updated or Created based on the SFA's Event Trigger type (Record Created / Record Updated / Record Field updated)
// If the Event Trigger type is Updated, then prepare the JSON payload for SVMXC__Old_Record_JSON__c. This JSON contains the Old value of a record.
// For a Record update, it is important to know the Old and New values of a record, so that it can be compared and then a notification can be sent.


if (Trigger.isUpdate) {
plEvent.SVMXC__Event_Type__c = 'Updated';
plEvent.SVMXC__Old_Record_JSON__c = JSON.serialize(old_Record);
} else if (Trigger.isInsert) {
plEvent.SVMXC__Event_Type__c = 'Inserted';
}
plEvent.SVMXC__Object_Name__c = 'SVMXC__Installed_Product__c';
platformEventsToPublish.add(plEvent);
}

// Here the Platform Event is published to the Event bus

system.debug('Events being published: ' + platformEventsToPublish);
EventBus.publish(platformEventsToPublish);
}
}
SFA_WrapperDef Code Sample
The SFA_WrapperDef is a custom wrapper class that is used by the custom trigger internally to prepare the data required for the Platform Event. It also has the required logic to retrieve the SFA configuration details.
// This is a Wrapper class and is used to serialize/deserialize a record’s new vs old values to be added to the Platform Event.
// Do not change the name of this class

public with sharing class SFA_WrapperDef{

//Using to generate JSON for record to publish the platform event
public class SFA_RecordWrapper{
public Map<String,String> mapfieldNameValues= new Map<String,String>();
}
}
Was this helpful?