Update a table based on another table

Facundo Prado
Tera Expert

Hello Everyone!
I have 2 tables (table_1 and table_2). with 3 fields each one (field_!, field_2 and field_3).
I have a business rule(Insert, update and delete), and I need script the business rule to edit table_2 based on table_1. (When a record is inserted, updated and deleted)... I´ve thought on a SWITCH, but can you help me with this?

This is the script that I´ve done:

 

 

(function executeRule(current, previous /*null when async*/ ) {
    var appId = current.getValue('u_application_id');
    var customTable = new GlideRecord('table_2');
    customTable.addQuery('u_app_name', current.getUniqueValue());
    customTable.setLimit(1);
    customTable.query();

    if (customTable.next()) { 

        switch (current.operation()) {
            case 'insert':
                customTable.initialize();
                customTable.field_1 = appId;
                customTable.field_2 = current.getUniqueValue();
                customTable.field_3 = current.getValue('u_assigment_group');
                customTable.insert();
                break;

            case 'update':
                customTable.field_1 = appId;
                customTable.field_2 = current.getUniqueValue();
                customTable.field_3 = current.getValue('u_assigment_group');
                customTable.update();
                break;

            case 'delete':
                customTable.setWorkflow(false); // Disable workflow if needed
                customTable.deleteRecord();
                break;
        }
    } else if (current.operation() === 'insert') {
        customTable.initialize();
        customTable.field_1 = appId;
        customTable.field_2 = current.getUniqueValue();
        customTable.field_3 = current.getValue('u_assigment_group');
        customTable.insert();
    }

})(current, previous);

 


Thanks!!

1 REPLY 1

JPing
Tera Expert

Create a reference field on Table 1 to Table 2. 

Write 3 business rules one for update, insert and delete. 

In the insert rule - use the advance script to create a record in table_2 and set all it fields the same. Then set the reference to the created table_2 record 

In the update rule - use the advance script to dot walk to the corresponding table_2 record and update all if field accordingly. 

in the Delete rule - user the advance script to delete the reference records in table_2. 

 

 

Without knowing any of the business reason why you are doing this, I would use the above. But i think if i could understand why you are trying to keep two tables in sync, then i might be able to think up a better solution.