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
AnirudhRdyAnirudhRdy 

When trying to get the value for lightning:inputField into controller.js value is coming as undefined and this is only happening with a particular field in my value attribute.Can anyone help on this please

Hi All,

I have been trying to get the value of a lightning:inputfield into controller.js but when I try to get the value it is coming as undefined.

Code:

Component:
<aura:component implements="force:hasRecordId,lightning:actionOverride,force:appHostable,force:hasRecordId,force:hasSObjectName,flexipage:availableForRecordHome,flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes," access="global"> 

 <aura:attribute name="ServiceRequestRecordFields" type="WorkOrder"/>
  <force:recordData aura:id="recordEditor"
                      layoutType="FULL"
                      targetError="{!v.recordError}"
                      targetRecord="{!v.ServiceRequestRecord}"
                      targetFields ="{!v.ServiceRequestRecordFields}"
                      mode="EDIT"/>

<lightning:input aura:id="shopWrkId" label="Shop Work Order" value="{!v.ServiceRequestRecordFields.Shop_Work_Order__c}" onchange="{!c.CheckLength}"/>

</aura:component>

Controller.js:
 CheckLength : function(component, event, helper) {
        var val = component.find("shopWrkId").get('v.value');
         console.log('shopWrkId'+val);
        if(val.length > 20){
            var comp = component.find("number");
            comp.set('v.value',val.substring(0,20));
        }
    },

Now in the console value is coming as undefined. Please note that this is happening only with this particular field and when I change the field to a different one it is working as expected. Can anyone please help with this.

Two Fields:
Shop_Work_Order__c : Datatype: Text ; Length: 20 => not working
EVL_Customer__c: Datatype: Text ; Length: 250 => Working

Thanks,
Anirudh
 
AnudeepAnudeep (Salesforce Developers) 
Hi Anirudh, 

Are both Shop_Work_Order__c and EVL_Customer__c fields part of ServiceRequestRecordFields object?

Can you try declaring your attributes as objects in your code?
 
<aura:attribute name="ServiceRequestRecord" type="Object"
<aura:attribute name="ServiceRequestRecordFields" type="Object"/>

Also, see this post

If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep