when instantiating an SObject type, I can set initial field values by passing them to the constructor. For example like this:

Account a = new Account(name = 'Acme', billingcity = 'San Francisco');

I'm wondering what exactly, in terms of Apex language primitives, is name = 'Acme', billingcity = 'San Francisco' in that example. In other words, what is SObject's constructor method signature? The docs are not very helpful on this topic.

Staring at the example above, knowing that variable = expression is a common syntax for assignment and that assignments often return the assigned value, my initial guess would be that - in this case - the constructor receives two String arguments, 'Acme' and 'San Francisco'. However, that is obviously not the case.

Semantically the argument(s) also resemble a Map literal, but the syntax does not match. Closer, but still no cigar.

To put my question in practical context, try building the constructor argument(s) dynamically, eg. from an arbitrary Map.

2 Answers 2


This is a named parameter language feature that is only supported for specific SObject types and nowhere else in Apex. It makes code where you want to assign several values cleaner - see this example. Agreed that the documentation isn't good on this.

If you want to initialise from a map, you have to write your own utility method such as:

public void someMethod() {
    Account a = (Account) init(new Account(), new Map<SObjectType, Object> {
            Account.Name => 'Acme',
            Account.BillingCity => 'San Francisco'

private SObject init(SObject sob, Map<SObjectField, Object> values) {
    for (SObjectField field : values.keySet()) {
        sob.put(field, values.get(field));
    return a;

that makes use of SObject.put(SobjectType, Object).

You can also use strings as the keys (though you lose the compile-time checking that the field names are valid) as there is also SObject.put(String, Object).

  • Thanks, Keith! So, essentially, a "special" Apex language syntax applies to SObject constructor arg lists. In that context "a = b" is a named parameter (key-value pair), while elsewhere it is an assignment statement. A rather interesting case of programming language design, imho. Aug 5, 2014 at 8:16

The parameters are repeated field assignments - you can have as many of these as you like. The syntax is fieldname=fieldvalue, where fieldvalue is appropriate for that field type. This works for standard and custom fields, outside of those assigned by the system such as createddate, lastmodifieddate.

Field value can be any primitive type that is appropriate for the field in question, so a checkbox would require a boolean, a text/picklist would require a string, a datetime would require a Datetime etc.

You must log in to answer this question.

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