diff --git a/Server-Side Components/Business Rules/Sync Fields for two tables/README.md b/Server-Side Components/Business Rules/Sync Fields for two tables/README.md new file mode 100644 index 0000000000..6cebac4140 --- /dev/null +++ b/Server-Side Components/Business Rules/Sync Fields for two tables/README.md @@ -0,0 +1,6 @@ +**Scenario**: Synchronize fields between two different tables. + +**Example**: Any changes made to the fields on the New Hire HR Case for a user, where the same field also exists on the HR Profile, will automatically be updated on the HR Profile when the field is modified on the case. +Fields on the case that are derived from the HR Profile are excluded from this synchronization. + +**Script Logic**: An after-update business rule that checks the updated fields in the current table (HR case) exist in the other table (HR Profile) and updates them accordingly. diff --git a/Server-Side Components/Business Rules/Sync Fields for two tables/script.js b/Server-Side Components/Business Rules/Sync Fields for two tables/script.js new file mode 100644 index 0000000000..a4cc26599f --- /dev/null +++ b/Server-Side Components/Business Rules/Sync Fields for two tables/script.js @@ -0,0 +1,35 @@ +(function executeRule(current, previous /*null when async*/ ) { + // Check if the Subject Person of case is empty + if (gs.nil(current.subject_person)) + return; + // Subject person field reference to User Table. Check if the HR profile is present for the user. + var profileGR = new GlideRecord('sn_hr_core_profile'); + if (!profileGR.get(current.subject_person)) + return; + + // Get all fields from the current record (case) + var elements = current.getElements(); + + // Loop through each field on the case and get the name + for (var i = 0; i < elements.size(); i++) { + var field = elements.get(i); + var fieldName = field.getName(); + + // Skip system fields and derived fields of hr profile (If any) + if (fieldName.startsWith('sys_') || fieldName === 'hr_profile') + continue; + + var newValue = current.getValue(fieldName); + var oldValue = previous.getValue(fieldName); + + // Only act if value changed + if (newValue != oldValue) { + // Check if the same field exists in HR profile and is accessible + if (profileGR.isValidField(fieldName)) { + profileGR.setValue(fieldName, newValue); + } + } + } + profileGR.update(); + +})(current, previous);