Thursday, 15 September 2022

Dynamic Scatter chart in Vf page using ChartJs


SCENARIO


While working on one of the requirements for an Automobile sector project for a client based out of Dallas, Texas, There was a requirement to display dynamic data responsively into a Scatter chart using the VF page. 

CHALLENGE


The “Standard Report Chart” component can be used to display the chart on the page but we have several custom logic and filter to be implemented in a VF page then the Standard Report Chart” has filter Restrictions. To prevent that problem, we have used ChartJs.
 

APPROACH


We have used the VF page and apex controller to display dynamic data for the Account standard object. We have taken the Number of Quantities as the X axis and the Amount as the Y axis for plotting the data dynamically in this scatter chart using chartJs.

Below is an example of a scatter chart.

Example:-

In the example, we have drawn a scatter chart to display the Account amount field and No of quantity field data.

Step 1:- Create an Apex controller with the name 'ChartVfController'.

Retrieving Account data and setting on the VF page.

Step 2:- Create a VF page with the name 'ChartVf'.

VF page consists of a canvas tag, and the ChartJs library uses that canvas to draw the chart.

The most important thing is chart configuration, all the chart behavior is based on the config that we set to the chart. Below are main three attributes.
  • type:- It defines the type of chart being rendered, here we have 'scatter'. 
  • data:- Here we pass the chartLabels and chartData and other settings for the scatter chart.
  • options:- Additional settings for chart.  

Output:-



If you have any questions you can reach out our Salesforce Consulting team here.

Thursday, 8 September 2022

Notify Case Creator when something is posted/commented on Chatter using Salesforce Flow

SCENARIO


While working on one of the requirements for an Electric Automotive Company based in Sweden, there was a requirement to send an Email to the Case creator when something is posted or commented on chatter.

APPROACH


As the requirement is pretty straightforward, The client wants to send an email to the case creator. We have many ways to achieve this requirement like Apex Code, but Salesforce encourages us to code less.

So, we have used Record Trigger Flow to fulfill this requirement.

Below are the steps to achieve the requirement:

Step 1:-  Define flow properties.

  • Click Setup.
  • In the Quick Find box, type Flows.
  • Select Flows then click on the New Flow.
  • Select the Record-Triggered Flow option and click on Next and configure the flow as follows:
    • How do you want to start building: Freeform

Step 2:- Select Object and Set Entry Condition. 

Give Input as follows:
  • Object: Feed Item.
  • Trigger the Flow When: A record is created or updated.
  • Condition Requirements: All Conditions Are Met(AND).
  • Field: ParentId.
  • Operator: Starts With.
  • Value:500(Case Object record Id starts with "500...").
  • When to Run the Flow for Updated Records: Every time a record is updated and meets the condition requirements.
  • Optimize the Flow for Actions and Related Records. 


Step 3:- Get Case Records. 

Drag Get Record Element into canvas from Data Section under Elements Tab from the ToolBox & Give Input as follows:
  • Object: Case.
  • Condition Requirements: All Conditions Are Met(AND).
  • Field: Id.
  • Operator: Equals.
  • Value:{!$Record.Parent:Case.Id}.
  • How Many Records to Store: Only the first record.
  • How to Store Record Data: Automatically store all fields.
  • Join start Element to Get Element.



Step 4:- Create a Decision. 

Drag Decision Element into canvas from Logic Section under Elements Tab from the ToolBox & Give Input as follows:
  • Condition Requirements to Execute Outcome: All Conditions Are Met(AND).
  • Resource: {!Get_Case_Records.CreatedBy.Id}.
  • Operator: Is Null.
  • ValueFalse.
  • When to Execute Outcome: If the condition requirements are met.
  • Join Get Element to Decision Element.

Step 5:- Define Body and Subject Variables for Email.

We need to create flow variables for the Body & Subject portion of the email.
Click New Resource under Elements Tab from the ToolBox & Give Input as follows:
  • Resource Type: Text Template.
  • API Name: TextTemplate.(Write as per your convenience)
  • Body: Comment has been Made. (Write as per your convenience)
  • Click Done.
Repeat the same above steps for Subject.
  • Resource Type: Text Template.
  • API Name: SubjectTemplate.(Write as per your convenience)
  • Body: Hi How are you?. (Write as per your convenience)
  • Click Done.

Step 6:- Send an Email. 

Drag Action Element into canvas from Interaction Section under Elements Tab from the ToolBox, scroll down the list from the left-hand side and select Email from the bottom, in the Action select Send Email & Give Input as follows:
  • Set Body & Subject variables in their respective input field.
  • Press the Don't Include Button for RecipientEmailAddresses(comma-separated) and set it to Include(True).
  • RecipientEmailAddresses(comma-separated):{!Get_Case_Records.CreatedBy.Email}.
  • Click Done.
  • Join Decision Element to Email Action Element for Case Creator.


Step 7:- Now Save the flow & then Activate it.

FLOW DIAGRAM




CONCLUSION


By following the steps mentioned above you can send an Email Notification to Case Creator when something is Posted/Commented on the Chatter.

If you have any questions you can reach out our Salesforce Consulting team here.

Thursday, 1 September 2022

How to use ChartJS to generate a chart in a lightning web component?

The age of Data Science nowadays places a lot of importance on data visualization. Visual representation of the data puts more impact on the user interface. But the issue is to build custom, responsive, reactive charts and graphs in salesforce. Open source is one of the best sources for this kind of solution. The ChartJs is an open source solution that powers, to create charts and graphs that are completely responsive, accessible, and reactive, giving the best performance. By using ChartJs in salesforce, we can create custom charts and graphs.

In this blog, We will see how we can leverage the power of ChartJs to build a responsive Bar chart in LWC. We will see how to pull information from an Opportunity and feed it into the bar chart.

Why use ChartJS?

The charts can be added to the pages using the App Builder’s “Standard Report Chart” component. However, the report filters can be used to display relevant data on the charts but when we have a lot of custom logic and filters to be implemented in a lightning component then the “Standard Report Chart” have filter limitations. ChartJs is a handy solution for this. Let's jump for example.

Example:-

In the example we will draw a bar chart for latest 10 Opportunity amounts whose stage is Closed won.

Step 1:- Download ChartJs library.
  • Click Here to open link and right click on page and save as.
  • Upload file in Static Resource with the name 'ChartJS'.

Step 2:-
 Create a LWC with the name 'chart'.
HTML consist a canvas tag, the Chartjs library uses that canvas to draw the chart.
  
Here we have declared two @api attributes in the chart component
  • loaderVariant - Used to set the loader variant from the parent component.
  • chartConfig - Used to pass the chart settings and data from the parent component, so that we can reuse this component for different charts.


Step 3:- Create an apex class to get Opportunity data.
Retrieving Opportunity data.

Step 4:- Create  parent LWC named 'barChartExample'.
Calling child component chat and set chart configration.

The wire method gets the opportunity data and feeds it into the chart lwc.

The most important thing is chartConfiguration, all the chart behaviour is based on the config that we set to the chart. Below are main three attributes.
  • type:- It defines the type of chart being rendered, here we have 'bar'. 
  • data:- Here we pass the chartLabels and chartData and other settings for the bar chart.
  • options:- Additional settings for chart.


Output:-



If you have any questions you can reach out our Salesforce Consulting team here.