Working with Salesforce Records

It’s easy to work with your Salesforce records in Apex.

The term sObject refers to any object that can be stored in Force.com. This could be a standard object, such as Account, or a custom object that you create, such as a Merchandise object.

An sObject variable represents a row of data, also known as a record. To work with an object in Apex, declare it using the SOAP API name of the object. For example:

Account a = new Account();
MyCustomObject__c co = new MyCustomObject__c();

For more information on working on records with Apex, see Working with Data in Apex.

This example controller persists an updated Account record. Note that the update method has the @AuraEnabled annotation, which enables it to be called as a server-side controller action.

public with sharing class AccountController {

    @AuraEnabled
    public static void updateAnnualRevenue(String accountId, Decimal annualRevenue) {
        Account acct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :accountId];
        acct.AnnualRevenue = annualRevenue;

        // Perform isAccessible() and isUpdateable() checks here 
        update acct;
    }
}

For an example of calling Apex code from JavaScript code, see the Quick Start.

Loading Record Data from a Standard Object

Load records from a standard object in a server-side controller. The following server-side controller has methods that return a list of opportunity records and an individual opportunity record.
public with sharing class OpportunityController {

    @AuraEnabled
    public static List<Opportunity> getOpportunities() {
        List<Opportunity> opportunities = 
                [SELECT Id, Name, CloseDate FROM Opportunity];
        return opportunities;
    }

    @AuraEnabled
    public static Opportunity getOpportunity(Id id) {
        Opportunity opportunity = [
                SELECT Id, Account.Name, Name, CloseDate, 
                       Owner.Name, Amount, Description, StageName
            FROM Opportunity
            WHERE Id = :id
         ];

        // Perform isAccessible() check here 
        return opportunity;
    }
}
This example component uses the previous server-side controller to display a list of opportunity records when you press a button.
<aura:component controller="OpportunityController">
    <aura:attribute name="opportunities" type="Opportunity[]"/>
    
    <ui:button label="Get Opportunities" press="{!c.getOpps}"/>
    <aura:iteration var="opportunity" items="{!v.opportunities}">
    	<p>{!opportunity.Name} : {!opportunity.CloseDate}</p>
    </aura:iteration>
</aura:component>
When you press the button, the following client-side controller calls the getOpportunities() server-side controller and sets the opportunities attribute on the component. For more information about calling server-side controller methods, see Calling a Server-Side Action.
({
    getOpps: function(cmp){
        var action = cmp.get("c.getOpportunities");
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.opportunities", response.getReturnValue());
            }
        });
	 $A.enqueueAction(action);
    }
})
Note

Note

To load record data during component initialization, use the init handler.

Loading Record Data from a Custom Object

Load record data using an Apex controller and setting the data on a component attribute. This server-side controller returns records on a custom object myObj__c.
public with sharing class MyObjController {
    
    @AuraEnabled
    public static List<MyObj__c> getMyObjects() {

        // Perform isAccessible() checks here 
        return [SELECT Id, Name, myField__c FROM MyObj__c];
    }
}

This example component uses the previous controller to display a list of records from the myObj__c custom object.

<aura:component controller="MyObjController"/>
<aura:attribute name="myObjects" type="namespace.MyObj__c[]"/>
<aura:iteration items="{!v.myObjects}" var="obj">
    {!obj.Name}, {!obj.namespace__myField__c}
</aura:iteration>
This client-side controller sets the myObjects component attribute with the record data by calling the getMyObjects() method in the server-side controller. This step can also be done during component initialization using the init handler.
getMyObjects: function(cmp){
    var action = cmp.get("c.getMyObjects");
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS") {
            cmp.set("v.myObjects", response.getReturnValue());
        }
    });
    $A.enqueueAction(action);
}

For an example on loading and updating records using controllers, see the Quick Start.