You need to sign in to do that
Don't have an account?
shalini sharma 24
null value getting passed in js controller of lightning component
Hi,
I am getting null value in the parameter "contactField" which is being passed to the apex class. i am expecting the value in the lightning :input to be passed to the apex controller.
public class wrapperDisplayController {
@AuraEnabled
public static void saveRecords(String contactField){
system.debug('contactField >> ' +contactField);
if(!string.isBlank(contactField)){
Contact c = new Contact();
MainClass.wrapperClass oWrapField = (MainClass.wrapperClass)System.JSON.deserialize(contactField,MainClass.wrapperClass.class);
c.Wrapper_Field__c = oWrapField.headerMsg;
c.LastName = 'Test wrap';
}
}
Wrapper Class
public class MainClass {
public class wrapperClass{
@AuraEnabled public String headerMsg {get;set;}
}
}
Lightning Component
<aura:component controller="wrapperDisplayController" implements="lightning:actionOverride,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
<!--aura init handler , call js "loadData" function on component load, and display contact data on table-->
<aura:attribute name="wrapperList" type="MainClass.wrapperClass"/>
<lightning:input name="contactedPerson" label="Person being Contacted" placeholder="type here..." value="{!v.wrapperList.headerMsg}"/>
<p>
Contacted Person : {!v.wrapperList.headerMsg}
</p>
<div class="slds-size--1-of-3 slds-large-size--1-of-3 slds-align--absolute-center">
<lightning:button class="slds-button slds-button--brand" onclick="{!c.saveContactReport}">Save Contact Report</lightning:button>
</div>
</aura:component>
Js Controller:
({
saveContactReport : function(component,event,helper) {
alert('save method called');
var field = component.get("v.wrapperList");
var action = component.get('c.saveRecords');
alert('field is >> '+field);
var fieldVal = JSON.stringify(field);
alert('fieldVal is >> '+fieldVal);
action.setParams({
"contactField" : fieldVal
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
alert("SUCCESS");
}
});
$A.enqueueAction(action);
},
})
}
I am getting null value in the parameter "contactField" which is being passed to the apex class. i am expecting the value in the lightning :input to be passed to the apex controller.
public class wrapperDisplayController {
@AuraEnabled
public static void saveRecords(String contactField){
system.debug('contactField >> ' +contactField);
if(!string.isBlank(contactField)){
Contact c = new Contact();
MainClass.wrapperClass oWrapField = (MainClass.wrapperClass)System.JSON.deserialize(contactField,MainClass.wrapperClass.class);
c.Wrapper_Field__c = oWrapField.headerMsg;
c.LastName = 'Test wrap';
}
}
Wrapper Class
public class MainClass {
public class wrapperClass{
@AuraEnabled public String headerMsg {get;set;}
}
}
Lightning Component
<aura:component controller="wrapperDisplayController" implements="lightning:actionOverride,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
<!--aura init handler , call js "loadData" function on component load, and display contact data on table-->
<aura:attribute name="wrapperList" type="MainClass.wrapperClass"/>
<lightning:input name="contactedPerson" label="Person being Contacted" placeholder="type here..." value="{!v.wrapperList.headerMsg}"/>
<p>
Contacted Person : {!v.wrapperList.headerMsg}
</p>
<div class="slds-size--1-of-3 slds-large-size--1-of-3 slds-align--absolute-center">
<lightning:button class="slds-button slds-button--brand" onclick="{!c.saveContactReport}">Save Contact Report</lightning:button>
</div>
</aura:component>
Js Controller:
({
saveContactReport : function(component,event,helper) {
alert('save method called');
var field = component.get("v.wrapperList");
var action = component.get('c.saveRecords');
alert('field is >> '+field);
var fieldVal = JSON.stringify(field);
alert('fieldVal is >> '+fieldVal);
action.setParams({
"contactField" : fieldVal
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
alert("SUCCESS");
}
});
$A.enqueueAction(action);
},
})
}
<aura:attribute name="wrapperList" type="MainClass.wrapperClass"/>
<lightning:input name="contactedPerson" label="Person being Contacted" placeholder="type here..." value="{!v.wrapperList.headerMsg}"/>
replace above lines by
<aura:attribute name="contactedPerson" type="String"/>
<lightning:input name="contactedPerson" label="Person being Contacted" placeholder="type here..." />
and in the JS file replace
var field = component.get("v.wrapperList");
by
var field = component.get("v.contactedPerson");
Donot stringify anything as we are storing the input value in the String format. so you can remove that lines. Let me know if this works.
Thanks for your reply. My requirement is such that i have to use attribute of wrapper (inner) class type only. And with this i am facing issue.