I have a issue, I'm trying to create a Dynamic Form with two lightning Components and fieldsets, each component's related to an Object 'WorkOrder' and 'WorkOrderLineItem'. I pass the API names of each object and field set via Lightning App Builder page designer. The problem that I've had is when I'm trying to save the form. the console show me this error on "handleSubmit" from ComplexWOFormController.js "Error message: Invalid literal value [1, 1]: 'WorkOrder'" I can't understand where's come from, I don't know if is from the Aura attribute definition.

Can anyone help me? -

--- ComplexWOForm.cmp---

<aura:component controller="FSFormController"                           
            implements="flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName,flexipage:availableForAllPageTypes">

<aura:attribute name="recordTypeId" type="String" />   
<aura:attribute name="fields" type="Object[]" access="private" />


<aura:handler name="init" value="{!this}" action="{!c.init}" />
<aura:handler event="force:refreshView" action="{!c.init}" /> 



<aura:attribute name="fieldSetName" type="String" description="The api name of the field set to use from the given object." />
<aura:attribute name="fieldSetName2" type="String" description="The api name of the field set to use from the given object." />
<aura:attribute name="WOLIDetailsList" type="List" default="[]"/>
<aura:attribute name="sObjectName2" type="string" />

<aura:attribute name="oBJECT1" type="SObject" default="{'sobjectType' : 'sObjectName'}"/>
<aura:attribute name="mrName" type="String" description="API name of MR field on the child. "/>


<!--the following attribute is for receive the ID of the parent
    I use it in the controller on de doSubmit-->
<aura:attribute name="ParentRecordId" type="String" />


    <!--FORM start here-->

<lightning:recordEditForm aura:id="test"
                          objectApiName="{! v.sObjectName }"
                          recordId="{! v.recordId }"
                          onsuccess="{!c.handleSubmit}"
                         class="slds-show">
    <lightning:messages />

 <!-- Header -->   

    <div class=" slds-size_6-of-8 slds-box slds-theme_default ">
        <div class="slds-page-header slds-grid slds-grid_pull-padded-medium">

            <div class="slds-order_1">
                <div class="slds-col slds-p-horizontal_medium"> 
                    <img src="{!$Resource.twiloLogo}"/> 
                </div>
            </div>

            <div class="slds-order_2">
                <div class="slds-col slds-p-horizontal_medium">
                    <div class="slds-text-heading_large"> 
                        <h1 > Complex Work Order.</h1>
                    </div>
                </div>
            </div>
        </div>
    </div>

<!-- /Header -->


<!-- Creating the Parent Form--> 
    <div class="slds-size_6-of-8 slds-box slds-theme_default">
        <aura:iteration items="{!v.fields }" var="field" indexVar="index">
            <lightning:inputField aura:id="fieldid" 
                                  fieldName="{! field.APIName }" 
                                  required="{! field.DBRequired}" 
                                  class=" slds-p-top_small slds-m-top_medium" />
        </aura:iteration>
    </div>
    <aura:iteration items="{!v.WOLIDetailsList}" var="item" indexVar="index">
        <div class="slds-size_6-of-8 slds-box slds-theme_default">
            <!--Dynamic Binding -->
            <c:Add_WOLI mrName="{!v.mrName}"
                        fieldSetName2="{!v.fieldSetName2}" 
                        sObjectName2="{!v.sObjectName2}" 
                        RecordIdChild="{!v.ParentRecordId}" 
                        WOLIDetailsInnerComponent="{!v.WOLIDetailsList}" 
                        indexNo="{!index}"/>
            <hr/>
        </div>
    </aura:iteration>


    <div>
        <lightning:button iconName="utility:add" variant="border-filled" label="WOLI" onclick="{!c.addDetails}"/>          
    </div> 

 <!-- /Adding WOLI List to Parent Form-->

 <lightning:layout horizontalAlign="center" class="slds-m-top_large">                    
        <lightning:button variant="brand" label="Submit" title="Submit" type="submit"/>
        <lightning:button variant="neutral" label="Cancel" title="Cancel" type="text" onclick="{!c.handleCancel}"/>
    </lightning:layout>        




</lightning:recordEditForm>

--- ComplexWOFormController.js ---

({
init: function(cmp, event, helper) {

    var fieldSetName = cmp.get('v.fieldSetName');
    var sobjectName = cmp.get('v.sObjectName');
    var recordId = cmp.get('v.recordId');

     console.log("IM @ init parent fieldsetname ",fieldSetName);
     console.log("IM @ init parent sobjectName",sobjectName);
     console.log("IM @ init parent  recordId",recordId);

    if (!fieldSetName) {
        console.log('The field set is required.');
        return;
    }

    var getFormAction = cmp.get('c.getForm');

    getFormAction.setParams({
        fieldSetName: fieldSetName,
        objectName: sobjectName,
        recordId: recordId
    });

    getFormAction.setCallback(this, 
        function(response) {
            var state = response.getState();
            console.log('FieldSetFormController getFormAction callback PARENT');
            console.log("callback state: " + state);

            if (cmp.isValid() && state === "SUCCESS") {
                var form = response.getReturnValue();
                cmp.set('v.fields', form.Fields);
                cmp.set('v.record', form.Record);
            }
        }
    );
    $A.enqueueAction(getFormAction);
},

 handleSubmit : function(component, event, helper) {

     console.log("I'm at Handle Submit " );

     var simpleWO =component.get("v.sObjectName");

     console.log("I'm at Handle Submit simpleWO: ", simpleWO); 

     var action= component.get("c.saveSimpleWO");

     action.setParams({simpleWO : simpleWO});

     action.setCallback(this, function(response) {
      var state = response.getState();

         console.log("State of SIMPLE WO",state);

        if (state === "SUCCESS") {


            // Alert the user with the value returned 
            // from the server
            alert("From server: " + response.getReturnValue());

            var parentId= response.getReturnValue();

            //assign the value of parent ID to our attribute
            component.set("v.ParentRecordId",parentId);


            // You would typically fire a event here to trigger 
            // client-side notification that the server-side 
            // action is complete
            // 
            // add more logic her
            // store/save education detail records as well

            event.preventDefault(); // Prevent default submit         
            var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams({"title": "Success!","message": "The COmplex WO has been saved.","type": "success"});
            toastEvent.fire();
            $A.get('e.force:refreshView').fire();

        }
        else if (state === "INCOMPLETE") {
            // do something
        }
        else if (state === "ERROR") {
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + 
                             errors[0].message);
                }
            } else {
                console.log("Unknown error");
            }
        }
    });//$A.enqueueAction adds the server-side action to the queue

$A.enqueueAction(action);






     /*event.preventDefault(); // Prevent default submit         
    var toastEvent = $A.get("e.force:showToast");
    toastEvent.setParams({"title": "Success!","message": "The Simple WO has been saved.","type": "success"});toastEvent.fire();
    $A.get('e.force:refreshView').fire();*/

},

handleCancel : function(component, event, helper) {
    //helper.showHide(component);
    event.preventDefault();
    $A.get('e.force:refreshView').fire();
},



addDetails: function(component,event,helper){

    console.log("Add WOLI Details");
    var CurrentWOLIdetailsList = component.get("v.WOLIDetailsList");


    var currentSize= parseInt(CurrentWOLIdetailsList.length);
    var NewSize= parseInt((currentSize)+1);


    CurrentWOLIdetailsList.push(NewSize);
    component.set("v.WOLIDetailsList", CurrentWOLIdetailsList);

},

})

--- ADD_WOLI.cmp ---

<aura:component controller="FSFormController"   
            implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >



<!--Defining the WOLI Object-->

<aura:attribute name="fieldSetName2" type="String" description="The api name of the field set to use from the given object." />
 <aura:attribute name="sObjectName2" type="string" />   
<aura:attribute name="mrName" type="String" description="API name of MR field on the child. "/>

<aura:attribute name="indexNo" type="Integer"/>
<aura:attribute name="sequenceNo" type="Integer"/>

 <!--in EDU details is name="RegistrationRecordIdChild"-->
<aura:attribute name="RecordIdChild" type="String"/>

<!--this is the conection between the parent (I declared use this attribute on the parent COMPLEXWOFORM)-->
<aura:attribute name="WOLIDetailsInnerComponent" type="List" />


<aura:attribute name="fields2" type="Object[]" access="private" />
<aura:handler event="force:refreshView" action="{!c.doinit}" /> 

 <!---whenever any change occurs in RecordIdChild aura handler calls to the controller-->

<aura:handler name="change" value="{!v.RecordIdChild}" action="{!c.saveWOLI}"/>

<aura:handler name="change" value="{!v.indexNo}" action="{!c.changeInIndexNo}" />

 <aura:attribute name="sNAME" type="SObject" default="{'sobjectType ':'sObjectName2'}" />



<div class="slds-size_6-of-8">
    <div class="slds-page-header">
        <div class="sslds-col slds-p-horizontal_medium">
            <div class="slds-text-heading--small">

            <h2> WOLI # {!v.sequenceNo} </h2> 
            </div> 
       </div>
    </div>
</div>


<lightning:recordEditForm aura:id="test"
                          objectApiName="{! v.sObjectName2 }"
                          recordId="{! v.RecordIdChild }"
                          recordTypeId="{! v.recordTypeId }" 
                          class="slds-show">
 <lightning:messages />


    <div class="slds-size_6-of-8 slds-box slds-theme_default">

            <aura:iteration items="{! v.fields2 }" var="field2">
                <span class="slds-col slds-p-horizontal_medium">
                <lightning:inputField aura:id="fieldid2" fieldName="{!field2.APIName }" class=" slds-p-top_small slds-m-top_medium" />
                </span>
                </aura:iteration>
        </div>




    <lightning:button iconName="utility:delete" variant="border-filled" label="Delete this wOLI" onclick="{!c.deleteDetails}"/>
</lightning:recordEditForm>

--- Add_WOLIController.js ---

({

doinit: function(cmp, event, helper){
    var fieldSetName = cmp.get('v.fieldSetName2');
    var sobjectName = cmp.get('v.sObjectName2');
    var recordId = cmp.find("test2").get("v.value");

    console.log("IM AT dointchild fieldsetname WOLI",fieldSetName);
     console.log("IM AT dointchild WOLI sobjectName",sobjectName);
     console.log("IM AT dointchild WOLI recordId",recordId);
    if (!fieldSetName) {
        console.log('The field set is required.');
        return;
    }
    console.log("DO INIT CHILD CMP")
    var getFormAction = cmp.get('c.getForm');

    getFormAction.setParams({
        fieldSetName: fieldSetName,
        objectName: sobjectName,
        recordId: recordId
    });

    getFormAction.setCallback(this, 
        function(response) {
            var state = response.getState();
            console.log('FSFormController getFormAction callback CHILD');
            console.log("callback state: " + state);

            if (cmp.isValid() && state === "SUCCESS") {


                var form = response.getReturnValue();
                cmp.set('v.fields2', form.Fields);
                cmp.set('v.record', form.Record);

            }
        }
    );
    $A.enqueueAction(getFormAction);
    helper.helperRectifySequence(cmp,event);
},



changeInIndexNo: function(component, event, helper){
    //console.log("Im at changeInIndexNo  value:",indexNo);
            helper.helperRectifySequence(component,event);

},


deleteDetails : function(component, event, helper) {

    var NewWOLIdetails= component.get("v.WOLIDetailsInnerComponent");
    var currentIndex= component.get("v.indexNo");
    if(currentIndex > -1)
          NewWOLIdetails.splice(currentIndex,1);
    component.set("v.WOLIDetailsInnerComponent", NewWOLIdetails);
},


saveWOLI: function(component,event, helper){
    // call apex class function w the name of the class
    //SaveEducationalDetails

        console.log("IM AT save WOLI");

   var RecordIdChild = component.get("v.RecordIdChild");

    var sOName2 = component.get("v.sObjectName2");
    var mrName=component.get("v.mrName");

    console.log("Lo que mando en SaveWOLI ",sOName2);
    console.log("Lo que mando en SaveWOLI ",mrName);

    component.set("v.sOName2.mrName",RecordIdChild);
    var action = component.get("c.saveComplexWO");

          action.setParams({complexWO : sOName2});

    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            console.log("Add WOLI saved");

        }
        else if (state === "INCOMPLETE") {
            // do something
        }
        else if (state === "ERROR") {
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + 
                             errors[0].message);
                }
            } else {
                console.log("Unknown error");
            }
        }
    });//$A.enqueueAction adds the server-side action to the queue

             $A.enqueueAction(action);


},

})

---- FSFormController.apxc -----

public with sharing class  FSFormController {


@AuraEnabled
public static FieldSetForm getForm(Id recordId, String objectName, String fieldSetName) {
    FieldSetForm form = new FieldSetForm();        
    form.Fields = getFields(recordId, objectName, fieldSetName);

    return form;
}

private static List<Field> getFields(Id recordId, String objectName, String fieldSetName) {
    Schema.SObjectType objectType = null;

    if (recordId != null) {
        objectType = recordId.getSobjectType();
    }
    else if (String.isNotBlank(objectName)) {
        objectType = Schema.getGlobalDescribe().get(objectName);
    }

    Schema.DescribeSObjectResult objectDescribe = objectType.getDescribe();
    Map<String, Schema.FieldSet> fieldSetMap = objectDescribe.fieldSets.getMap();
    Schema.FieldSet fieldSet = fieldSetMap.get(fieldSetName);
    List<Schema.FieldSetMember> fieldSetMembers = fieldSet.getFields();

    List<Field> fields = new List<Field>();
    for (Schema.FieldSetMember fsm : fieldSetMembers) {
        Field f = new Field(fsm);

        fields.add(f);
    }

    return fields;
}

public class FieldSetForm {
    @AuraEnabled
    public List<Field> Fields { get; set; }

    public FieldSetForm() {
        Fields = new List<Field>();
    }
}



@AuraEnabled
public static id saveSimpleWO(WorkOrder simpleWO){

    //DML op to save SWO
    insert simpleWO;
    return simpleWO.id;
}

@AuraEnabled
public static id saveComplexWO(SObject complexWO){

    //DML op to save SWO
    insert complexWO;
    return complexWO.id;
}   

}

---- Field.apxc-----

public class Field {

public Field(Schema.FieldSetMember f) {
    this.DBRequired = f.DBRequired;
    this.APIName = f.fieldPath;
    this.Label = f.label;
    this.Required = f.required;
    this.Type = String.valueOf(f.getType());
}

public Field(Boolean DBRequired) {
    this.DBRequired = DBRequired;
}

@AuraEnabled
public Boolean DBRequired { get;set; }

@AuraEnabled
public String APIName { get;set; }

@AuraEnabled
public String Label { get;set; }

@AuraEnabled
public Boolean Required { get;set; }

@AuraEnabled
public String Type { get; set; }

}

up vote 0 down vote accepted

There is an issue with this piece of code in handleSubmit method:

 var simpleWO =component.get("v.sObjectName");

 console.log("I'm at Handle Submit simpleWO: ", simpleWO); 

 var action= component.get("c.saveSimpleWO");

 action.setParams({simpleWO : simpleWO});

If you will check the console log, you will find that simpleWO hold a string value which is WorkOrder. You want to call server-side method saveSimpleWO which expects a parameter of type (WorkOder a sobject).

  • Thanks Pragati. But yes, why should be the correct way to retrieve a WorkOder a sobject? I'm stuck in the definition. --> <aura:attribute name="oBJECT1" type="SObject" default="{'sobjectType' : 'sObjectName'}"/> I think the problem is there in the 'type', but I've tried everything and I can't get with the solution – Karla Martinez Oct 22 at 16:29
  • Update solved. Thanks Pragati! – Karla Martinez Oct 23 at 3:53

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.