I havent been able to find any sample js that clearly describes how to instantiate a new sobject from within a lightning component. Lets say I want to create a list of new custom sobjects named foo (that only have a name field) to pass into my server side controller for final DML - any ideas ?

I assume I would have an aura:attribute on my component of name = fooList and type ='foo[]'. Then from within my controller.js I would do a fooList = component.get("v.fooList") but how do I then define a new sobject of type foo with name set, and push in instances of the sobject foo into fooList?

Any insights would be most appreciated.

2 Answers 2


You can certainly create a sobject instance in client and pass as it as array from the component and retrieve it as List<sObject> in the controller.

If you want to pass a specific sObject type you, have to set the sobjectType.

For eg: sobjectType to Account

Here an simple example to do it.


<aura:application controller="AccountController" access="public">
    <aura:attribute name="accounts" type="List" access="private"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <aura:iteration items="{!v.accounts}" var="acc">
        <ui:inputText value="{!acc.Name}"/><br/>

    <ui:button press="{!c.saveAcc}" label="Save"/>


    doInit : function(cmp, event, helper) {
        var acc = [];

        for(var i = 0;i < 5;i++){
            //set sobjectType to Account, if account is passed


    saveAcc : function(cmp, event, helper) {

        var action = cmp.get("c.insertAccounts");


            var state = resp.getState();

            if(state === 'SUCCESS'){
            else if(state === 'ERROR'){
                var errors = resp.getError();
                for(var i = 0 ;i < errors.length;i++){




Apex Controller:

public with sharing class AccountController {

    //if sobject is specific for eg:Account,then you use List<Account> acc
    public static String insertAccounts(List<sObject> acc){
            insert acc;
            return 'success';
        catch(DMLException ex){
           AuraHandledException e = new AuraHandledException(ex.getMessage());
           throw e;
        return '';        
  • the line {'sobjectType':'Account','Name':'Test'+i} is clear, but how would you also enter a date field? I guess the date should be a string. Oct 21, 2020 at 13:57

If what do you want to is to create new custom object records in SF, you should send the information needed to your server side controller, then perform the corresponding DML there, if you need to identify what sObject you want to create I would add an extra param to identify them.

For more detailed info, check the online help: Calling a Server-Side Action

  • Ok thanks - so you're saying that we cant do a javascript object to then be deserialised server side ? I was hoping for something like that rather than transforming json string from within apex. Have you tried that approach ? Jul 22, 2016 at 21:34
  • I was not saying that exactly, but what you are saying make sense too, you could create the structure in javascript and send the string to the server, and then do the parsing in apex.
    – pfernandez
    Jul 23, 2016 at 1:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .