Salesforce UI API Lightning Example

In this blog, I am going to explain how to use Salesforce UI API.Salesforce has the number of APIs to build the native and mobile apps.  Salesforce UI allows you to build native mobile apps and custom web apps. The Great advantage of the Salesforce UI API, you get data and metadata in a single response. You don’t have to worry about layouts, picklists, field-level security, or sharing.

Step 1: Create Connected App 

Create a salesforce connected app as shown below. We need to create a named credential to authenticate the web service call. Go to setup –> create apps–> create a new connected app as shown below and save it to get consumer key and secret key. In this step use any URL as callback URL. later after confirmed the auth provides we will replace the callback URL from auth provider callback URL

After saving the connected app you will get consumer key and consumer secret which we will use to configure the Auth provides.

Step 2: Auth Provider 

Now we need to create an auth provide as shown below. Go to setup –> administration  –> Security control -> create a new Auth provider as shown below .

Replace the connected App callback URL with Auth provide callback URL

Step 3: Named credentials 

Create a named credentials as shown below for authenticating salesforce REST API from the lightning component. 

Code :

Below is the apex class that used to get the data and metadata from the Salesforce UI API.Based on the account selected, you will get the response which contains data and metadata.

Create a lightning component Component that will display the data based on the selected account from the drop-down.

helper

 

 

Salesforce UI API

In this blog, I am going to explain how to use Salesforce UI API.Salesforce has the number of APIs to build the native and mobile apps.  Salesforce UI allows you to build native mobile apps and custom web apps. The Great advantage of the Salesforce UI API, you get data and metadata in a single response. You don’t have to worry about layouts, picklists, field-level security, or sharing.

Step 1: Create Connected App 

Create a salesforce connected app as shown below.  In this post, i am using named credential to authenticate the web service call. Go to setup –> create apps–> create a new connected app as shown below and save it to get consumer key and secret key. In this step use any URL as callback URL. later after confirmed the auth provides we will replace the callback URL from auth provider callback URL

After saving the connected app you will get consumer key and consumer secret which we will use to configure the Auth provides.

Step 2: Auth Provider 

Now we need to create an auth provide as shown below. Go to setup –> administration  –> Security control -> create a new Auth provider as shown below .

Replace the connected App callback URL with Auth provide callback URL

Step 3: Named credentials 

Create a named credentials as shown below for authenticating Salesforce REST API.

Code :

Visualforce page
Create a visualforce page that shows the data and metadata from based on selected account.

 

 

Lightning Attributes unbound vs bound bindings

Introduction:-

In this blog, I am going to explain how to use salesforce attribute unbound vs bound binding. Attributes are key parts of a web component-based framework like lighting, Polymer and react etc which are used to communicate or exchange data between components. You can think it like a member variable in Java. Here is the example of attributes

Now if you see the below components having an attribute name message which is referred in the component by using {!v.message} with {!} expression know as the bounded expression. Salesforce supports two types of expression bounded and unbounded to pass the attribute values.

Expression Binding: –

Salesforce supports two type of binding expression in Salesforce namely bounded and Unbounded.  The two forms exhibit different behaviors for data binding and updates when you pass an expression from a parent component to a child component that it contains.

{! Expression} (Bound Expressions)

Data updates in either component are reflected through bi-directional data binding in both components. Similarly, change notifications are triggered in both the parent and child components. Means if you update any attributes in parent components those changes will be updated to child component attributes.

{#Expression} (Unbound Expressions)

Data updates behave, as you would expect in JavaScript. Primitives, such as String, are passed by value, and data updates for the expression in the parent and child are decoupled.Objects, such as Array or Map, are passed by reference so changes to the data in the child propagate to the parent. However, change handlers in the parent won’t be notified so it will not be re-rendered. The same behavior applies for changes in the parent propagating to the child.

Bi-directional data binding is expensive for performance and it creates hard-to-debug errors due to the propagation of data changes. So its recommend using the {#expression} syntax instead when you pass an expression from a parent component to a child component unless you require bi-directional data binding.

Here is the simple Component ChildExp1.cmp with bounded expression

 

Here is the parent Component which is passing referring the child component to and pass the attributes by using Bound expression

 

If you click on Update Message – Parent  Button those changes will be now propagated to the child component and vice versa which is similar to two-way data binding. 

 

Now Replace the child component invocation in the Parent Component with the below code by using Unbound Expression Now the parent component changes will not propagate to the child component which acts like as unidirectional binding. 

Now When you click Update Child Component data its will update only child component attributes and when you click on the Update Message -Parent parent component its update the parent component data which is unidirectional data binding. Now let see how handler works with unbound and bound expression.When you use a bound expression, a change in the attribute in the parent or child component triggers the change handler in both components. When you use an unbound expression, the change is not propagated between components so the change handler is only triggered in the component that contains the changed attribute.

Now update childExpr1.cmp with below code

Here is the controller

Here is the ParentExpr1.cmp

Press the Update parentAttr button. The change handlers for c:parentExpr1 and c:childExpr1 are both triggered as we’re using a bound expression.

Now Change c:parentExpr1 to use an unbound expression instead as like this. <c:childExpr1 childAttr=”{#v.parentAttr}” />  which use an unbound expression, the change is not propagated between childExp1.cmp and parentExpr1.cmp .

 

 

 

Lightning Aura.Action Attributes

In this blog, I am going to explain salesforce lightning Aura.Action Attribute Type.With the Aura.Action attribute type you can able to pass the actions handlers from the parent component to child components such as on-click or other HTML actions like on** actions.  Although Aura.Action works for passing an action handler to a child component, its recommended for registering an event in the child component and firing the event in the child’s controller instead. Then, handle the event in the parent component. This example demonstrates how to pass an action handler from a parent component to a child component.Here’s the child component with the Aura.Action attribute. The on-click handler for the button uses the value of the on-click attribute, which has the type of Aura.Action.Create the new Lightning component with below code.
AuraAction.cmp

The above component is having two Aura.Action attributes which are used to pass from the parent component to child component on invoke on button press and click on div section handler actions.

AuraActionInvoke.cmp

The below component  is parent component which passes the action to the child component

Simple Auraapp.app

Now if you see the above app, when the user clicks on the Press Me button it will pass the action handler from the parent component to child component. Similarly, even user click on Click Me text also passes the action handler to child component and update the parent component.

 

Lightning Aura method

In this blog, I am going to explain how to use the aura: method which allows you to directly call a method in a component’s client-side controller instead of firing and handling a component event.Here is the simple component “AuraMethod” which contains method “defaultParamers”  with the action called loadDefaultParams

In the component controller doInit method I am calling the component  method with the name as shown below

which hooks the loadDefaulParams method action to retrieve the values from the component action handler.

Calling Server Side Action

Now let’s consider the defaultParamers aura:method calls a server-side controller action to pass the method parameters. now server-side controller will return the result asynchronously so to handler the response we invoke the callback passed into the aura: method and set the result as a parameter in the callback.

 Define the apex class as shown below

Modify the component as shown below.

Now in the controller, we have defined the “onSucess” callback function to handle the server side controller response. But in the below controller I am simply printing the server side response on callback function to console.

Parent component to call a method on a child component

 aura: method is used to communicate down the containment hierarchy means from the parent component to child components.To communicate up the containment hierarchy, fire a component event in the child component and handle it in the parent component.Here are the child components

AuraMethod.cmp

Here is the below parent component which is used to pass the parent component data to the child component of the button click.

AuraMethodInvoke.cmp

 

AuraMethodTest.app

when the user clicks on the parent component button which will get the data from the parent component attributes and pass it to the child component.

Method with Interface

Define the simple interface with the aura method as shown below.

AuraMethodIntf.intf

And then you can implement the interface as shown below.