Sunday, March 13, 2022

Logical Operators in Javascript (Part-I)

 

In this blog, we will learn about logical operators in JavaScript.

Trust me, it's going to be an amazing journey as logical operators in JavaScript are a little different 👿


We have 4 logical operators as below : 

  • OR ( || ) 
  • Null Coalescing ( ?? ) 
  • AND ( && ) 
  • NOT ( ! ) 

We will learn about the OR operator first.

  1. OR ( || )

In most languages OR is supposed to be used with Boolean values and give out only Boolean values ie return TRUE if any of the operands are TRUE else return FALSE ie 

    • TRUE || TRUE  -- > TRUE 
    • FALSE || TRUE --> TRUE 
    • TRUE || FALSE -- > TRUE 
    • FALSE || FALSE --> FALSE

But in JavaScript this operator works a little differently, we can not only use it Boolean operands but also with everything else as well ( any data type).

And No output will also not be necessarily Boolean rather can be any data type 🤯

OR operator works on the basis of truthy and falsy values (Refer to MDN docs if you need a refresher on truthy and falsy values)

Now let's look at the OR operator working. It evaluates all operands from left to right. If the left operand is truthy value. It will return it else it will return 2nd operand. 

If I try to put the above statement in other words, we already know that the OR operator returns TRUE unless both operands are FALSE ie 

If the first operand is a truthy value, it doesn't matter what the next operand is as output is going to be a truthy value or say first operand.

Only if the first operand is falsy value, now output will depend on 2nd operand. If it's truthy value it will lead to truthy value else a falsey value.

Let's look at some examples. 


We only discussed and saw examples, when we have only 2 operands but what If we have chain of operands. What happens in that case? 

Well, OR operator still acts the same and traverses from L-R searching for truthy values, and as soon as a truthy value is found it's returned else it keeps on moving towards the end of the chain and if no truthy value is found it returns the last value.

Let's look at an example


 

Note: While processing from L to R as soon as the first truthy value is returned and all the other operands are left untouched, this is called short-circuiting.

Let's take a look at another example to understand short-circuiting better.


If you notice in the above example fun1 and fun2 were never even executed because of short-circuiting.

Note: Remember in the JavaScript output of OR operator is not going to be necessarily Boolean rather will depend on which operand will get returned after evaluation and can lead to any data type


Thanks.

If you liked the post do support me by buying a pizza for me :D.

 

 

Thursday, March 10, 2022

Multiple objects in single DML ?

This is an extension of our earlier post where we read about how we can insert both parent and child in a single DML. If you haven't read it yet visit it by clicking.

Note: Usage of single DML to insert/update different object is not just limited to objects which are in a relationship rather it could be simply the creation of unrelated different objects on the occurrence of a certain event.

We already know the limit of the number of records that can be updated by a DML is 10,000 (Remember the Limit is for the whole transaction, not just one DML but in our case, our transaction consists of only one DML).

Now questions come to mind, can I also insert List<Sobject> that contains a mixture of Accounts and Contact records in total (2,400) records.

Before reading further try to think of the answer...

The default expectation is "Yes", having multiple Objects in the List should not impact the 10,000 limits and 2,400 is way below 10,000. But unfortunately, that is not the case.

If you try to insert 1,200 Accounts and 1,200 Contacts that are unrelated, below is a snippet of code used to add records.



You will receive the below error.

Error



But why this error?

When we know we can easily insert 10,000 records of Accounts or any other object for that matter when inserted/updated individually. (Code example below).


On deep diving into the issue we figured, when working with multiple objects, salesforce traverses through the list and creates chunks of records. 

Chunk is nothing but a subset of input array or list and each chunk contains records as per the below rules.

  • Each chunk will only contain records of one object type.
  • If object changes while traversing the list, the chunk also changes.
Assume you have a list of records as A1, A2, C1, C2, A3. 
They will be distributed as 
Chunk 1 --> A1, A2 
Chunk 2 --> C1, C2
Chunk 3 --> A3

  • In a chunk maximum of 200 records can come, the 201st record will be placed in the 2nd chunk.
  • In total for a successful DML, you can have a maximum of 10 chunks. 
  • Derivation from the above rule we can also say we can have at most 10 different types of objects in a Single DML which will lead to 10 different chunks
  • Data is committed chunk by chunk ie if for example, any trigger exists on records it will be called once for each chunk of records.

Taking the above example as a base,  when the Account trigger is called ( Trigger.new will only contain A1 and A2 first time then it will be called again (for chunk 3) and this time it will only contain A3.


Note: Chunking comes into the picture only if you are working with multiple objects, when doing data manipulation on a single object no chunking takes place.


Let's look at another example where DML could fail.

Example 1 :

Here we are adding different instances of multiple objects in a single list and inserting them.


But if you try to run above code it will fail although we are only inserting 11 records, it fails as it will lead to 11 chunks.


Just because every time an object changes, chunk also changes we can simply sort all the same type of object instances so they are part of a single chunk (200 max). 
We can resolve the above code by simply using sort() method which will sort the list putting all same types of instances in order and which will reduce chunk size to 5.(Code below) 


To resolve the earlier example of 1200 Accounts and 1200 Contacts we will have to distribute it in two DMLs as it can't be achieved via a single DML.



To summarize the topic, I will say there can be 3 scenarios if we are working with multiple Sobjects.

  • We have a hybrid list that contains different types of object records which are scattered like (Account1, Contact1, Lead1, Account2 ...). In this type of hybrid list, we can simply sort the list to reduce the chunk size.
  • We have two or three objects but their numbers are huge for example 1200 Accounts, 1200 Contact which will lead to 12 chunks, in this case, we will have to simply divide the records into multiple DMLs in case the number of chunks is above 10 otherwise we can use single DML.
  • If you are working with more than 10 types of objects, you will again end up with the same error as every time object changes chunk also changes. In this case, you will have to separate out DML as well. Depending on the use case you can also use Async transaction to divide DML as doing multiple DML in a single context may not be very good.

Thanks.


If you liked the post do support me by buying a pizza for me :D.