You need to sign in to do that
Don't have an account?
Sabrina Oliveira 3
Lightning component working alone but not working inside another component
I'm using a Lightning Component to update a field called "Observations__c" in Account Object.
It's working fine, but when I try to use it inside another component, I receive the following error:
EXCEPTION_THROWN [9]|System.NullPointerException: Attempted to upsert a null list
Component:
<aura:component implements="force:lightningQuickActionWithoutHeader,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="getAccount">
<aura:handler name="init" action="{!c.init}" value="{!this}" />
<aura:attribute name="accounts" type="Account" />
<aura:attribute name="recordId" type="Id" />
<ltng:require styles="/resource/slds103/assets/styles/salesforce-lightning-design-system.min.css"/>
<form>
<ui:inputText aura:id="client"
label="{!$ObjectType.Account.fields.Observations__c.Label}"
class="form-control"
value="{!v.accounts.Observations__c}"
placeholder="Insira, caso necessário, comentários adicionais"
/>
<br />
<ui:button class="form-control" aura:id="button" label="Anexar observação" press="{!c.save}"/>
</form>
</aura:component>
Controller js:
({
init : function(component, event, helper) {
var accId = component.get("v.recordId");
var action = component.get("c.getAcct");
action.setParams({
"accountId": component.get("v.recordId")
});
action.setCallback(this, function(data) {
component.set("v.accounts", data.getReturnValue());
});
$A.enqueueAction(action);
},
save : function(component, event, helper) {
var action = component.get("c.saveAcct");
var accounts = component.get("v.accounts");
action.setParams({"act": accounts});
$A.enqueueAction(action);
console.log('save ran');
}
})
Apex Controller:
public class getAccount {
@AuraEnabled
public static Account getAcct(Id accountId){
return (Account) Database.query( ' SELECT Name, Observations__c FROM Account WHERE Id =: accountId LIMIT 1 ' )[0];
}
@AuraEnabled
public static Account saveAcct(Account act){
upsert act;
return act;
}
}
I'm calling <c:sparkAccount/> in my second component, but it is not working.
What am I doing wrong?
It's working fine, but when I try to use it inside another component, I receive the following error:
EXCEPTION_THROWN [9]|System.NullPointerException: Attempted to upsert a null list
Component:
<aura:component implements="force:lightningQuickActionWithoutHeader,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="getAccount">
<aura:handler name="init" action="{!c.init}" value="{!this}" />
<aura:attribute name="accounts" type="Account" />
<aura:attribute name="recordId" type="Id" />
<ltng:require styles="/resource/slds103/assets/styles/salesforce-lightning-design-system.min.css"/>
<form>
<ui:inputText aura:id="client"
label="{!$ObjectType.Account.fields.Observations__c.Label}"
class="form-control"
value="{!v.accounts.Observations__c}"
placeholder="Insira, caso necessário, comentários adicionais"
/>
<br />
<ui:button class="form-control" aura:id="button" label="Anexar observação" press="{!c.save}"/>
</form>
</aura:component>
Controller js:
({
init : function(component, event, helper) {
var accId = component.get("v.recordId");
var action = component.get("c.getAcct");
action.setParams({
"accountId": component.get("v.recordId")
});
action.setCallback(this, function(data) {
component.set("v.accounts", data.getReturnValue());
});
$A.enqueueAction(action);
},
save : function(component, event, helper) {
var action = component.get("c.saveAcct");
var accounts = component.get("v.accounts");
action.setParams({"act": accounts});
$A.enqueueAction(action);
console.log('save ran');
}
})
Apex Controller:
public class getAccount {
@AuraEnabled
public static Account getAcct(Id accountId){
return (Account) Database.query( ' SELECT Name, Observations__c FROM Account WHERE Id =: accountId LIMIT 1 ' )[0];
}
@AuraEnabled
public static Account saveAcct(Account act){
upsert act;
return act;
}
}
I'm calling <c:sparkAccount/> in my second component, but it is not working.
What am I doing wrong?
This is because of the force:hasRecordId will loose the current page record Id context inside the child component. To overcome this you have to pass recordId from parent to child component.
Important The recordId attribute is set only when you place or invoke the component in an explicit record context. For example, when you place the component directly on a record page layout, or invoke it as an object-specific action from a record page or object home.
https://salesforce.stackexchange.com/questions/241412/forcehasrecordid-cascade
Thanks,
Maharajan.C
All Answers
Came across this similar post that might help you
https://developer.salesforce.com/forums/?id=9060G0000005P9YQAU
https://salesforce.stackexchange.com/questions/66954/how-to-navigate-from-one-lightning-component-to-another-lightning-component
https://salesforce.stackexchange.com/questions/97290/how-to-get-parent-lightning-components-attribute-in-child-component
https://salesforce.stackexchange.com/questions/215789/attempt-to-upsert-a-null-list-in-lightning-helper-component/215856
If this information helps, please mark the answer as best. Thank you
This is because of the force:hasRecordId will loose the current page record Id context inside the child component. To overcome this you have to pass recordId from parent to child component.
Important The recordId attribute is set only when you place or invoke the component in an explicit record context. For example, when you place the component directly on a record page layout, or invoke it as an object-specific action from a record page or object home.
https://salesforce.stackexchange.com/questions/241412/forcehasrecordid-cascade
Thanks,
Maharajan.C
Thanks for your reply but I keep getting the same error :(
I hope you have the force:hasRecordId in parent component.
Also refresh the lightning page one or two times to see the changes.
Thanks,
Maharajan.C
I've cleaned my browser cache and it worked.
Thank you, it helped me a lot!