2

I am creating a custom form in lightning to create case. The default create case form shows all the read only fields and is not as user friendly. Using the solution mentioned in this post I created a form that dynamically retrieves fields from fieldset and renders the form.

The challenge I am having is when submitting the forms I am not able to retrieve the fields using their id. Setting aura-id in dynamically created components does not work and when using the html id attribute I am not able to retrieve it using document.getElementById. I need to validate to make sure required fields are populated and then submit values to apex controller to create case. Below is code from my component. Any help will be very much appreciated!

The component:

<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickAction" controller="FieldSetController">
    <aura:attribute name="values" type="Object[]"/>
    <aura:attribute name="fields" type="Object[]"/>
    <aura:attribute name="form" type="Aura.Component[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
&lt;fieldset class=&quot;slds-box slds-theme--default slds-container--medium&quot;&gt;
    &lt;legend class=&quot;slds-text-heading--small slds-p-vertical--medium&quot;&gt;
        Create New Case
    &lt;/legend&gt;
    &lt;form id=&quot;caseForm&quot; class=&quot;slds-form--stacked&quot;&gt;
        {!v.form}
    &lt;/form&gt;
&lt;/fieldset&gt;
&lt;ui:inputText label=&quot;testbox&quot; aura:id=&quot;testboxid&quot; value=&quot;test&quot;/&gt;

</aura:component>

The controller:

({
    doInit: function(component, event, helper) {
        console.log('**** INSIDE INIT');
        helper.getFields(component, event);
    },
doSubmit: function(component, event, helper) {
    console.log('called submit');
    helper.submitForm(component, event);
},

fieldOnChange: function(component, event, helper){
    console.log('value changed');
},

submit2: function(component, event, helper) {
    //debugger;
    console.log(component.get(&quot;v.form&quot;)[0].get(&quot;domId&quot;));
}

})

The helper:

({
/*
 *  Map the Schema.FieldSetMember to the desired component config, including specific attribute values
 *  Source: https://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_class_Schema_FieldSetMember.htm|StartTopic=Content%2Fapex_class_Schema_FieldSetMember.htm|SkinName=webhelp
 *
 *  Change the componentDef and attributes as needed for other components
 */
configMap: {
    &quot;anytype&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;base64&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;boolean&quot;: {componentDef: &quot;ui:inputCheckbox&quot; },
    &quot;combobox&quot;: { componentDef: &quot;ui:inputSelect&quot; },
    &quot;currency&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;datacategorygroupreference&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;date&quot;: { componentDef: &quot;ui:inputDate&quot; },
    &quot;datetime&quot;: { componentDef: &quot;ui:inputDateTime&quot; },
    &quot;double&quot;: { componentDef: &quot;ui:inputNumber&quot;, attributes: { values: { format: &quot;0.00&quot;} } },
    &quot;email&quot;: { componentDef: &quot;ui:inputEmail&quot; },
    &quot;encryptedstring&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;id&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;integer&quot;: { componentDef: &quot;ui:inputNumber&quot;, attributes: { values: { format: &quot;0&quot;} } },
    &quot;multipicklist&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;percent&quot;: { componentDef: &quot;ui:inputNumber&quot;, attributes: { values: { format: &quot;0&quot;} } },
    &quot;picklist&quot;: { componentDef: &quot;ui:inputSelect&quot; },
    &quot;reference&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;string&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;textarea&quot;: { componentDef: &quot;ui:inputTextArea&quot; },
    &quot;time&quot;: { componentDef: &quot;ui:inputDateTime&quot;, attributes: { values: { format: &quot;h:mm a&quot;} } },
    &quot;url&quot;: { componentDef: &quot;ui:inputText&quot; },
    &quot;button&quot;: { componentDef: &quot;ui:button&quot; }
},

// Adds the component via newComponentAsync and sets the value handler
addComponent: function(component, facet, config, fieldPath) {
    if(config.attributes.values.fieldtype === 'button'){
        console.log('Inside creating button')
        $A.createComponents(
            [
                [&quot;aura:html&quot;,{
                &quot;tag&quot;: &quot;div&quot;,
                HTMLAttributes:{&quot;class&quot; : &quot;slds-form-element&quot;},
                }],
                [config.componentDef,{
                &quot;label&quot;: &quot;Submit&quot;,
                &quot;buttonType&quot;: &quot;button&quot;,
                &quot;class&quot;: &quot;slds-button slds-button--brand&quot;,
                &quot;press&quot;: component.getReference(&quot;c.submit2&quot;)
                }]
            ],
            function(components, status, errorMessage){
                if(status === &quot;SUCCESS&quot;){
                    var cmp1 = components[0];
                    var cmp2 = components[1];
                    cmp1.set(&quot;v.body&quot;,cmp2);
                    facet.push(cmp1);
                }
                else if(status === &quot;INCOMPLETE&quot;){
                    console.log(&quot;No response&quot;);
                }
                else if(status === &quot;ERROR&quot;){
                    console.log(&quot;ERROR: &quot; + errorMessage);
                }
            }
        );
    }
    else{
        $A.createComponents(
            [
                [&quot;aura:html&quot;,{
                &quot;tag&quot;: &quot;div&quot;,
                HTMLAttributes:{&quot;class&quot; : &quot;slds-form-element&quot;},
                }],
                [&quot;aura:html&quot;,{
                    &quot;tag&quot;:&quot;label&quot;,
                    HTMLAttributes:{&quot;class&quot; : &quot;slds-form-element__label&quot;}
                }],
                [&quot;aura:html&quot;,{
                    &quot;tag&quot;:&quot;div&quot;,
                    HTMLAttributes:{&quot;class&quot; : &quot;slds-form-element__control&quot;}
                }],
                [config.componentDef,{
                    &quot;domId&quot; : fieldPath,
                    &quot;label&quot;: config.attributes.values.label,
                    &quot;required&quot; : config.attributes.values.required,
                    &quot;onchange&quot; : component.getReference(&quot;c.fieldOnChange&quot;)
                }]
            ],
            function(components, status, errorMessage){
                if(status === &quot;SUCCESS&quot;){
                    var cmp1 = components[0];
                    var cmp2 = components[1];
                    var cmp3 = components[2];
                    var cmp4 = components[3];

                    if(config.attributes.values.fieldtype == 'PICKLIST')
                        cmp4.set(&quot;v.options&quot;,config.attributes.values.picklistopts);
                    /*cmp4.set(&quot;v.id&quot;,fieldPath);
                    cmp4.set(&quot;v.name&quot;,fieldPath);*/

                    console.log('*** ' + cmp4.getLocalId());
                    cmp1.set(&quot;v.body&quot;,cmp2);
                    cmp1.set(&quot;v.body&quot;,cmp3);
                    cmp3.set(&quot;v.body&quot;,cmp4);

                    facet.push(cmp1);
                }
                else if(status === &quot;INCOMPLETE&quot;){
                    console.log(&quot;No response&quot;);
                }
                else if(status === &quot;ERROR&quot;){
                    console.log(&quot;ERROR: &quot; + errorMessage);
                }
            }
        );
    }
},

// Create a form given the set of fields
createForm: function(component, fields) {
    var field = null;
    var cmp = null;
    var def = null;
    var config = null;
    var self = this;
    var btn = {&quot;label&quot; : &quot;Submit&quot;,
                &quot;type&quot; : &quot;button&quot;,
                &quot;required&quot; : &quot;false&quot;};
    fields.push(btn);

    // Clear any existing components in the form facet
    component.set(&quot;v.form&quot;, []);

    var facet = component.get(&quot;v.form&quot;);
    var values = component.get(&quot;v.values&quot;);
    for (var i = 0; i &lt; fields.length; i++) {
        field = fields[i];
        // Copy the config, note that this type of copy may not work on all browsers!
        config = JSON.parse(JSON.stringify(this.configMap[field.type.toLowerCase()]));
        // Add attributes if needed
        config.attributes = config.attributes || {};
        // Add attributes.values if needed
        config.attributes.values = config.attributes.values || {};

        // Set the required and label attributes
        config.attributes.values.required = field.required;
        config.attributes.values.label = field.label;
        config.attributes.values.fieldtype = field.type;
        if(field.type == 'PICKLIST'){
            var opts = [];

            for(var j=0; j &lt; field.picklistoptions.length; j++){
                opts.push({class: &quot;optionClass&quot;, label: field.picklistoptions[j], value: field.picklistoptions[j]});
            }
            config.attributes.values.picklistopts = opts;
        }

        // Add the value for each field as a name/value
        values.push({name: field.fieldPath, value: config});
        // Add the component to the facet and configure it
        self.addComponent(component, facet, config, field.fieldPath);

    }
    component.set(&quot;v.form&quot;, facet);
    component.set(&quot;v.values&quot;, values);
    console.log('Values list ' + values);
},



getFields: function(component) {
    var action = component.get(&quot;c.getFields&quot;);
    var self = this;
    var typeName = component.get(&quot;v.type&quot;);
    var fsName = component.get(&quot;v.fsName&quot;);

    action.setCallback(this, function(a) {
        var fields = a.getReturnValue();
        component.set(&quot;v.fields&quot;, fields);
        self.createForm(component, fields);
    });
    $A.enqueueAction(action);
},

submitForm: function(component, event) {
    //var values = component.get(&quot;v.values&quot;);
    //var s = JSON.stringify(values, undefined, 2);
    console.log('submit called');

}

})

isherwood
  • 442
  • 1
  • 6
  • 19
Vinod Agrawal
  • 133
  • 1
  • 7
  • Setting the aura:id on a dynamically created component is a known issue. See http://salesforce.stackexchange.com/questions/148634/dynamically-created-component-with-auraid-set-as-a-facet-inside-a-parent-compo/148845#148845. Could you clarify a bit where exactly in this repro you're trying the following: "when using the html id attribute I am not able to retrieve it using document.getElementById". – TrevorBliss Dec 15 '16 at 03:25
  • Did you ever get this resolved? I am curious how to got the fieldsetmember class to populate picklistoptions. Can you post your apex code? – Allen Mann Feb 12 '18 at 21:47

0 Answers0