function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Margaret FleischakerMargaret 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.
<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"));
            })
        );
    },


 
Best Answer chosen by Margaret Fleischaker
Margaret FleischakerMargaret Fleischaker
Thank you for the suggestion! That didn't end up working for me, but the following did work:
 
var a = component.get("v.simpleNewCase",true);
a.Origin = "Email - Attendee TF Music";
component.set("v.simpleNewCase",a);

 

All Answers

Margaret FleischakerMargaret Fleischaker
My formatting didn't stick because I entered it as code. For reference, the relevant lines are 04 and 21 in the component, and 17-23 in the controller.
Raj VakatiRaj Vakati
Try to set the value as shown below
 
component.set("v.caseRecordCreator.fields.caseorigion.value", "Email - Attendee TF Music");
Sample cod e
 
aveRecord : function(component,event,helper) {
var propBeds = parseInt(component.find('propBeds').get("v.value"), 10);
var propBaths = parseInt(component.find('propBaths').get("v.value"), 10);
var propPrice = parseInt(component.find('propPrice').get("v.value"), 10);

component.set("v.selectedProperty.fields.Beds__c.value", propBeds);
component.set("v.selectedProperty.fields.Baths__c.value", propBaths);
component.set("v.selectedProperty.fields.Price__c.value", propPrice);

var tempRec = component.find("editRecord");
tempRec.saveRecord($A.getCallback(function(result){
if (result.state === "SUCCESS" || result.state === "DRAFT") {
var event = $A.get("e.c:recordUpdated");
event.fire();
} else if (result.state === "ERROR") {
console.log('Error: ' + JSON.stringify(result.error));
} else {
console.log('Unknown problem, state: ' + result.state + ', error: ' + JSON.stringify(result.error));
}
}));
}


 
Margaret FleischakerMargaret Fleischaker
Thank you for the suggestion! That didn't end up working for me, but the following did work:
 
var a = component.get("v.simpleNewCase",true);
a.Origin = "Email - Attendee TF Music";
component.set("v.simpleNewCase",a);

 
This was selected as the best answer