Lightning, Lightning, Lightning… With each new day we found something interesting in Lightning. There are many new things coming in lightning that’s make us to try our hand on all of them. Previously if we need to create Edit or detail page we need to manually create them with all custom code and then we can use Lightning:recordEditForm or LightningDataService.
But that still take lots of code. But now we have lightning:recordForm, which enables usto quickly create forms to add, view, or update a record. Using this component to create record forms is easier than building forms manually with lightning:recordEditForm
and lightning:recordViewForm
.
Using a single line of code we can create full page layout.
<lightning:recordForm recordId="001XXXXXX" objectApiName="Account" layoutType="Full" mode="view" />
But what if we need to add some custom validation before creating record or need to check some detail or update some fields before saving the record then we can also customize this as well. Today we will take an example where we will use onsubmit to save the record and then redirect to record detail page.
Here for demo purpose I didn’t done much customization but you can make changes. Also don’t forget to stop the standard flow else it will create two records for you.
As this component is generic so you can use it with any object to quickly get the edit page or create new record.
Did you want to add anything or have any question, please let me know in comments section. Happy programming 🙂
Is this code working for you because in your Apex code you have taken map and while putting value in that map, for each field of data type other than String it will give error “Illegal Assignment from String to Date” (for example).
Thanks Abhishek. I have updated that part. But we need to handle the DateTime part. it will be different for different orgs. Current code is working in my org.
Thanks for the info Tushar. I have a question on whether we can group set of fields as sections using lightning:recordForm?
As we are giving layoutType here so section made in layout should come here. I’m checking it in detail and will update if I found anything useful.
Can we update the record details using this code.??
Yes we can. In the sample I am updating record.
Hi Tushar, Can we create a dependent layout using this.
Example:
1. If a picklist value= A
Then only section A has to be shown
1. If a picklist value= B
Then only section B has to be shown
If you already have dependent picklist on standard page then you can view them here
Hi Tushar, can we make fields required like we can in page layouts
I don’t think its possible.
Alright thanks, how can we apply custom css like an asterisk on fields
I don’t think thats possible here. You can hardcode class name but I don’t think that will work.
Searching for lightning “createRecords” brought me here, but unfortunately this only saves one record 🙁
Looking for a bulk implementation of lightning/uiRecordApi’s “createRecord” method