2

I have the following list definition in my Apex controller:

List<Object> mylist = new List<Object>();

And I would like to do something like:

mylist.add({
    key : objKey,
    label : objlabel,
    property1 : objproperty1
});

This code gives an error. Is there any way to achieve such a data structure in Apex?

4

You need to create a Wrapper class for this.

Example :

public class TestWrapper {

    public static void  pushValue(){
        list<fieldWrapper> n = new list<fieldWrapper>();

        fieldWrapper f = new fieldWrapper();
        f.key = 'a';
        f.label = 'b';
        f.property1 = 'c';
        n.add(f);
        system.debug('DD'+n);
    }

    public class fieldWrapper{
        public String key;
        public String label;
        public String property1;
    }

}
0

It looks like you're trying to instantiate a new object without creating a variable, so you can add it directly to a list.

If you know the type of the object, and that object is an sObject or a child of an sObject (any standard object type or custom object), you can use this method to instantiate a new instance of the record.

List<sObject> someSObjectList = new List<sObject>(); 

someSObjectList.add(
    new Account(
        Name = 'Some Name'
    )
);

someSObjectList.add(
    new Some_Custom_Object__c(
        Name = 'Some Name',
        Custom_Field__c = 6
    )
);

I don't think you'll be able to create a new Object without using JSON.serialize on a Map<String, Object>, then JSON.deserialize. I've included an example below.

Map<String, Object> fields = new Map<String, Object>(); 

// required in order to deserialize into an sObject 
fields.put('attributes', new Map<String, String>{
    'type' => 'Account' // Type of record 
}); 

// Add fields by name 
fields.put('Name', 'Test Name'); 

sObject result = JSON.deserialize(JSON.serialize(fields), sObject.class);

I can see a use case where you're getting some data from a custom setting or other storage method, and need to dynamically create records based on preset data. I wrote an implementation I would use to create objects quickly:

public static sObject CreateSObject(String typeOf, Map<String, Object> fields) {
    Map<String, Object> record = new Map<String, Object>(); 

    record.put('attributes', new Map<String, String>{
        'type' => typeOf
    }); 

    record.addAll(fields);

    return JSON.deserialize(JSON.serialize(fields), sObject.class); 
}

sObject someAccount = CreateSObject('Account', new Map<String, Object> {
    'Name' => 'Test Account',
    'Some_Custom_Field__c' => 7
}); 

Regardless of the method, you won't get too far without knowing the type of the object you are trying to create. Apex handles generics very, very, very poorly. You're almost always better off using concrete types or interfaces than trying to use a generic object. Take a look around the stack, and you'll find a lot of other questions on the type system and its flaws.

  • Thank you for your answer! I was more trying to create a simple associative array that could handle more than one property and be accessible with keys, but a wrapper class will do! Thank you for your explanations! – thiout_p Nov 29 '17 at 8:53
-1

Please go through below document and try to understand salesforce collection

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections.htm

You can achieve that requirement through wrapper class:

   public class  MainClass{

   public static void  wrapperApproach(){
    list<wrapperclass> wraperList = new list<wrapperclass>();

    wrapperclass   wc= new wrapperclass();
    wc.key = 'objKey';
    wc.label = ' objlabel';
    wc.property1 = 'objproperty1';
    wraperList.add(wc);

}

public class wrapperclass{
    public String key;
    public String label;
    public String property1;
}}
-1

you can type it as list<map<string,string>>:

list<map<string,string>> mylist = new list<map<string,string>>();
mylist.add(new map<string, string>({'key':objkey, 'label':objlabel, 'property1':objproperty1}));

or whatever the crazy java syntax for static list initializer is.

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.