16

You define a list variable and initialize it at the same time as shown in the following example

List<String> myStrings = new List<String> { 'one', 'two' };

How would I do this for a static list of object within my PostInstallScript? I know it is not necessary, however I feel my current code (below) is not very optimal and I am making multiple calls to the list Of Codes collection.

List<Special_Codes__c> listOfCodes = new List<Special_Codes__c>();
listOfCodes.add(new Special_Codes__c(Name='A1', Meaning__c='Special Customer', Type__c='Customer Code'));
listOfCodes.add(new Special_Codes__c(Name='A2', Meaning__c='Bad Customer', Type__c='Customer Code'));
listOfCodes.add(new Special_Codes__c(Name='DF', Meaning__c='Poor Customer', Type__c='Customer Code'));
listOfCodes.add(new Special_Codes__c(Name='A3', Meaning__c='Rich Customer', Type__c='Customer Code'));
listOfCodes.add(new Special_Codes__c(Name='Z1', Meaning__c='Household Customer', Type__c='Customer Code'));
listOfCodes.add(new Special_Codes__c(Name='Z3', Meaning__c='Average Customer', Type__c='Customer Code'));
listOfCodes.add(new Special_Codes__c(Name='ZX', Meaning__c='Private Business', Type__c='Customer Code'));
listOfCodes.add(new Special_Codes__c(Name='AB', Meaning__c='Homebased Business', Type__c='Customer Code'));

2 Answers 2

23

This cuts down on noise in the code and so is worth doing whether or not there is any performance benefit:

List<Special_Codes__c> listOfCodes = new List<Special_Codes__c>{
        new Special_Codes__c(Name='A1', Meaning__c='Special', Type__c='Customer Code'),
        new Special_Codes__c(Name='A2', Meaning__c='Bad', Type__c='Customer Code'),
        ...
        };

Also personally I prefer the (largely interchangeable with the list syntax but shorter) array syntax:

Special_Codes__c[] listOfCodes = new Special_Codes__c[] {
        new Special_Codes__c(Name='A1', Meaning__c='Special', Type__c='Customer Code'),
        new Special_Codes__c(Name='A2', Meaning__c='Bad', Type__c='Customer Code'),
        ...
        };
1
  • one comment - if you are initializing an array of objects (not custom objects) in apex it won't let you initialize using name/value pairs in the constructor. This will work though - MySpecialClass[] listOfCodes = new MySpecialClass[] { new MySpecialClass (A1', 'Special', 'Customer Code'), new MySpecialClass ('A2', 'Bad', Customer Code'), ... }; Jul 20, 2017 at 19:00
2

to me what you are doing looks ok.. since you are creating multiple records of an object with each record having different field values.

you can try different approaches.. but all these might end up in multiple lines either during initialisation or during the object creation.

  1. Using Nested List

one way is to store the field values in an array / nested list and do loop thru the array to initialise the objects and then add the objects into the collection..

List<List<String>> fieldValues = 
      new List<List<String>> {
          new List<String>{'A1', 'Special Customer'},
          new List<String>{'A2', 'Bad Customer'},
          new List<String>{'A3', 'Poor Customer'}
      };

List<Special_Codes__c> listOfCodes = new List<Special_Codes__c>();

for(List<String> fields : fieldValues){
    listOfCodes.add(new Special_Codes__c(Name=fields[0], Meaning__c=fields[1], Type__c='Customer Code'));
  1. Using JSON

or you can try storing the fieldValues as a json string and deserialize it back to the object and add it to the collection. I havn't tried this but if you store the correct JSON representation of your record values, you can do something like

List<Special_Codes__c> listOfCodes = (List<Special_Codes__c>) JSON.deserialize(youlistCodeJSON, List<Special_Codes__c>.class);

you can refer this article for handling JSON in APEX which has some code samples for serializing & deserializing

You must log in to answer this question.

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