I have a requirement where I need to insert and update a contact. Now, I am using force:recordData
.
<force:recordData aura:id="ldsContact"
recordId="{!v.selectedUserId}"
layoutType="FULL"
mode="EDIT"
targetRecord="{!v.record}"
targetFields="{!v.imContact}"
targetError="{!v.recordError}" />
When I try to edit a record, it is working fine, but when I try adding, it gives following error:
We couldn't find the record you're trying to access. It may have been deleted by another user, or there may have been a system error. Ask your administrator for help.
I understand, that the above error is most likely due to the recordId
attribute and as per the documentation
To create an empty record from a record template, you can’t set a recordId on the force:recordData tag. Without a recordId, Lightning Data Service doesn’t load an existing record.
So, my concern is, I don't want to repeat myself just because of the above issue and use another force:recordData
just for adding a new record, as there is a huge work associated with the one I am already using. Any way I could use one force:recordData
for both Insert & Update?
force:recordData
and base them on the same attributes? what is the 'huge work associated with the one I am already using'? – Itai Shmida Dec 10 '18 at 7:43force:recordData
has around 10 fields, absolutely same, and their CSS, customization, JS, and opening them in a pop up are few details of the work I was referring to. In the JS controller of my cmp, I am referring the fields throughaura:id
, and since there are many behaviors associated with the fields which aren't gonna change whether I add or edit, but with 2force:recordData
I would need to repeat the code in JS and CSS and also the mark up. I hope that clarified your question. – Mahmood Dec 10 '18 at 7:58force:recordData
you can still use the same attributes - no need to duplicate – Itai Shmida Dec 10 '18 at 10:26