0

I am having trouble with LDS trying to load a page with fields tied to checkbox values. I am trying to display the boolean values as radio buttons and everything is loading correctly. However, the problem is whenever a radio button is changed I get the message 'Problem saving record, invalid data type' despite trying to return boolean values.
The basic functionality of LDS is working, however, since whenever I try to change the "Name" field on the record using just a "lightning:input" it works. Below is my code (I have cut out some code for brevity):

<aura:component>

<aura:attribute name="record" type="Object"/>
<aura:attribute name="caseRecord" type="Object"/>
<aura:attribute name="recordError" type="String"/>
<aura:attribute name="options" type="List" default="[{'label': 'Yes', 'value':true},
                                                     {'label': 'No', 'value': false}]"/>



<force:recordData aura:id="recordLoader"
  layoutType="FULL"
  recordId="{!v.recordId}"
  targetRecord="{!v.record}"
  targetFields="{!v.caseRecord}"
  targetError="{!v.recordError}"
  fields="Name, Id, fieldToChange__c"
  mode="EDIT"
  recordUpdated="{!c.handleRecordUpdated}"
  />    
<lightning:radioGroup 
                      name="q14"    
                      aura:id="question14"    
                      label="change value"
                      options="{!v.options}"
                      value="{!v.caseRecord.fieldToChange__c  }"
                      type="radio"
                      />
     <lightning:button label="Save Account" variant="brand" onclick="{!c.handleSaveRecord}" />
</aura:component>

Controller (which is copied direct from Documentation):

 ({
    handleSaveRecord: function(component, event, helper) {
        component.find("recordLoader").saveRecord($A.getCallback(function(saveResult) {

            // NOTE: If you want a specific behavior(an action or UI behavior) when this action is successful 
            // then handle that in a callback (generic logic when record is changed should be handled in recordUpdated event handler)
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // handle component related logic in event handler
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        }));
    },

    /**
     * Control the component behavior here when record is changed (via any component)
     */
    handleRecordUpdated: function(component, event, helper) {
        var eventParams = event.getParams();
        if(eventParams.changeType === "CHANGED") {
            // get the fields that changed for this record
            var changedFields = eventParams.changedFields;
            console.log('Fields that are changed: ' + JSON.stringify(changedFields));
            // record is changed, so refresh the component (or other component logic)
            var resultsToast = $A.get("e.force:showToast");
            resultsToast.setParams({
                "title": "Saved",
                "message": "The record was updated."
            });
            resultsToast.fire();

        } else if(eventParams.changeType === "LOADED") {
            // record is loaded in the cache
        } else if(eventParams.changeType === "REMOVED") {
            // record is deleted and removed from the cache
        } else if(eventParams.changeType === "ERROR") {
            // there’s an error while loading, saving or deleting the record
        }
    }
})

Any help to why the radio buttons will not work would be greatly appreciated. All of the other functionality is working as planned.

1

I have modified code of component and JS controller. This is working fine for me and saving value of radio button in field in database as well.

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction,force:hasSObjectName">

    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="caseRecord" type="Object"/>
    <aura:attribute name="recordError" type="String"/>
    <aura:attribute name="options" type="List" default="[{'label': 'Yes', 'value':true},
                                                        {'label': 'No', 'value': false}]"/>

    <aura:attribute name="value" type="String" default="option1"/>

    <force:recordData aura:id="recordLoader"
                      layoutType="FULL"
                      recordId="{!v.recordId}"
                      targetRecord="{!v.record}"
                      targetFields="{!v.caseRecord}"
                      targetError="{!v.recordError}"
                      fields="Name, Id, fieldToChange__c"
                      mode="EDIT"
                      recordUpdated="{!c.handleRecordUpdated}"
                      />    
    <lightning:radioGroup 
                          name="q14"    
                          aura:id="question14"    
                          label="change value"
                          options="{!v.options}"
                          value="{! v.value }"
                          type="radio"
                          />
    <lightning:button label="Save Account" variant="brand" onclick="{!c.handleSaveRecord}" />
</aura:component>





({
    handleSaveRecord: function(component, event, helper) {
        var fieldValue = component.get("v.value");
        var record = component.get("v.caseRecord");
        if(fieldValue === 'true'){
            record.fieldToChange__c = true;
        }
        else{
            record.fieldToChange__c = false;
        }

        component.find("recordLoader").saveRecord($A.getCallback(function(saveResult) {

            // NOTE: If you want a specific behavior(an action or UI behavior) when this action is successful 
            // then handle that in a callback (generic logic when record is changed should be handled in recordUpdated event handler)
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // handle component related logic in event handler
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        }));
    },

    /**
     * Control the component behavior here when record is changed (via any component)
     */
    handleRecordUpdated: function(component, event, helper) {
        var eventParams = event.getParams();
        if(eventParams.changeType === "CHANGED") {
            // get the fields that changed for this record
            var changedFields = eventParams.changedFields;
            console.log('Fields that are changed: ' + JSON.stringify(changedFields));
            // record is changed, so refresh the component (or other component logic)
            var resultsToast = $A.get("e.force:showToast");
            resultsToast.setParams({
                "title": "Saved",
                "message": "The record was updated."
            });
            resultsToast.fire();

        } else if(eventParams.changeType === "LOADED") {
            // record is loaded in the cache
        } else if(eventParams.changeType === "REMOVED") {
            // record is deleted and removed from the cache
        } else if(eventParams.changeType === "ERROR") {
            // there’s an error while loading, saving or deleting the record
        }
    }
})

ENJOY

  • Thank you, This worked for me, I didn't realize I would have to explicitly set the value to "true" or "false" in the controller. I though Lightning data service would recognize the boolean by default – Mike Meadows Nov 16 '18 at 19:15

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.