You need to sign in to do that
Don't have an account?
Eric Smith 9
Lightning Data Service set value of field when field name is specified in a parameter
I have a simple component using Lightning Data Service to set the value of a field. I would like to pass the name of the field to update in as a parameter instead of hardcoding it. What changes should I make to my component to do this?
<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global" > <aura:attribute name="record" type="Object" /> <aura:attribute name="simpleRecord" type="Object" /> <aura:attribute name="recordError" type="String" /> <force:recordData aura:id="recordEditor" layoutType="FULL" recordId="{!v.recordId}" targetError="{!v.recordError}" targetRecord="{!v.record}" targetFields="{!v.simpleRecord}" mode="EDIT" /> <!-- Display LDS errors if any --> <aura:if isTrue="{!not(empty(v.recordError))}"> <div class="recordError"> <ui:message title="Error" severity="error" closable="true"> {!v.recordError} </ui:message> </div> </aura:if> <!-- Custom Interface Goes Here --> <lightning:button class="slds-button_neutral" label="New OSP Project" onclick="{!c.handleSaveRecord}" /> </aura:component>
({ handleSaveRecord : function(component, event, helper) { // Field Updates go Here component.set("v.simpleRecord.Trigger_OSP__c", true); // THIS IS WHERE I WANT TO UPDATE A FIELD BASED ON A FIELD NAME PASSED IN AS A PARAMATER TO THE COMPONENT // //Standard way to save a record template using Lightning Data Service component.find("recordEditor").saveRecord($A.getCallback(function(saveResult){ if(saveResult.state === "SUCCESS" || saveResult.state === "DRAFT"){ console.log("Save completed successfully."); $A.get("e.force:closeQuickAction").fire(); }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)); } })); }, cancel : function(component, event, helper){ $A.get("e.force:closeQuickAction").fire(); } })
<aura:attribute name="fieldName" type="String" />
var fieldName = component.get("v.fieldName");
var simpleRecord = component.get("v.simpleRecord");
simpleRecord [ fieldName ] = true; // givent that fieldName is a boolean
component.set("v.simpleRecord", simpleRecord);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors