0

I have what at first seems to be a simple question, but I can't seem to pin it down. I'm working with the lightning:recordForm, and I'm sending the fields dynamically based off a callout where I'm populating the {!v.fields}. I have tried multiple ways to structure the variable so that it will render the correct fields, but the best I seem to get is it will render the first field in the list. Documentation says the structure should be String[] and be in the form of "['Item1', 'Item2']". Maybe its my own shortcomings on the intricacies of JavaScript types, but I can't get it to work. Note that one sticking point that may be part of the issue is that my callout returns an object with a string of fields and those need to be split and stored in the list. Here are the relevant snippets of my code:

 <aura:attribute name="fields" type="String[]"/>
 ...
 <lightning:recordForm objectApiName="{!v.thisObject}"
                                                      fields="{!v.fields}"
                                                      recordTypeId="{!v.selectedRecordType}"
                                                      columns="2"
                                                      oncancel="{!c.handleCancel}"
                                                      onSubmit="{!c.handleSubmit}"
                                                      onsuccess="{!c.handleSuccess}"/>

Controller:

    var fieldsList = custRecCreateList[i].Fields__c.split(",");      
    for(var j=0; j<fieldsList.length; j++){
                fields[j] = fieldsList[j]; //I've also tried fields.push(fieldsList[j]) here
            }
    ....
    component.set("v.fields", fieldsList);

Thanks in advance for your help!

1 Answer 1

0

You don't need the complicated dance you're trying to go through:

component.set("v.fields",
  custRecCreateList[i].Fields__c.split(",")
);

However, one caveat here: if you have extra spaces in the list, that'll screw things up. You might want to go with:

component.set("v.fields",
  custRecCreateList[i].Fields__c.split(/,\s*/)
);

This trims any extra spaces after the comma.

1
  • You were spot on with the whitespace comment. That was exactly my problem. Thanks so much for your help! Sep 17, 2020 at 12:27

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.