2

I've used 2 different tutorials from sfdcmonkey to try to create a multirow data entry page. The multirow tutorial:http://sfdcmonkey.com/2017/08/09/add-delete-rows-dynamic/ and a lookup field tutorial: http://sfdcmonkey.com/2017/07/17/re-usable-custom-lookup

While these two tutorials work great on their own, when I combine them I am not getting a value to save from the lookup field. I keep getting this error:

Action failed: c:dynamicRow$controller$Save [Cannot read property 'Id' of undefined] Failing descriptor: {c:dynamicRow$controller$Save}

I'm not sure what I'm missing. I didn't want to post all the code since it's identical to what is in the tutorials but this is the code I have changed:

On Add Delete Rows tutorial - dynamicRowController.js I tried to add the code form the lookup tutorial "How to Use" Javascript controller saveContactRecord function:

    Save: function(component, event, helper) {
    var conObj = component.get("v.contactList");
       conObj.AccountId = null ; 
    if(component.get("v.selectedLookUpRecord").Id != undefined){
      conObj.AccountId = component.get("v.selectedLookUpRecord").Id;
    } 
    if (helper.validateRequired(component, event)) {
        var action = component.get("c.saveContacts");
        action.setParams({
            "ListContact": conObj
        });

And on dynmaicRowItem.cmp I added this attribute near the top

<aura:attribute name="selectedLookUpRecord" type="sObject" default="{}"/>

and this below the phone

    <td>
        <c:customLookup objectAPIName="account" IconName="standard:account" label="Account Name" selectedRecord="{!v.selectedLookUpRecord}"/>
    </td>

I tried component.get("v.selectedLookUpRecord.Id") as well and while there is no error, there is also no data.

Thanks in advance for any help!

  • It might be helpful to post how all the various components are interacting. Otherwise, If you do console.log(component.get("v.selectedLookUpRecord")) do you see any values in console? – Kris Goncalves Nov 15 at 1:47
1
+200

The reason you're getting the error (which doesn't provide an answer to the real basis your question) is because of this:

if(component.get("v.selectedLookUpRecord").Id != undefined){
  conObj.AccountId = component.get("v.selectedLookUpRecord").Id;
} 

Basically, it looks like component.get("v.selectedLookUpRecord") is returning a null and hence the error when you try to access .Id of null. To prevent that, you'd first need to perform a null check on v.selectorLookupRecord, e.g.:

if(component.get("v.selectedLookUpRecord") &&  component.get("v.selectedLookUpRecord").Id != undefined){
  conObj.AccountId = component.get("v.selectedLookUpRecord").Id;
} 

But you can ignore all that, it still wouldn't give you what you are looking for. So why is v.selectedLookUpRecord null? I think you have composed things slightly incorrectly and are trying to access things in the wrong place. If you think about it, there are many contacts in the list and in that Save method when you declare var conObj = component.get("v.contactList"); what you're actually referring to there is a list of Contacts rather than one. So to set AccountId on conObj would not work anyway because conObj is a list of items rather than a singular item.

I think you should focus on dynamicRowItem.cmp and its controller dynamicRowItemController.js.

What I would be tempted to do would be to have a hidden field in dynamicRowItem.cmp accompanied by the Lookup, so instead of:

<td>
    <c:customLookup objectAPIName="account" IconName="standard:account" label="Account Name" selectedRecord="{!v.selectedLookUpRecord}"/>
</td>

Try:

<td>
    <ui:inputText class="slds-input slds-hide" value="{!v.ContactInstance.AccountId}"/>
    <c:customLookup objectAPIName="account" IconName="standard:account" label="Account Name" selectedRecord="{!v.selectedLookUpRecord}"/>
</td>

The custom lookup component is what the user sees and interacts with, and the ContactInstance.AccountId is what gets saved. The trick is then getting the value from v.selectedLookUpRecord into ContactInstance.AccountId because then, you can do away with your changes to the Save method in the dynamicRowController.js because the Id will already be set on the AccountId of the Contact and saved with the rest of the data.

To do this, I would look at the AddNewRow event handler, take the value of v.selectedLookupRecord and copy it to v.ContactInstance.AccountId. You could also think about bubbling up the selectedsObjectRecordEvent.evt to the dynamicRow.cmp to do the same.

PS. I haven't tried any of this, but looking at the examples think it might work.

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.