0

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?

  • I don't think this is possible, why cann't you use 2 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:43
  • Each force: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 through aura:id, and since there are many behaviors associated with the fields which aren't gonna change whether I add or edit, but with 2 force: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:58
  • 1
    OK, but if you create another force:recordData you can still use the same attributes - no need to duplicate – Itai Shmida Dec 10 '18 at 10:26
  • I didn't really think of that, but now it makes sense. If put this as an answer, and if possible, explain it with an example as well, it would be helpful for other readers. :-) – Mahmood Dec 10 '18 at 10:35
1

I suggest to add another force:recordData without the id, and use the same attributes inside both of the components:

<force:recordData aura:id="ldsContactEdit"
     recordId="{!v.selectedUserId}"
     layoutType="FULL"
     mode="EDIT"
     targetRecord="{!v.record}"
     targetFields="{!v.imContact}"
     targetError="{!v.recordError}" />

<force:recordData aura:id="ldsContactAdd"
     layoutType="FULL"
     mode="EDIT"
     targetRecord="{!v.record}"
     targetFields="{!v.imContact}"
     targetError="{!v.recordError}" />      

This way you do not need to add more work, just use your current one, and you can choose in your javascript controller if to use ldsContactAdd to add a record, or ldsContactEdit to edit one.

| improve this answer | |

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.