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
Ricardo Coutinho 7Ricardo Coutinho 7 

Attempted to upsert a null list

Hi,

I'm making a lightning component that list the contacts and we can search them or add new ones.
I'm following the Lightning Developer Guide, they have a lightning component for expenses and i'm just addapting to contacts.

Everything works fine, but when i try to add a new contact it gives me an error in the Developer Console logs:
"Attempted to upsert a null list"

I already tested the apex class and i see the debug, so i don't know exactly what's happening.

 Apex Class
@AuraEnabled
    public static Contact SaveContacts(Contact contact) {
    system.debug('Yes');
    upsert contact;
    return contact;
    }

My Input Form component
<aura:component implements="force:appHostable" controller="ContactsApexClass">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler event="c:SearchKey" action="{!c.SearchKey}"/>
  <ltng:require styles="{!$Resource.SLDS + '/assets/styles/salesforce-lightning-design-system.css'}"/>
  <aura:attribute name="contacts" type="Contact[]"/>
  <aura:attribute name="NewContact" type="Contact"
         default="{ 'sobjectType': 'Contact',
                         'Name': '',
                         'Birthdate': '',
                         'MailingStreet': '', 
                         'Email': ''
                       }"/>
 
  <!-- Attributos do Contador -->
  <aura:attribute name="exp" type="Double" default="0" />
    
   <ui:button label="JQuery Test"
                		class="slds-button slds-button--neutral"
                		labelClass="label"
                		press="{!c.teste}"/> 
    
    <!-- Contador -->
  <div class="container slds-p-top--medium">
        <div class="row">
            <div class="slds-tile ">
                <div class="slds-notify slds-notify--toast slds-theme--alert-texture">
                      <p class="slds-tile__title slds-truncate">Número de Contactos</p>
                      <ui:outputNumber class="slds-truncate" value="{!v.exp}"/>
                  </div>
            </div>
        </div>
    </div>
    
  <!-- Input Form -->
  <div class="container">
    <form class="slds-form--stacked">
      <div class="slds-form-element slds-is-required">
        <div class="slds-form-element__control">

          <ui:inputText aura:id="contactname" label="Nome"
                        class="slds-input"
                        labelClass="slds-form-element__label"
                        value="{!v.NewContact.Name}"
                        placeholder="Nome" required="true"/>
         </div>
       </div>
       <div class="slds-form-element slds-is-required">
         <div class="slds-form-element__control">
           <ui:inputDate aura:id="contactbirth" label="Data de Nascimento"
                           class="slds-input"
                           labelClass="slds-form-element__label"
                           value="{!v.NewContact.Birthdate}"
                           required="true"/>
          </div>
        </div>
        <div class="slds-form-element">
          <div class="slds-form-element__control">
            <ui:inputText aura:id="contactadress" label="Morada"
                          class="slds-input"
                          labelClass="slds-form-element__label"
                          value="{!v.NewContact.MailingStreet}"
                          placeholder="Avenida Cidade BL2, 1D" required="true"/>
           </div>
         </div>
          <div class="slds-form-element">
          <div class="slds-form-element__control">
            <ui:inputText aura:id="contactemail" label="Email"
                          class="slds-input"
                          labelClass="slds-form-element__label"
                          value="{!v.NewContact.Email}"
                          placeholder="nome@dominio.com"/>
           </div>
         </div>
        <br></br>
                        <ui:button label="Submit"
                		class="slds-button slds-button--neutral"
                		labelClass="label"
                		press="{!c.CreateContact}"/>
    </form>
  </div><!-- ./container-->
<br></br>
    <aura:iteration items="{!v.contacts}" var="contact">
        <c:ContactsList contact="{!contact}"/>
 </aura:iteration>
</aura:component>

My controller
({		      
    	doInit : function(component, event, helper) {
       helper.getContacts(component);
    	},
    
    	CreateContact : function(component, event, helper) {
            var fields = ["contactname", "contactbirth", "contactadress"];
            var errors = ["Introduza um nome válido.","Introduza uma data de nascimento válida.","Introduza uma morada válida."  ];
            var erro = 0;
    			for (var i = 0; i < fields.length; i++){
                var field = component.find(fields[i]);
                var fieldValue = field.get("v.value");
                	if (fieldValue == ''){
                    field.set("v.errors", [{message:errors[i]}]);
                    erro = 1;
               		} else {
                    field.set("v.errors", null); 
                	}
				}
            		if (erro == 0){
                	var NewContact = component.get("v.NewContact");
                	helper.CreateContact(component, NewContact);
            		}
		},
    
    	updateEvent : function(component, event, helper) {
    	helper.upsertContact(component, event.getParam("contact"));
		},
    	
    	SearchKey: function(component, event) {
    	var searchKey = event.getParam("searchk");
    	var action = component.get("c.findByName");
    	action.setParams({
      	"searchKey": searchKey
    	});
    	action.setCallback(this, function(a) {
        component.set("v.contacts", a.getReturnValue());
    	});
    	$A.enqueueAction(action);
		}

})

My Helper:
({
    getContacts: function(component) {
		var action = component.get("c.getContacts");
		action.setCallback(this, function(response) {
		var state = response.getState();
			if (component.isValid() && state === "SUCCESS") {
			component.set("v.contacts", response.getReturnValue());
			this.updateTotal(component);
			}
		});	
		$A.enqueueAction(action);
	},		
  
    updateTotal : function(component) {
		var contacts = component.get("v.contacts");
			for(var i=0; i<contacts.length; i++){
			var e = contacts[i];
			}
		//Update contador
		component.set("v.exp", contacts.length);
	},	

	CreateContact: function(component, contact) {
    	this.upsertContact(component, contact, function(a) {
        var contacts = component.get("v.contacts");
        contacts.push(a.getReturnValue());
        component.set("v.contacts", contacts);
        this.updateTotal(component);
      	});
		},

    upsertContact : function(component, contact, callback) {
    	var action = component.get("c.SaveContacts");
   		action.setParams({ 
        "Contact": contact
    	});
   	 		if (callback) {
      		action.setCallback(this, callback);
    		}
    			$A.enqueueAction(action);
	}  
})




 
EldonEldon
Try to debug the contacts in apex if contacts are there then try to insert and update separately. It is a temporary solution though.