Best Practices in Scripting

If you want to Update, please do not Insert!

      There are scenarios when instead of updating a record, a script creates a new one. If it is the correct logic behind the Use Case it’s fine, but as good consultants we need to make sure it doesn’t happen if the client just want to perform updates.

      1.1 Use Case 1: The get() method

      In the script below we may think that the lines 9 to 11 are setting values to an existing record so in line 12 we’re just updating the record.

      In the right panel we can see that, as the record wasn’t found, a new record was created in the sys_user table!

Script commented

      To guarantee that we are updating a record, it is important to wrap the get() method in an if statement like this:


      If the record to be updated does not exist, the else statement is warning us about that fact and we’re not creating any unnecessary record.

      1.2 Use Case 2: Loading data

      Use the coalesce fields to locate records when loading data.

      The coalesce fields will inform the platform how a record can be identified to the respective fields will be updated accordingly.

      Like the first Use Case, we need to make sure that the coalesce fields are consistent. Otherwise, the platform will create new records instead of update the desired ones.