You need to sign in to do that
Don't have an account?
deepak kumar 13
Passing value from lightning controller to apex controller
Hi,
i am getting attempt de-reference null object error when i click save button in lightning component.
Here is my Lightning component code:
<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader,force:hasRecordId"
controller="PAY_SiteBudgetController">
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="sites" type="Clinical_Site__c" />
<aura:attribute name="sitebudget" type="PAY_Site_Budget__c"
default="{'sobjectType':'PAY_Site_Budget__c',
'Status__c': 'Planned',
'Name':'',
'Description__c':''}"/> <!--default to empty record-->
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<div class="slds-page-header" role="banner">
<p class="slds-text-heading--label">Site Budget</p>
<h1 class="slds-page-header__title slds-m-right--small
slds-truncate slds-align-left">Create Site Budget</h1>
</div>
<ui:inputText aura:id="requestField" value="{!v.sitebudget.Name}" label="Site Budget ID" disabled="true"/>
<ui:inputSelect label="Status" aura:id="requestField" value="{!v.sitebudget.Status__c}">
<aura:iteration items="{!v.sitebudget}" var="sts">
<option value="{!sts}" text="{!sts}"/>
</aura:iteration>
</ui:inputSelect>
<lightning:textarea aura:id="requestField" name="desc" label="Description" maxlength="255"
value="{!v.sitebudget.Description__c}" />
<lightning:button label="Save" onclick="{!c.handleSave}" variant="brand" class="slds-m-top--medium"/>
</aura:component>
And Lightning controller :
({
doInit : function(component, event, helper) {
var action =component.get("c.getSite");
action.setParams({"SiteId":component.get("v.recordId")});
action.setCallback(this, function(response) {
var state = response.getState();
console.log('--rturn payee --' + JSON.stringify(response.getReturnValue()));
if(component.isValid() && state === "SUCCESS") {
component.set("v.sites", response.getReturnValue());
} else {
console.log('Problem getting account, response state: ' + state);
}
});
$A.enqueueAction(action);
var action_reqType = component.get("c.getStatusValues")
action_reqType.setCallback(this, function(response) {
var state = response.getState();
console.log('--rturn type --' + JSON.stringify(response.getReturnValue()));
if(state === "SUCCESS") {
component.set("v.sitebudget", response.getReturnValue());
} else {
console.log('Problem getting request Type, response state: ' + state);
}
});
$A.enqueueAction(action_reqType);
},
handleSave:function(component, event, helper){
var SaveSiteBudg = component.get("c.SaveSiteBudget");
SaveSiteBudg.setParams({
"budget":component.get("v.sitebudget"),
"siteId":component.get("v.recordId")
});
SaveSiteBudg.setCallback(this, function(response){
var state=response.getState();
var resultsToast= $A.get("e.force:showToast");
if(state=="SUCCESS"){
resultsToast.setParams({
"title":"Success!",
"message":"New Site Budget is Created",
"type": "success,"
});
$A.get("e.force:closeQuickAction").fire();
resultsToast.fire();
$A.get("e.force:refreshView").fire();
}
});
$A.enqueueAction(SaveSiteBudg);
}
})
And apex controller :
public with sharing class PAY_SiteBudgetController {
@AuraEnabled
public static Clinical_Site__c getSite (Id SiteId){
system.debug('--SiteId--'+SiteId);
return [Select id,Name from Clinical_Site__c where id=:SiteId];
}
@AuraEnabled
public static PAY_Site_Budget__c SaveSiteBudget (PAY_Site_Budget__c budget, Id siteId){
system.debug('--sitebudget--'+budget.Status__c);
system.debug('--siteId--'+siteId);
system.debug('--budget--'+budget);
if (siteId!=null)
budget.Site__c=siteId;
try{
upsert budget;
}
catch(exception ex){
}
return budget;
}
/**/
@AuraEnabled
public static list<string> getStatusValues(){
List<string>options=new list<string>();
Schema.DescribeFieldResult fieldResult = PAY_Site_Budget__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for (Schema.PicklistEntry f: ple) {
options.add(f.getLabel());
}
return options;
}
}
Getting error : apex controller :SaveSiteBudget in this method as Attempt dereference null object and also 'budget' value i am getting null but in 'siteId' getting value in apex controller.
Please Help me to fix it.
Thanks
i am getting attempt de-reference null object error when i click save button in lightning component.
Here is my Lightning component code:
<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader,force:hasRecordId"
controller="PAY_SiteBudgetController">
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="sites" type="Clinical_Site__c" />
<aura:attribute name="sitebudget" type="PAY_Site_Budget__c"
default="{'sobjectType':'PAY_Site_Budget__c',
'Status__c': 'Planned',
'Name':'',
'Description__c':''}"/> <!--default to empty record-->
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<div class="slds-page-header" role="banner">
<p class="slds-text-heading--label">Site Budget</p>
<h1 class="slds-page-header__title slds-m-right--small
slds-truncate slds-align-left">Create Site Budget</h1>
</div>
<ui:inputText aura:id="requestField" value="{!v.sitebudget.Name}" label="Site Budget ID" disabled="true"/>
<ui:inputSelect label="Status" aura:id="requestField" value="{!v.sitebudget.Status__c}">
<aura:iteration items="{!v.sitebudget}" var="sts">
<option value="{!sts}" text="{!sts}"/>
</aura:iteration>
</ui:inputSelect>
<lightning:textarea aura:id="requestField" name="desc" label="Description" maxlength="255"
value="{!v.sitebudget.Description__c}" />
<lightning:button label="Save" onclick="{!c.handleSave}" variant="brand" class="slds-m-top--medium"/>
</aura:component>
And Lightning controller :
({
doInit : function(component, event, helper) {
var action =component.get("c.getSite");
action.setParams({"SiteId":component.get("v.recordId")});
action.setCallback(this, function(response) {
var state = response.getState();
console.log('--rturn payee --' + JSON.stringify(response.getReturnValue()));
if(component.isValid() && state === "SUCCESS") {
component.set("v.sites", response.getReturnValue());
} else {
console.log('Problem getting account, response state: ' + state);
}
});
$A.enqueueAction(action);
var action_reqType = component.get("c.getStatusValues")
action_reqType.setCallback(this, function(response) {
var state = response.getState();
console.log('--rturn type --' + JSON.stringify(response.getReturnValue()));
if(state === "SUCCESS") {
component.set("v.sitebudget", response.getReturnValue());
} else {
console.log('Problem getting request Type, response state: ' + state);
}
});
$A.enqueueAction(action_reqType);
},
handleSave:function(component, event, helper){
var SaveSiteBudg = component.get("c.SaveSiteBudget");
SaveSiteBudg.setParams({
"budget":component.get("v.sitebudget"),
"siteId":component.get("v.recordId")
});
SaveSiteBudg.setCallback(this, function(response){
var state=response.getState();
var resultsToast= $A.get("e.force:showToast");
if(state=="SUCCESS"){
resultsToast.setParams({
"title":"Success!",
"message":"New Site Budget is Created",
"type": "success,"
});
$A.get("e.force:closeQuickAction").fire();
resultsToast.fire();
$A.get("e.force:refreshView").fire();
}
});
$A.enqueueAction(SaveSiteBudg);
}
})
And apex controller :
public with sharing class PAY_SiteBudgetController {
@AuraEnabled
public static Clinical_Site__c getSite (Id SiteId){
system.debug('--SiteId--'+SiteId);
return [Select id,Name from Clinical_Site__c where id=:SiteId];
}
@AuraEnabled
public static PAY_Site_Budget__c SaveSiteBudget (PAY_Site_Budget__c budget, Id siteId){
system.debug('--sitebudget--'+budget.Status__c);
system.debug('--siteId--'+siteId);
system.debug('--budget--'+budget);
if (siteId!=null)
budget.Site__c=siteId;
try{
upsert budget;
}
catch(exception ex){
}
return budget;
}
/**/
@AuraEnabled
public static list<string> getStatusValues(){
List<string>options=new list<string>();
Schema.DescribeFieldResult fieldResult = PAY_Site_Budget__c.Status__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for (Schema.PicklistEntry f: ple) {
options.add(f.getLabel());
}
return options;
}
}
Getting error : apex controller :SaveSiteBudget in this method as Attempt dereference null object and also 'budget' value i am getting null but in 'siteId' getting value in apex controller.
Please Help me to fix it.
Thanks