Trigger.newMap.keySet() gives you id of the all record which are going to update but in the case of before insert you will get null because record still don’t have any id as it is going to insert
Trigger.newMap: It is a map of all records in your trigger. The key used in the map is the record ID, and the value is the record itself.
.keySet() is a method used on maps that returns all the keys of the map. Since the key in the map above is the record ID, you are returned a set of all IDs on your map.
The key used in the map is the record ID, and the value is the record itself.
.keySet() is a method used on maps that returns all the keys of the map. Since the key in the map above is the record ID, you are returned a set of all IDs on your map.