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.

Categories Salesforce

Lightning Web Components: Show Child Records for a Parent

Code samples to quickly start playing with Lightning Web Components:

getContactList.html

<template>
<lightning-card title=”Related Contacts” icon-name=”custom:custom63″>
<div>
<template if:true={contacts.data}>
<template for:each={contacts.data} for:item=”contact”>
<p key={contact.Id}>{contact.name}</p>
</template>
<template iterator:it={contacts.data}>
<li style=”list-style-type: none;” key={it.value.Id}>
<article>
<h3 title=”Anypoint Connectors”><a
href=”javascript:void(0);”>{it.value.Name}</a></h3>
<div>
<ul>
<li>Title: {it.value.Title}</li>
</ul>
<ul>
<li>Phone: {it.value.Phone}</li>
</ul>
<ul>
<li>Email: {it.value.Email}</li>
</ul>
</div>
</article>
</li>
</template>
</template>
<template if:true={contacts.error}>
<p>{contacts.error}</p>
</template>
</div>
</lightning-card>
</template>

view rawgetContactList.html hosted with ❤ by GitHub

getContactList.js

/*eslint no-console: [“error”, { allow: [“warn”, “error”] }] */
import { LightningElement, track, wire, api } from ‘lwc’;
import findContacts from ‘@salesforce/apex/ContactController.getContacts’;
/** The delay used when debouncing event handlers before invoking Apex. */
const DELAY = 300;
export default class ApexWireMethodWithParams extends LightningElement {
@track searchKey = ”;
@api recordId;
@wire(findContacts, { searchKey: ‘$searchKey’ })
contacts;
//connectedCallback function is similar to init method in Lightning Components.
connectedCallback(){
this.searchKey = this.recordId;
}
}

view rawgetContactList.js hosted with ❤ by GitHub

*connectedCallBack(): Check out this function. This is a replacement of “init” event in Aura component.

Apex Controller

public class ContactController {
public List<Contact> contactList {get;set;}
public ContactController(){
this.contactList = new List<Contact>();
}
@AuraEnabled(cacheable=true)
public static list<Contact> getContacts(string searchKey){
List<Contact> lst = new List<Contact>([SELECT Id, Name, Title, Phone, Email FROM Contact WHERE Accountid = :searchKey ]);
return lst;
}
}

view rawContactController.java hosted with ❤ by GitHubCode samples to quickly start playing with Lightning Web Components:

getContactList.html

<template>
<lightning-card title=”Related Contacts” icon-name=”custom:custom63″>
<div>
<template if:true={contacts.data}>
<template for:each={contacts.data} for:item=”contact”>
<p key={contact.Id}>{contact.name}</p>
</template>
<template iterator:it={contacts.data}>
<li style=”list-style-type: none;” key={it.value.Id}>
<article>
<h3 title=”Anypoint Connectors”><a
href=”javascript:void(0);”>{it.value.Name}</a></h3>
<div>
<ul>
<li>Title: {it.value.Title}</li>
</ul>
<ul>
<li>Phone: {it.value.Phone}</li>
</ul>
<ul>
<li>Email: {it.value.Email}</li>
</ul>
</div>
</article>
</li>
</template>
</template>
<template if:true={contacts.error}>
<p>{contacts.error}</p>
</template>
</div>
</lightning-card>
</template>

view rawgetContactList.html hosted with ❤ by GitHub

getContactList.js

/*eslint no-console: [“error”, { allow: [“warn”, “error”] }] */
import { LightningElement, track, wire, api } from ‘lwc’;
import findContacts from ‘@salesforce/apex/ContactController.getContacts’;
/** The delay used when debouncing event handlers before invoking Apex. */
const DELAY = 300;
export default class ApexWireMethodWithParams extends LightningElement {
@track searchKey = ”;
@api recordId;
@wire(findContacts, { searchKey: ‘$searchKey’ })
contacts;
//connectedCallback function is similar to init method in Lightning Components.
connectedCallback(){
this.searchKey = this.recordId;
}
}

view rawgetContactList.js hosted with ❤ by GitHub

*connectedCallBack(): Check out this function. This is a replacement of “init” event in Aura component.

Apex Controller

public class ContactController {
public List<Contact> contactList {get;set;}
public ContactController(){
this.contactList = new List<Contact>();
}
@AuraEnabled(cacheable=true)
public static list<Contact> getContacts(string searchKey){
List<Contact> lst = new List<Contact>([SELECT Id, Name, Title, Phone, Email FROM Contact WHERE Accountid = :searchKey ]);
return lst;
}
}

view rawContactController.java hosted with ❤ by GitHub