You need to sign in to do that
Don't have an account?
Margaret Fleischaker
Default lightning:input based on attribute value
I have a lightning component that is used in a community page to allow guests to create cases. I want to set the case origin by default without them seeing the value, so I've created a lightning:input field that I will eventually hide using CSS. I'm trying to set the value of that input field based on an attribute, but it's not working correctly. No values are getting set, and the console log I put after trying to set the value isn't getting logged, so something is clearly going wrong.
Any ideas of the correct way to do this? I feel like I must be missing something simple (hopefully). Below is the component and controller; I've tried to strip out unrelated code so it's easier to read and made the super relevant lines bold.
Any ideas of the correct way to do this? I feel like I must be missing something simple (hopefully). Below is the component and controller; I've tried to strip out unrelated code so it's easier to read and made the super relevant lines bold.
<aura:component implements="forceCommunity:availableForAllPageTypes,force:hasRecordId" access="global" > <aura:attribute name="newCase" type="Object"/> <aura:attribute name="simpleNewCase" type="Object"/> <aura:attribute name="caseOrigin" type="String" default="Email - Attendee TF Music"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <force:recordData aura:id="caseRecordCreator" layoutType="FULL" targetRecord="{!v.newCase}" targetFields="{!v.simpleNewCase}" targetError="{!v.recordSaveError}"/> <!-- Display the new case form --> <aura:if isTrue="{!v.showForm}"> <div class="Create Case"> <lightning:input aura:id="caseField" label="Full Name" value="{!v.simpleNewCase.SuppliedName}" required="true" maxlength="80"/><br/> <lightning:input aura:id="caseField" label="Email" value="{!v.simpleNewCase.SuppliedEmail}" type="Email" required="true" maxlength="80"/><br/> <!-- hidden input to set the case origin by default--> <lightning:input aura:id="caseField" name="originField" label="Case Origin" value="{!v.simpleNewCase.Origin}"/><br/> <aura:set attribute="else"> <p>Thank you for reaching out.</p> <p>The support team has received your email and will get back to you shortly.</p> </aura:set> </aura:if> </aura:component>
({ doInit : function(component, event, helper) { // Prepare a new record from template component.find("caseRecordCreator").getNewRecord( "Case", // sObjecttype "01239000000EGzv", // recordTypeId false, // skip cache $A.getCallback(function() { var rec = component.get("v.newCase"); var error = component.get("v.recordSaveError"); if(error || (rec === null)) { alert("Error initializing record template" + error); return; } var caseOrigin = component.get("v.caseOrigin"); console.log("[MF] caseOrigin:" + caseOrigin); console.log("[MF] v.originField before: " + component.get("v.originField")); component.set(component.get("v.originField"), caseOrigin); // I've also tried the following, with no success: // component.set("v.originField", caseOrigin); console.log("[MF] v.originField after: " + component.get("v.originField")); }) ); },
All Answers
Sample cod e