Categories Salesforce

How & What: Salesforce Platform Events (Part 1)

Platform Events

Platform Events are based on Event-Driven Architecture(EDA). EDA is a framework to define the production, detection, consumption of, and reaction to events. The two components of EDA are: event creators and event consumers. Event creators responsibility is to just create the event and Event consumers responsibility is to know when the event occurs and take an action. In EDA, no requests need to be made to a server to obtain information about a certain state. Instead, a system subscribes to an event channel and is notified whenever new states occur. Any number of consumers can receive and react to the same events in near real time.

Use Cases

Salesforce and external systems communicate through platform event messages.

  • News agency sends events to subscribed clients with the latest breaking news about traffic and road conditions in a mountain retreat destination
  • A status change in Salesforce notifies external order fulfillment apps of a product shipment order.
  • An external product app notifies Salesforce of merchandise returns

Use platform events in the following cases:

  • To send and receive custom event data with a predefined schema
  • To publish or subscribe to events in Apex
  • For the flexibility of publishing and processing events on and off the Salesforce platform

Characteristics

A platform event is a special kind of Salesforce entity, similar in many ways to an sObject. An event message is an instance of a platform event, similar to how a record is an instance of a custom object. Unlike with custom objects, you can’t update or delete event records, or view event records in the Salesforce user interface.

Create Platform Events

From Setup, enter Platform Events in the Quick Find box, then select Platform Events.
On the Platform Events page, click New Platform Event.
You define the custom data that platform events contain. Just like custom objects, you define platform events in Salesforce Development. You create a platform event definition by giving it a name and adding custom fields.
Let’s say CloudFountain wants to notify its subscribers of its product launches. We’ll create a Platform event object called CloudFountain and a text field named Description to store the message.
When you create a platform event, the system appends the __e suffix to create the API name of the event. In our example, for the CloudFountain event, the API name is Cloud_Fountain__e. The description field still is as usual Description__c

Publish Platform Events

If your app is on the Salesforce platform, you can publish events using an Apex method or with declarative tools, such as Process Builder or the Cloud Flow Designer. If your app is an external app, you can publish events using Salesforce APIs.

Publish Through Apex


To publish more than one event in the same call, add your events to a list of events, and pass the list to the EventBus.publish() method. The output of this method is an array of Database.SaveResult objects

Publish Through API

Endpoint to call
/services/data/v40.0/sobjects/Cloud_Fountain__e
JSON Request Body

Response

When you publish platform events, DML limits and other Apex governor limits apply.

Subscribe to Platform Event Notifications with Apex Triggers

With platform events, the process is similar to database triggers. You simply write an after insert Apex trigger on the event object Cloud_Fountain__e to subscribe to incoming events. Triggers provide an auto subscription mechanism in Apex. Triggers receive event notifications from various sources whether they’re published through Apex or APIs. We will see this in “How & What: Salesforce Platform Events (Part 2)”
Platform events support only after insert triggers. The after insert trigger event corresponds to the time after a platform event is published. After an event message is published, the after insert trigger is fired.
As always,  @CloudFountain, your feedback would be invaluable. Please feel free to contact us in case of any questions or concerns.

Categories Salesforce

How & What: Salesforce Platform Events (Part 2)

In our last blog post, we’ve explained what Platform Events are and how to publish them in Salesforce. In this post, we’ll be talking about Subscribing/Listening to the Platform Events to be notified of the latest news.

You can receive event notifications via Apex triggers, process builder, and visual flows.

Subscribe with Apex Triggers

Apex Triggers receive event notifications from various sources—whether they’re published through Apex or APIs. Platform events support only after insert triggers. After an event message is published, the after insert trigger is fired.

Continuing with the same event we created in our last post, Cloud_Fountain__e. Let’s create a trigger to listen to the news event and create a follow-up Case if the news event is an Urgent one.

Subscribe with Process Builder

To subscribe to event messages without code, create a process that starts when a platform event occurs.

Setup → Process Builder → New Process. From the drop-down for the process starting point, choose ‘A Platform Event Occurs’

Create Process Builder on Platform Events (for subscribing).

Then in the Add Trigger → Choose CloudFountain Platform Event. Let’s take it further and say subscribe only if the event is sent from a particular contact only, you can set the matching conditions. This can be varied per your requirements.

As we did in the trigger, we create an immediate action to create a new Case record as shown below.

Subscribe with Flows

Similarly, you can subscribe to a platform event message with a Flow by using a Wait element. Instead of starting a flow when a platform event occurs, a flow that was started previously waits for a platform event and then resumes.

Here’s an example of a Wait element that waits for a CloudFountain event message to occur. The flow resumes only if the Event’s location matches {!MailingCity_Location.MailingCity}.

{!MailingCity_Location} is just a variable in the flow.

Considerations while implementing platform events subscription:

  • There might be a delay between when an event is published and when the trigger processes the event since platform event trigger runs in its own process asynchronously.
  • A trigger processes platform event notifications sequentially in the order they’re received. An Apex trigger can receive a batch of events at once. The order of events is preserved within each batch.
  • Platform Events run under the Automated Process system user and not under the one who executed it.
  • System fields of records such as CreatedById and LastModifiedById also reference the Automated Process System user.
  • Platform Event triggers are subject to Apex governor limits and Apex Trigger Limits as usual.

Use Platform Events for any number of applications and integrations, such as processing business transactions or engaging in customer service.

Thank you for visiting the page. Please feel free to post your invaluable feedback, comments, and questions. We @CloudFountain will be glad to assist you with your Salesforce implementation.

Categories Salesforce

How & What: Salesforce Duplicate Data Management

Find Duplicates Across Your Org

Maintaining accurate, clean data is one of the most critical things you can do to get the most out of Salesforce. It builds the trust of your team and helps you work toward complying with various data security and privacy laws.

Manage Duplicates

Salesforce finds and handles duplicates using a combination of matching rules and duplicate rules. Duplicate rules and duplicate jobs specify matching rules that determine how duplicates are identified.
To manage duplicates, follow this process:

  1. Find duplicates across your org
  2. Create reports on duplicate records
  3. Manage duplicates using duplicate record sets
  4. View error logs for duplicate rules and Matching rules

1. Find Duplicates Across Your Org

Use duplicate jobs with standard or custom matching rules to scan your Salesforce business or person accounts, contacts, or leads for duplicates.
i. Go to Setup → Duplicate Jobs → Click New Job
ii. Select an object → Select an existing matching rule or create one → Click Run
iii. View summary by going to Setup → Duplicate Jobs

2. Create Reports on Duplicate Records

Use duplicate record reports to fine-tune your duplicate and matching rules and share the results of duplicate jobs.
A duplicate records report can include these records.

  • Duplicates created when a rule alerts a user to a possible duplicate, but the user creates the duplicate anyway.
  • Records manually added to a duplicate record set
  • Records in duplicate record sets generated by duplicate jobs

Go to Setup → Report Types → Create a report type → Select Duplicate Record Set as the primary object → Relate Duplicate Record Items to the primary object that you selected → Save the report type.
Save the Report Type. I named the report type as Duplicate Records.

Then create a new Report. Select the newly created Report Type, Duplicate Records, as the report type for this report. The below report shows the Duplicate Record Sets and the related Duplicate Record Items.
If you open the Duplicate Record Set, it shows the actual duplicate records too as highlighted below.

3. Manage Duplicates Using Duplicate Record Sets

A duplicate record set is a list of items identified as duplicates. It’s created when a duplicate rule or job runs. To manage duplicates that aren’t surfaced by a duplicate rule, create a duplicate record set.

  • On a duplicate record set list view, click New.
  • Specify a duplicate rule or a duplicate job.
  • In Lightning Experience, in the Related tab, click New.
  • In Lightning Experience only, merge duplicates in a set by selecting the Compare and Merge action.

4. View Error Logs for Duplicate Rules and Matching Rules
Troubleshoot system errors that prevent duplicate rules or matching rules from running. Error logs are deleted after 90 days.
Setup → Duplicate Error Logs → View the error logs

Categories Salesforce

Build Lightning Web Components

Set Up Visual Studio Code

You already know that Lightning Web Components is a new programming model introduced by Salesforce for building Lightning components. In this article, we’ll discuss what tools you need to build Lightning web components and how to build one.

To create and develop Lightning Web Components and use their powerful features and performance benefits, you need to set up Salesforce DX. You also use Visual Studio Code, which is the recommended Salesforce development environment. You write a simple Lightning web component and add it to a page in Lightning Experience. Below are the steps to create a Lightning Web Component with conditional rendering.

Steps to build Lightning web components

  1. Setup Visual Studio Code
  2. Create a Project & connect to Salesforce org
  3. Create a Lightning web component
  4. Deploy & add to a Lightning page

1. Set Up Visual Studio Code

Visual Studio code editor has easy-to-install extensions for syntax highlighting, code completion, and more. First, install Salesforce extensions for Visual Studio Code. Search for Salesforce Extension Pack and click Install.

Search for Lightning Web Components and click Install. Re-launch Visual Studio Code to complete the installation.

Make sure the editor is ready by typing sfdx, you should see the commands already.

2. Create a Project & connect to Salesforce org

  1. In Visual Studio code, press Command + Shift + P on a Mac or Ctrl + Shift + P on Windows.
  2. Type SFDX.
  3. Select SFDX: Create Project
  4. Enter a Project Name & click create.
  5. Select SFDX: Authorize an Org. Login & Allow

Success message shows like this.

3. Create a Lightning web component

  1. In Visual Studio code, press Command + Shift + P on a Mac or Ctrl + Shift + P on Windows.
  2. Type SFDX.
  3. Select SFDX: Create Lightning Web Component.
  4. Press Enter to accept the default force-app/main/default/lwc.

Give a name to the component, say Cloud Fountain. This simple component displays the text ‘Hello Cloud Fountain’

This would be your HTML file. In this, we are displaying the text “hello {greeting}”. Note that greeting is a variable here

<template>
<lightning-card title=”HelloWorld” icon-name=”custom:custom14″>
<div>
<p>Hello, {upperCaseName}!</p>
<lightning-input name=”last_name” label=”Last Name” onchange={changeHandler}></lightning-input>
<template if:false={showLastNameOnly}>
<div>
<lightning-input name=”first_name” label=”First Name” onchange={changeHandler}></lightning-input>
</div>
</template>
<lightning-input type=”checkbox” label=”Show Last Name only” onchange={handleCheckBox}></lightning-input>
</div>
</lightning-card>
</template>

view rawhelloWorld.html hosted with ❤ by GitHub

This is the .js file. In this javascript file, we are assigning a value ‘Cloud Fountain’ to the variable called ‘greeting’. @track indicates, it is a variable.

import { LightningElement, track } from ‘lwc’;
export default class HelloWorld extends LightningElement {
// @track greeting = ‘World’;
@track firstName = ”;
@track lastName = ”;
@track showLastNameOnly = false;
changeHandler(event) {
// this.greeting = event.target.value;
const fldValue = event.target.name;
if(fldValue === ‘first_name’)
this.firstName = event.target.value;
else if(fldValue === ‘last_name’)
this.lastName = event.target.value;
}
handleCheckBox(event){
this.showLastNameOnly = event.target.checked;
}
get showLastNameOnly(){
return this.showLastNameOnly;
}
get upperCaseName(){
if(this.showLastNameOnly){
return `${this.lastName}`.toUpperCase();
}
else{
return `${this.firstName} ${this.lastName}`.toUpperCase();
}
}
}

view rawhelloWorld.js hosted with ❤ by GitHub

This is the .xml file. In this file, we are setting isExposed to True implies, this component is available to be used in the Lightning pages. Which type of Lightning pages is defined in the targets.

<?xml version=”1.0″ encoding=”UTF-8″?>
<LightningComponentBundle xmlns=”http://soap.sforce.com/2006/04/metadata” fqn=”helloWorld”>
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>

view rawlcb.xml hosted with ❤ by GitHub

That’s it. Now you can deploy this project to Salesforce

4. Deploy & Add the component to Lightning Page

Just right click the file and select SFDX: Deploy Source to Org

After deployment, the lightning web component will be available in the App Builder as a custom component on the left side menu to use in the page.

Thank you for reading this post. Please feel free to leave feedback.

Categories Salesforce

Lightning Web Components and Aura Components – coexistence

Lightning Web Component. Aura Component

As you know Salesforce introduced Lightning web components in Spring 19 release and with that the existing Lightning components are renamed as Aura components. So, the question lies how does Aura components and Lightning web components coexist in the same space and here we are to discuss the same. One thing is for sure – Aura components and Lightning web components can co-exist and work together in many different ways.

Coexistence

If you’re looking to use Aura components and Lightning web components together in your applications then there are two strategies to consider: Nested and Side-by-side compositions.

Nested Composition

A nested composition is a component within a component, either an Aura component or Lightning web component. This is like a parent-child relationship. The rule is you can have both Aura components and Lighting web components inside a parent Aura component, but Lightning web components can only contain other Lightning web components.

In the above ‘nested composition’ example, the Aura component has two child components: another Aura component and a Lightning web component.

The other parent, Lightning web component, also has two children. Both children are Lightning web components.

Remember – Aura component is the ultimate parent.

Side-by-side Composition

The other type of composition is side-by-side in which Aura component and Lightning web component are not within the same hierarchy but are placed separately in a Lightning App Page as shown below.

Here the parent Aura component and the parent Lightning web component are in Side-by-Side strategy. And within the parent Aura component and the parent Lightning web component, the strategy used is nested composition.

How do they work together?

Now that we have seen the different ways the components can co-exist within a single Lightning App Page, let’s look at how they talk between themselves in both nested and side-by-side arrangement using the above example.

    1. Aura component: The parent aura component can interact directly with child component attributes whether those are standard Aura events or custom Aura event bundles.
    2. Lightning web component: The parent Lightning web component can send and receive information through JavaScript CutomObject objects, as well as interacting with child component methods and properties exposed through public APIs.

Both Aura and Lightning web components can send and receive data through the Lightning Data Service (LDS) and the User Interface API.

Limitation: A mechanism for communication between Aura components and Lightning web components that aren’t in the same component hierarchy isn’t provided by the platform yet! But this will be soon available in Lightning Event Service as highlighted in the images above.