10

I would like to create an object and set its properties in one statement in Apex. Ideally, the syntax would be similar to C# object initializers. Does this exist in Apex?

// Create a Student instance and set some property values
Student student = new Student
{
    FirstName = "Jane",
    LastName = "Doe",
};
1

4 Answers 4

8

This is the syntax

Student student = new Student(FirstName = "Jane",LastName = "Doe");
1
  • 2
    This only works with SObjects, but I'll mark as accepted anyway. For non-SObjects, you'll have to take Fernando Gavinho's advice and create a custom constructor.
    – Joe B
    Commented Nov 15, 2016 at 20:32
15

The correct answer is it depends.

Currently salesforce only accepts that for sObjects. Other Objects (1) cannont be initialized that way.

You can however, create custom constructors like:

public class Student{

        private String firstName;
        private String lastName;

        public Student(String firstName, String lastName){
            this.firstName = firstName;
            this.lastName = lastName;
        }
}

(1) as per Derek's comment: an object defined in an Apex class using the 'class' keyword, can be a top level class or a inner class

1
  • 3
    I'd argue that this means the answer is the ever-useful it depends rather than a flat no. Also might be helpful to note that by Normal Object, you mean an object defined in an Apex class (.cls) file using the 'class' keyword
    – Derek F
    Commented Nov 15, 2016 at 17:06
3

You can create a sObject and initialise its properties using the following examples.

// Create a Student instance and set some property values
Student student = new Student(
    FirstName = "Jane",
    LastName = "Doe",
);

Student student2 = new Student();
student2.FirstName = "Jim";
student2.LastName = "Doe";

I would suggest taking a look into this Trailhead module that will guide you through using sObjects.

1
  • 1
    this doesn't work on custom objects like the one in your example. Commented Dec 24, 2017 at 10:28
3

If you would like similar functionality for custom classes, you can add a constructor that takes a Map<String, Object> and assign using that.

public class Student
{
    private String firstName;
    private String lastName;
    private Integer grade;

    public Student(Map<String, Object> initMap)
    {
        firstName = (String)initMap.get('firstName');
        lastName = (String)initMap.get('lastName');
        grade = (Integer)initMap.get('grade');
    }
}

Student student1 = new Student(new Map<String, Object>{'firstName' => 'John', 
                              'lastName' => 'Doe', 'grade' => 90});

Of course, this requires a decent amount of upkeep and can't really deal with bad keys or values until runtime, so it is probably not something you should plan on adding to every class. It could be useful for classes with lots of optional fields, though.

You must log in to answer this question.

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