Inline Lists and Maps, how they help us save a lot of development time.

Inline Lists and Maps helps us build robust business logic very quickly.

Inline Lists and Maps, how they help us save a lot of development time.

List's and Map's play a very key role when you work on a Salesforce development project.

Here am gonna walk you through how to save a lot of time using inline List and Maps.

Inline Lists

Let's start off with inline Lists.

Inline List's produce the same output as that of the Traditional List's with the only difference that it takes a less effort to use them.

When you are want to work with a list of strings, you can do it in either of the two ways shown below.

However, the second approach saves a lot of time and it's more verbose (create List of string values with the values being John, Done, Jane).

//this can be refactored
List<String> s = new List<String>();
s.add('John');
s.add('Doe');
s.add('Jane');
System.debug(' 🚀 ' +s);


//to this
List<String> s = new List<String>{'John', 'Doe', 'Jane'};
System.debug(' 🚀 ' +s);

Now let's look at a different use case where we can make use of Inline Lists.

When you want to create a list of new sObject records, you can opt for any of the two below mentioned approaches.

However, second approach saves a lot of time.

//this can be refactored
List<Account> accounts = new List<Account>();
accounts.add(new Account(Name='Apple'));
accounts.add(new Account(Name='Microsoft'));
accounts.add(new Account(Name='Google'));

insert accounts;
System.debug(' 🚀 ' +accounts);

//to this
List<Account> accounts = new List<Account>{
	new Account(Name='Apple'),
	new Account(Name='Microsoft'),
	new Account(Name='Google')};

insert accounts;
System.debug(' 🚀 ' +accounts);

Inline Maps

Now let's check how can we use inline Maps.

As mentioned earlier, even with Inline Maps we are not looking at re-inventing the wheel, rather, we are just tweaking things a bit to get the same result with less no of lines of code and much cleaner code.

In case you are interested in creating a Map with keys being Id of the records and values being the actual record, you can opt for the second approach (you can check how simple it is).

//this can be refactored
Map<Id, Account> accountsMap = new Map<>(Id, Account);
List<Account> accountsList = [SELECT Id FROM Account];
for(Account a : accountsList){
	accountsMap.put(a.Id, a);
}
System.debug(' 🚀 ' +accountsMap);

//to this
Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id, Name FROM Account]);
System.debug(' 🚀 ' +accountsMap);

Let me know in case you have any other shortcuts that you follow.

You can also check my other tips here.