There is a use case in which we have to build a form using lightning-record-form. We have to display the set of fields from different objects into that form in different accordion sections.

I was going through the documentation, it says that lightning-record-form accepts only one Object name and only one record Id.

So, is there any way that I can show fields of different objects in a single form using lightning-record-form in LWC?

  • Its not possible with any of the standard forms. Why cant you use different forms in each accordian? – salesforce-sas Jun 5 '20 at 19:21
  • But then there has to be only one Save button for the entire form. And other than standard forms is there any other way to achieve this requirement? In which the form must behave in both View /Edit modes? – sfdc07 Jun 6 '20 at 19:17
  • You can programmatically save() all forms – salesforce-sas Jun 6 '20 at 19:19
  • Its like each accordion will have fields from different objects! and each record form has its own Save button implicitly. If in case I go with different record forms then there would be multiple Save buttons..! Every accordion has mixed fields from different objects! – sfdc07 Jun 6 '20 at 19:23
  • You are right, you will get save and cancel buttons by default. You should probably go with record-edit-form – salesforce-sas Jun 6 '20 at 19:26

Assuming that you have to use a lightning-record-form, and that you can not accomplish this using a combination of lightning-record-forms, you could first combine the different objects field data into a single custom object that effectively allows you to "stage" things as needed to overcome the 1 object 1 record Id constraint. This approach is likely more time consuming than seeing if the use case or requirements can be adjusted to overcome the constraint with more customizable functional or code-based approaches, given that you would have to account for the logic and independently triggered mechanisms needed to move and correctly combine all the different object field data.

  • Okay, Got you..! So other than lightning:record-form, is there any other way of designing a form which can support this requirement? Again the form should be in View mode initially and when user edits the form all the fields should be editable. Any other custom way to achieve this? if standard forms is not possible! – sfdc07 Jun 6 '20 at 19:15

There are couple of approaches you can try,

  1. You can submit multiple recordEditFroms with one single button by using javascript methods as below:-
singleOnSubmitMethod : function(component, event, helper) {

However this might not work if you hide it inside accordion, not sure if a form is hidden in one accordion section then whether it will be available in DOM or not.

  1. Create set of plain lightning:input fields without any forms, set their values as object's field api names, also 2 attribute of type objects say account & opportunity on aura cmp UI side and one last submitt button to call a js controller which in return call an apex method to save your data. This i have implemented and works like charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.