9

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.

12

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.

TestApp:

<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/>
    </aura:iteration>

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

TestAppController.js

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

        for(var i = 0;i < 5;i++){
            //set sobjectType to Account, if account is passed
            acc.push({'sobjectType':'Account','Name':'Test'+i});
        }

        cmp.set("v.accounts",acc);

    },
    saveAcc : function(cmp, event, helper) {

        var action = cmp.get("c.insertAccounts");
        action.setParams({
            "acc":cmp.get("v.accounts")
        });

        actions.setCallback(this,function(resp){

            var state = resp.getState();

            if(state === 'SUCCESS'){
                console.log(resp.getReturnValue());
            }
            else if(state === 'ERROR'){
                var errors = resp.getError();
                for(var i = 0 ;i < errors.length;i++){
                    console.log(errors[i].message);
                }
            }

        });

        $A.enqueueAction(action);

    }
})

Apex Controller:

public with sharing class AccountController {

    //if sobject is specific for eg:Account,then you use List<Account> acc
    @AuraEnabled
    public static String insertAccounts(List<sObject> acc){
        try{
            insert acc;
            System.debug('acc'+acc);
            return 'success';
        }
        catch(DMLException ex){
           AuraHandledException e = new AuraHandledException(ex.getMessage());
           throw e;
        }
        return '';        
    }
}
-1

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 ? – CloudHugger Jul 22 '16 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 '16 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.