Tuesday, 8 May 2018

Create record in lightning

With the advancement of lightning, we are getting introduced to new features of lightning that helps in creating a better functionality in short span of time.
One of the example is force:createRecord.
To understand this lets go through a scenarion. We have a lightining component where we have a text box to enter account name and a button "Search".
On click of button we query the account records matching the name given in textbox and display in a form of table.
If it doesn't have any record with the name given in textbox then we want to create an account with going away from the lightning page.
In this case we use force:createRecord.

We create a button named as "Create Account". Onclick of the button we call a method in lightning controller.








Below is the code of the method.












If you notice, we have used an event ie "e.force:createRecord" and assign them some parameters and finally firing the event.
This opens account edit form in a popup with prepopulated value. You can input values and click on save. This will create a new account record in the database without hitting server apex class.


Parameters used in force:createrecord :-
1. Entityapiname --> In this we need to provide the Object Api name whose record we want to create. In our case this parameter value will be "Account".
2. defaultFieldValues --> If we want to prepopulate the field values in the popup while creating record, we can assign the value to the fieldname using this parameter.
3. recordTypeId --> If your object is using recordType thenyou can specify recordTypeId.

Use Lightning Component In VF Page

In this blog we will see how we can use the lightning component in visualforce page.
As a first step lets create a lightning component which takes account Id as input and display the Account Id in lightning component.

Lightning Component :-










Once the lightning component is created, we need to create a lightning app which will contain the component. The lightning app should have "ltng:outApp" interface. This interface makes the lightning app visible on visualforce page.

Lightning App :-






Once the lightning app is created, lets create a visualforce page that will display the lightning app. Below is the screenshot of the page.




In the above code, we are using <apex:includeLightning/> which enables the inclusion of lightning app on visualforce page. also there should be an empty div element which will contain the lightning component.
Once the page is loaded, we use $Lightning.use to load the lightning application. Then we use $Lightning.createComponent to initialize the lightning component dynamically in the page. During component initialization we pass 4 parameters ie :

1. In the first parameter Lightning component name.
2. In the second parameter we will assign the values to lightning component attributes.
3. In 3rd parameter we give the div section where we need to display the component.
4. In 4th parameter we will define a function that fires once the component is initialized.

In the above example you can notice that we are passing the account Id to the lightning component which will be displayed in the component.
the above example shows the complete process of using the Lightning component in visualforce page.

Create record in lightning

With the advancement of lightning, we are getting introduced to new features of lightning that helps in creating a better functionality in ...