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.

No comments:

Post a Comment

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 ...