Skip to content

Commit 0d7a3d6

Browse files
Synchronization of fields between two tables (#2443)
* Create README.md * Create script.js
1 parent 9ce4d27 commit 0d7a3d6

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
**Scenario**: Synchronize fields between two different tables.
2+
3+
**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.
4+
Fields on the case that are derived from the HR Profile are excluded from this synchronization.
5+
6+
**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.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(function executeRule(current, previous /*null when async*/ ) {
2+
// Check if the Subject Person of case is empty
3+
if (gs.nil(current.subject_person))
4+
return;
5+
// Subject person field reference to User Table. Check if the HR profile is present for the user.
6+
var profileGR = new GlideRecord('sn_hr_core_profile');
7+
if (!profileGR.get(current.subject_person))
8+
return;
9+
10+
// Get all fields from the current record (case)
11+
var elements = current.getElements();
12+
13+
// Loop through each field on the case and get the name
14+
for (var i = 0; i < elements.size(); i++) {
15+
var field = elements.get(i);
16+
var fieldName = field.getName();
17+
18+
// Skip system fields and derived fields of hr profile (If any)
19+
if (fieldName.startsWith('sys_') || fieldName === 'hr_profile')
20+
continue;
21+
22+
var newValue = current.getValue(fieldName);
23+
var oldValue = previous.getValue(fieldName);
24+
25+
// Only act if value changed
26+
if (newValue != oldValue) {
27+
// Check if the same field exists in HR profile and is accessible
28+
if (profileGR.isValidField(fieldName)) {
29+
profileGR.setValue(fieldName, newValue);
30+
}
31+
}
32+
}
33+
profileGR.update();
34+
35+
})(current, previous);

0 commit comments

Comments
 (0)