Use Lightning Web Components in Flow

Endless limit for your Salesforce application by combining Flow with Lightning Web Components (LWC)

As part of Winter 20, we can embed Lightning Web Components in Flow. Imagine, endless possibilities for Rapid Application Development (RAD) and ease of maintaining application.

Few examples where this could come handy are like embedding lookup components (no more workaround), showing data table, capturing signature, slide shows and so on.

It’s time to jump into water. In this blog post, I would be creating Simple Lightning Web Component (LWC). This LWC Component would be able to get value from flow, pass value to flow, navigate to Next screen and hookup into flow validation engine to restrict next page navigation if there is error in LWC input.

Note : Read this blog post to learn how to embed Aura Component in Flow.

Continue reading “Use Lightning Web Components in Flow”

Data exchange between Aura, Lightning Web Components (LWC) and Visualforce

Using Lightning Message Services for event Communication between Lightning Web Components, Aura Components and Visualforce

Few months back, I wrote an article on how pub sub model can be used to communicate between Lightning Web Components. In that blog post, we used external library to pass event from child to Lightning Web Components.

Lightning Message Service

In Winter 20, Salesforce released Lightning Message Service which can be used to exchange data between Visualforce, Aura Component and Lightning Web Components. Unlike previous process, we don’t need to import any external library like pub or sub.

In this blog post, I would be creating Visualforce, Aura Component and Lightning Message Service and exchanging message between all of them using Lightning Message Service.

Continue reading “Data exchange between Aura, Lightning Web Components (LWC) and Visualforce”

Lightning Web Component Event Handling – Pub Sub

Author posted by Jitendra on Posted on under category Categories Salesforce and tagged as Tags , with Leave a comment on Lightning Web Component Event Handling – Pub Sub

How to handle events in Lightning Web Components between nested and non nested Components

It is very common to come across use case of communicating between Lightning Web Components.

If you are not interested on code explanation and directly want to jump on live demo , check this playground.

Communicate between Nested Lightning Web Components

This is one of most simplest and common use case, where parent LWC wants to react on event produced by child LWC.

In above image, Model 3 is child (nested component) of Tesla.

Continue reading “Lightning Web Component Event Handling – Pub Sub”

Framework to fix – Governor Limit of 100 jobs in Flex Queue

How to design an application in Salesforce to handle governor limit error of maximum 100 jobs in Flex Queue

Recently , I inherited an Org with huge customization , which enqueue 100+ Batch Jobs in few scenario. Now, don’t ask me why ?

I remember, few years back, Salesforce had limit of 5 Apex Batch that can be executed at a time, but we had expectation and demand !!! Salesforce introduced Flex queue and we can have 100 Batch apex waiting to be executed, still we are not happy. After all, human is wanting animal.

After 100 Batch apex, all jobs were failing with error System.AsyncException You have exceeded the limit of 100 Jobs in the flex queue. As I explained my life previously in this post, I had to fix this issue as well.

Right way to fix it was to analyze existing code, perform code review, why do we need even customization etc.. However, time was crucial and I had to do something quickly.

Below framework was used to fix the issue

Continue reading “Framework to fix – Governor Limit of 100 jobs in Flex Queue”

Video – Large Data Volume – How Lightning Platform Query Optimizer Works

Best Practices of handling Large Data Volume (LDV) in Salesforce

Last year I presented how Query Optimizer works in Kitchner developer group. Found myself coming back to slide multiple times, so thought its good idea to post all information in this post.

Getting Started with Lightning Web Component

Before starting with Lightning Web Component, I would suggest to read this introduction post first.

If you enjoy watching video then you might enjoy below video where I conducted Live coding explaining basics of Lightning Web Component.

In this post, we are going to Show loading image on initial load of LWC. In background we would be calling Apex class to take decision if loading image should be hided or redirect user to some other page.

Continue reading “Getting Started with Lightning Web Component”

Cannot change both ‘UnitPrice’ and ‘TotalPrice’ in update call

Trigger to change UnitPrice in Salesforce

Recently, I was in need to update Sales Price (Unit Price) for OpportunityLineItem using Trigger. I wrote a small trigger on before insert and before update of OpportunityLineItem to update Sales Price.

It worked fine on insert however on update, even if user clicked on update button without changing any value, we were getting error Cannot change both ‘UnitPrice’ and ‘TotalPrice’ in update call.

Its Standard Salesforce behavior and seems in background platform tries to perform some calculation for Total Price which is Quantity * Sales Price. I was not able to get it working on before update. However, below two approach worked :

  1. Use Process builder to compute Unit Price as per formula. Process builder field update works after trigger so it went through.
  2. In after update trigger, create list of Opportunity Product to be updates and make explicit update call for all records which qualified for update.

Enterprise Territory Management – Key Notes

How to use Apex based sharing to share opportunity using Territories

  • Enterprise Territory management is used to share Account and its related opportunity and Cases.
  • Account can have multiple Territories.
  • User can have multiple Territories
  • Opportunity can have only 1 Territory. Think about it – If Opportunity allowed to have multiple Territory, how revenue would be split between multiple Territory and what would be business rules ?
  • Territory enables “My Territories” and “My Team’s Territory” filter in Account and Opportunity list View.
  • While creating Territory, we can decide access to Account, Opportunity and Case. Depending on OWD, access can be private, read only and read write.
  • Enterprise Territory Management does not support overlay forecast unlike Roles. However we can create report on Opportunity split object.
  • Overlay forecast (Revenue sharing) is not supported because Opportunity can have only one territory and therefore only 1 revenue maker.
  • Maximum Territory that can be defined is around 99k unlike maximum role 10k.
  • Custom Field (but not lookup) can be created in Territory2 object.
  • Territory related list appears on user and Account records.
  • You cannot create lookup to Territory objects.
  • Territory to Opportunity can be assigned using Apex class plugin

Enable Territory based Forecast

  1. Mark user as “Allowed Forecast”
  2. Add user as Territory Team member
  3. Add user as Forecast Manager in Territory

Apex Based Sharing of Opportunity using Territory

For Apex based sharing, we use user of public group Id to share record. If we want to share Opportunity using Territory then there is quick code for that. If we click on manual share button on Opportunity, it will show an option to choose between user, public group, territory and territory with subordinates.

There is no clear documentation, but what I found that when we share using Territory, Salesforce internally creates public group for that territory and assigns that public group to Opportunity.

So, first step is to find public group created for territory. Below code showcase how it can be done.

References :

Introduction to HTML Web Components

Pillars of HTML Vanilla Web Components

Modern browsers have become so advanced and powerful that there is almost no or minimal need of Javascript libraries these days. There was time when web developers rely heavily on frameworks like JQuery, Angular or React. Same for Salesforce, when it launched Aura framework in 2014.

In last 5 years, web browsers have evolved so much that we need to revisit and check ourselves that do we really need libraries or frameworks for every small functionalities ? Considering rich feature set browsers, Salesforce already revisited aura framework and decided that it’s time to make it more lightweight and use native browser capabilities as much as possible. Welcome Lightning Web Components !!! Before we jump into ocean of Lightning Web Components, it’s important to understand what is supported by browsers natively ? Out of many features supported by modern browsers, one of most important one is Web Component.

Below are 4 pillars of Web Components

  1. Templates
  2. Custom Element
  3. Shadow DOM
  4. Imports
Continue reading “Introduction to HTML Web Components”

Get Current User Id in Salesforce

How to get Current Logged in user Id in Apex, Visualforce, Lightning Component and Formula fields in Salesforce

Quick Code Snippets of how to get Current logged in user ids in Salesforce

Apex

System.debug('Current User Id - '+UserInfo.getUserId());

Visualforce

<apex:page>
  <h1>Visualforce Page</h1> 
   <p>UserId: {!$User.Id}</p>
   <p>User Email: {!$User.Email}</p>
</apex:page>

Aura Component

let currentUser = $A.get("$SObjectType.CurrentUser.Id");
Console.log(currentUser);

Lightning Web Components (LWC)

Using wire

import USER_ID from '@salesforce/user/Id';
import NAME_FIELD from '@salesforce/schema/User.Name';
import EMAIL_FIELD from '@salesforce/schema/User.Email';

Formula Fields

$User.Id