Accelerator Salesforce Party Event Listener - Implementation Template

(0 reviews)

home

This API implementation template is a component of MuleSoft Accelerators, which accelerate the implementation of essential integration use cases.

The solution includes pre-built APIs, connectors, and integration templates that help unlock business-critical data from external systems and guide you in adopting best practices synthesized from thousands of customer implementations. Use these assets as is or extend them to meet your company’s unique needs.


Overview

The following functionality is supported by the Accelerator Salesforce Party Event Listener application.

CIM to Salesforce Objects Mapping

Salesforce Object - Record TypeCIM Type
Account - Person AccountsCustomer with PartyType as Individual
Account - AccountCustomer with PartyType as Organization
Account - HouseholdHousehold
Lead - LeadLead
ContactIndividual

Customer event listener

This event listener is subscribed to the Platform Event channel configured in Salesforce with API Name as Account__e. A Trigger generates an Event based on changes in Account Object. This event listener can be started/stopped using the customer-event-listener-state property in the configuration file.

The fields including customer and contact information are part of this Platform Event. These fields are received as JSON Object from Salesforce and transformed to CIM data type and published to Anypoint MQ. In case of any failures, the incoming original payload is published to dead letter queue by the exception handler.

Customer Address event listener

This event listener is subscribed to the Platform Event channel configured in Salesforce with API Name as AccountAddresses__e. A Trigger generates an Event based on changes to a Postal Address on Account (Record Type as Person Account) Object. This event listener can be started/stopped using the customer-addresses-event-listener-state property in the configuration file.

The fields including the address information are part of this Platform Event. These fields are received as JSON Object from Salesforce and transformed to CIM data type and published to Anypoint MQ. In case of any failures, the incoming original payload is published to the dead letter queue by the exception handler.

Contact event listener

This event listener is subscribed to the Platform Event channel configured in Salesforce with API Name as Contact__e. A Trigger generates an Event based on changes in Account Object. This event listener can be started/stopped using the contact-event-listener-state property in the configuration file.

The fields in the Platform Event include contact information. These fields are received as JSON Object from Salesforce and transformed to CIM data type (Party as Individual) and published to Anypoint MQ. In case of any failures, the incoming original payload is published to the dead letter queue by the exception handler.

Household event listener

This event listener is subscribed to the Platform Event channel configured in Salesforce with API Name as Household__e. A Trigger generates an Event based on changes in Account Object. This event listener can be started/stopped using the household-event-listener-state property in the configuration file.

The fields in the Platform Event include household account information. These fields are received as JSON Object from Salesforce and transformed to CIM data type (Party as Household) and published to Anypoint MQ. In case of any failures, the incoming original payload is published to the dead letter queue by the exception handler.

Lead event listener

This event listener is subscribed to the Platform Event channel configured in Salesforce with API Name as Lead__e. A Trigger generates an Event based on changes in Account Object. This event listener can be started/stopped using the lead-event-listener-state property in the configuration file.

The fields in the Platform Event include lead information. These fields are received as JSON Object from Salesforce and transformed to CIM data type (Party as Individual) and published to Anypoint MQ. In case of any failures, the incoming original payload is published to the dead letter queue by the exception handler.

Opportunities event listener

This event listener is subscribed to the Platform Event channel configured in Salesforce with API Name as Opportunity__e. A Trigger generates an Event based on changes in Account Object.This event listener can be started/stopped using the opportunity-event-listener-state property in the configuration file.

The fields in the Platform Event include Opportunity information. These fields are received as JSON Object from Salesforce and transformed to CIM data type (Party as Opportunity) and published to Anypoint MQ. As Opportunity is always related to Account in Salesforce, a call is made to accel-salesforce-customers-sys-api opportunities endpoint to get the Account details. In case of any failure the incoming original payload is published to the dead letter queue by the exception handler.

Getting started

bulb.png The Getting Started with MuleSoft Accelerators guide provides general information on getting started with the accelerator components. This includes instructions on setting up your local workstation for configuring and deploying the applications.

Once your workstation has been set up and the application template imported into Anypoint Studio, proceed with the Prerequisites section.

Prerequisites

This implementation template has the following dependencies:

  • Anypoint MQ Destinations
  • Salesforce Platform Events
  • Salesforce Apex Triggers

Please review the use cases described on the MuleSoft Accelerators solution pages for more information about dependencies on other APIs and services.

Anypoint MQ destinations

In addition to the Anypoint MQ client application credentials, the following destinations must be created and made accessible to this API:

  1. accel-partyrole-update-exchange
  2. accel-partyrole-update-dl-queue
  3. accel-address-update-exchange
  4. accel-address-update-dl-queue
  5. accel-party-update-exchange
  6. accel-party-update-dl-queue
  7. accel-lead-update-exchange
  8. accel-lead-update-dl-queue
  9. accel-opportunity-update-exchange
  10. accel-opportunity-update-dl-queue

Salesforce Platform Events

This implementation requires creation of Platform Events in Salesforce. To create a Platform event Object following the below steps:

  1. Login to Salesforce.
  2. Click Setup to search for Platform Events in Quick Find Box. Platform Events Page under Integrations section appears with list of available Platform Events.
  3. Click New Platform Event button to create an Object and enter Platform Information Details fields like Label, Plural Label and Object. Click Save.
  4. Enter Custom Fields & RelationShips to create an Event with specific defintions.

Platform Event Definition for Account PlatformEvent

Create a Platform Event with Label as AccountPlatformEvent, Object Name as Account and Custom Fields & RelationShips as per below table. These Custom fields can also be created via Apex script - instructions are included in the "Create Custom Fields via Apex script" section, below.

Field LabelAPI NameData Type
AccountNumberAccountNumber__cText(80)
BillingCityBillingCity__cText(80)
BillingCountryBillingCountry__cText(80)
BillingGeocodeAccuracyBillingGeocodeAccuracy__cText(18)
BillingLatitudeBillingLatitude__cText(80)
BillingLongitudeBillingLongitude__cText(80)
BillingPostalCodeBillingPostalCode__cText(80)
BillingStateBillingState__cText(80)
BillingStreetBillingStreet__cText(80)
CreatedDateCreatedDate__cDate/Time
FaxFax__cText(80)
FirstNameFirstName__cText(80)
Global_BillingAddress_IdGlobal_BillingAddress_Id__cText(36)
Global_Customer_IdGlobal_Customer_Id__cText(36)
Global_EmailAddress_IdGlobal_EmailAddress_Id__cText(36)
Global_FaxNumber_IdGlobal_FaxNumber_Id__cText(36)
Global_MailingAddress_IdGlobal_MailingAddress_Id__cText(36)
Global_MobileNumber_IdGlobal_MobileNumber_Id__cText(36)
Global_PhoneNumber_IdGlobal_PhoneNumber_Id__cText(36)
Global_ShippingAddress_IdGlobal_ShippingAddress_Id__cText(36)
IdId__cText(36)
IsDeletedIsDeleted__cCheckbox
IsPersonAccountIsPersonAccount__cCheckbox
LastModifiedDateLastModifiedDate__cDate/Time
LastNameLastName__cText(80)
NameName__cText(80)
PersonEmailPersonEmail__cText(80)
PersonMailingCityPersonMailingCity__cText(80)
PersonMailingCountryPersonMailingCountry__cText(80)
PersonMailingGeocodeAccuracyPersonMailingGeocodeAccuracy__cText(18)
PersonMailingLatitudePersonMailingLatitude__cText(80)
PersonMailingLongitudePersonMailingLongitude__cText(80)
PersonMailingPostalCodePersonMailingPostalCode__cText(80)
PersonMailingStatePersonMailingState__cText(80)
PersonMailingStreetPersonMailingStreet__cText(80)
PersonMobilePhonePersonMobilePhone__cText(80)
PhonePhone__cText(80)
ShippingCityShippingCity__cText(80)
ShippingCountryShippingCountry__cText(80)
ShippingGeocodeAccuracyShippingGeocodeAccuracy__cText(18)
ShippingLatitudeShippingLatitude__cText(80)
ShippingLongitudeShippingLongitude__cText(80)
ShippingPostalCodeShippingPostalCode__cText(80)
ShippingStateShippingState__cText(80)
ShippingStreetShippingStreet__cText(80)

Platform Event Definition for Account Address PlatformEvent

Create a Platform Event with Label as AccountAddressPlatformEvent, Object Name as AccountAddresses and Custom Fields & RelationShips as per below table. As mentioned above, these Custom fields can also be created via Apex script.

Field LabelAPI NameData Type
BillingCityBillingCity__cText(80)
BillingCountryBillingCountry__cText(80)
BillingGeocodeAccuracyBillingGeocodeAccuracy__cText(10)
BillingLatitudeBillingLatitude__cText(10)
BillingLongitudeBillingLongitude__cText(10)
BillingPostalCodeBillingPostalCode__cText(10)
BillingStateBillingState__cText(80)
BillingStreetBillingStreet__cText(80)
Global_BillingAddress_IdGlobal_BillingAddress_Id__cText(36)
Global_MailingAddress_IdGlobal_MailingAddress_Id__cText(36)
IdId__cText(36)
IsDeletedIsDeleted__cCheckbox
PersonMailingCityPersonMailingCity__cText(80)
PersonMailingCountryPersonMailingCountry__cText(80)
PersonMailingGeocodeAccuracyPersonMailingGeocodeAccuracy__cText(10)
PersonMailingLatitudePersonMailingLatitude__cText(10)
PersonMailingLongitudePersonMailingLongitude__cText(10)
PersonMailingPostalCodePersonMailingPostalCode__cText(10)
PersonMailingStatePersonMailingState__cText(80)
PersonMailingStreetPersonMailingStreet__cText(80)

Platform Event Definition for Contact PlatformEvent

Create a Platform Event with Label as ContactPlatformEvent, API Name as Contact and Custom Fields & RelationShips as per below table. As mentioned above, these Custom fields can also be created via Apex script.

Field LabelAPI NameData Type
BirthdateBirthdate__cDate
CreatedDateCreatedDate__cDate/Time
EmailEmail__cText(80)
FirstNameFirstName__cText(80)
Global_EmailAddress_IdGlobal_EmailAddress_Id__cText(36)
Global_Individual_IdGlobal_Individual_Id__cText(36)
Global_PhoneNumber_IdGlobal_PhoneNumber_Id__cText(36)
Global_PostalAddress_IdGlobal_PostalAddress_Id__cText(36)
IdId__cText(36)
IsDeletedIsDeleted__cCheckbox
LastModifiedDateLastModifiedDate__cDate/Time
LastNameLastName__cText(80)
MailingCityMailingCity__cText(80)
MailingPostalCodeMailingPostalCode__cText(36)
MailingStateMailingState__cText(36)
MailingStreetMailingStreet__cText(255)
NameName__cText(80)
PhonePhone__cText(80)
SalutationSalutation__cText(80)

Platform Event Definition for Household PlatformEvent

Create a Platform Event with Label as HouseholdPlatformEvent, Object Name as Household and Custom Fields & RelationShips as per below table. As mentioned above, these Custom fields can also be created via Apex script.

Field LabelAPI NameData Type
CreatedDateCreatedDate__cDate/Time
Global_Party_IdGlobal_Party_Id__cText(36)
IdId__cText(36)
IsDeletedIsDeleted__cCheckbox
IsPersonAccountIsPersonAccount__cCheckbox
LastModifiedByIdLastModifiedById__cText(36)
LastModifiedDateLastModifiedDate__cDate/Time
NameName__cText(80)
RecordTypeIdRecordTypeId__cText(36)

Platform Event Definition for Opportunity PlatformEvent

Create a Platform Event with Label as OpportunityPlatformEvent, Object Name as Opportunity and Custom Fields & RelationShips as per below table. As mentioned above, these Custom fields can also be created via Apex script.

Field LabelAPI NameData Type
AccountIdAccountId__cText(36)
Ach_Transfer_Auth_Document_IdAch_Transfer_Auth_Document_Id__cText(36)
Asset_Transfer_Auth_Document_IdAsset_Transfer_Auth_Document_Id__cText(36)
CloseDateCloseDate__cDate/Time
CreatedDateCreatedDate__cDate/Time
Document_Signatures_ReceivedDocument_Signatures_Received__cCheckbox
IdId__cText(18)
IsClosedIsClosed__cCheckbox
IsDeletedIsDeleted__cCheckbox
IsWonIsWon__cCheckbox
KYC_CompletedKYC_Completed__cCheckbox
LastModifiedDateLastModifiedDate__cDate/Time
NameName__cText(80)
StageNameStageName__cText(80)
TypeType__cText(80)

Platform Event Definition for Lead PlatformEvent

Create a Platform Event with Label as LeadPlatformEvent, Object Name as Lead and Custom Fields & RelationShips as per below table. As mentioned above, these Custom fields can also be created via Apex script.

Field LabelAPI NameData Type
CityCity__cText(80)
ConvertedAccountIdConvertedAccountId__cText(36)
ConvertedDateConvertedDate__cDate
CountryCountry__cText(80)
CreatedDateCreatedDate__cDate/Time
DescriptionDescription__cText(255)
EmailEmail__cText(80)
FirstNameFirstName__cText(80)
IdId__cText(36)
IsConvertedIsConverted__cCheckbox
IsDeletedIsDeleted__cCheckbox
LastModifiedDateLastModifiedDate__cDate/Time
LastNameLastName__cText(80)
LeadSourceLeadSource__cText(255)
NameName__cText(80)
PhonePhone__cText(80)
PostalCodePostalCode__cText(10)
SalutationSalutation__cText(80)
StateState__cText(80)
StatusStatus__cText(80)
StreetStreet__cText(255)

Create Custom Fields via Apex script

An Apex class called CustomFieldUtility can be used to create custom fields programmatically. The source for this class can be found in the Common Resources project. Download this project and follow the steps below.

  1. Click on Salesforce Setup icon and select Developer Console to open a new console window
  2. Go to File -> New -> Apex class and create a new class named CustomFieldUtility
  3. Copy the code from accelerator-common-resources/salesforce/CustomFieldUtility.apxc to the script editor
  4. Select the File -> Save menu option to compile and save the class
  5. Select Debug -> Open Execute Anonymous Window
  6. Copy and paste the contents of accelerator-common-resources/salesforce/CreatePlatformEventsCustomFields.txt into the window.
  7. Enable the Open Log option and click the Execute button.

Once the script completes, select the Debug Only filter option to show just the results of each create request. Verify that all fields have been successfully created.

Note: Do not execute all the content in the CreatePlatformEventsCustomFields.txt file at once as the console might throw a timeout error. Instead execute it in batches.

Salesforce Apex Triggers

This implementation requires creation of Apex Triggers for the Platform Events to be generated on changes to records in Salesforce.

Salesforce ObjectApex script locationTrigger name
Account/src/test/resources/scripts/accounts-event-publisher.apxtAccountEventsPub
Account/src/test/resources/scripts/account-addresses-event-publisher.apxtAccountAddressEventsPub
Account/src/test/resources/scripts/households-event-publisher.apxtHouseholdEventsPub
Contact/src/test/resources/scripts/contacts-event-publisher.apxtContactEventsPub
Opportunity/src/test/resources/scripts/opportunities-event-publisher.apxtOpportunityEventsPub
Lead/src/test/resources/scripts/leads-event-publisher.apxtLeadEventsPub

Follow the below steps to create the Apex Triggers

  1. Login to Salesforce.
  2. Click Setup to search for Object Manager in Quick Find Box.
  3. Search for the Salesforce Object. A page appears with Details of the Object.
  4. Click Triggers unders the Details section to see list of available triggers on the Object. Click New.
  5. Copy the Script from the location specified in the above table .
  6. Obtain the LastModifiedById and RecordTypeId (if it exists) by running the queries mentioned in the trigger from Developer Console.
  7. Replace the values in the script
  8. Click Save and ensure isActive checkbox is ticked.
  9. Repeat the steps for all the Salesforce objects in the above table

Deployment

Each Accelerator implementation template in Exchange includes Bash and Windows scripts for building and deploying the APIs to CloudHub. These scripts depend on repositories, global settings, deployment profiles, and associated properties configured in the Maven settings.xml file.

For additional details, please refer to the Application Deployment section of the Getting Started Guide.

Preparation

Ensure the Maven profile CloudHub-FINS-DEV has been properly configured in your settings.xml file. In particular, make sure the common properties for your environment have been provided (e.g., Anypoint Platform client ID and secret).

Required property overrides

At a minimum, the following properties must be customized to reflect the target deployment environment.

Property NameDescription
anypoint-mq.client-idAnypoint MQ Client Id
anypoint-mq.client-secretAnypoint MQ Client Id
sfdc.usernameSalesforce Instance client user Name
sfdc.tokenSalesforce Instance client token
sfdc.passwordSalesforce Instance client password
sfdc.client-idThe Consumer Id of the Connected App in Salesforce
sfdc.client-secretThe Consumer secret of the Connected App in Salesforce
sfdc.update-sourceName of the Source Salesforce Instance from where the Events are triggered

Implementation notes

  1. All the triggers filter records based on LastModifiedBy <> Service Account User ID to avoid cyclical updates.
  2. The Mule idempotent message validator is used to ensure that only unique messages are published to Anypoint MQ. The Salesforce ID with an expiration interval and time-to-live (ttl) of 30 seconds (configurable) is used as the unique ID to filter out duplicate Mule messages.
  3. The current implementation does not support replay of Platform Events that were missed when the Mule application is down.

Additional resources


Reviews

TypeTemplate
OrganizationMulesoft Inc.
Published by
MuleSoft Solutions
Published onNov 10, 2023
Asset overview

Asset versions for 1.0.x

Asset versions
VersionActions
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0