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
Sabrina Oliveira 3Sabrina 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.
User-added image

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.
User-added image

What am I doing wrong? 
Best Answer chosen by Sabrina Oliveira 3
Maharajan CMaharajan C
Hi Sabrina,

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.
 
<c:sparkAccount  recordId="{! v.recordId }"/>

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

SwethaSwetha (Salesforce Developers) 
HI Sabrina,
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
Maharajan CMaharajan C
Hi Sabrina,

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.
 
<c:sparkAccount  recordId="{! v.recordId }"/>

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
This was selected as the best answer
Sabrina Oliveira 3Sabrina Oliveira 3
Hi Maharajan,

Thanks for your reply but I keep getting the same error :(
Maharajan CMaharajan C
Hi Sabrina.

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
Sabrina Oliveira 3Sabrina Oliveira 3
Hi Maharajan,

I've cleaned my browser cache and it worked. 
Thank you, it helped me a lot!
John BalorJohn Balor
I have got the same type of problem. (https://www.theblacksheep.community/how-to-handle-fame/)
nhgj jhnythynhgj jhnythy
I am using some features that are helping me to manage it in an easy way as you can see here (https://proremovalsbarnsley.co.uk/barnsley-man-and-van-services/) this project I am doingon it is on a nice track with no issue.