Skip to content

Commit 5752145

Browse files
authored
Merge branch 'ServiceNowDevProgram:main' into main
2 parents c3f00eb + 5204396 commit 5752145

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-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);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Function that generates clickable HTML links to records in different UI contexts (Native UI, Workspace or Portal)
2+
3+
4+
5+
6+
Sample background Script:
7+
8+
var record_sysid = '';// add the record sys_id here.
9+
gs.info(new global.GetRecordDetails().getClickableURL('incident', record_sysid, 'INC- Workspace', 'workspace', 'cwf/agent'));
10+
11+
gs.info(new global.GetRecordDetails().getClickableURL('incident', record_sysid, 'INC- Portal', 'portal', 'sp'));
12+
13+
gs.info(new global.GetRecordDetails().getClickableURL('incident', record_sysid, 'INC - NativeUI', 'native'));
14+
15+
gs.info(new global.GetRecordDetails().getClickableURL('', record_sysid, 'INC - NativeUI', 'native'));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var GetRecordDetails = Class.create();
2+
GetRecordDetails.prototype = {
3+
initialize: function() {},
4+
/*
5+
table - table name of the record.
6+
record - sysid of the record.
7+
display_text - Display text of the clickable URL
8+
urlUI - the URL Type - Accepts workspace/portal and native UI as default.
9+
uiName - mandatory parameter if urlUI is workspace or portal.
10+
11+
*/
12+
getClickableURL: function(table, record, display_text, urlUI, uiName) {
13+
try {
14+
var grRecord = new GlideRecord(table);
15+
if (grRecord.get(record)) {
16+
var instance_url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/';
17+
var path;
18+
19+
if (urlUI == 'workspace' && uiName != '') {
20+
path = "now/" + uiName + "/record/" + table + "/" + record;
21+
} else if (urlUI == 'portal' && uiName != '') {
22+
path = uiName + "?sys_id=" + record + "&view=sp&id=ticket&table=" + table;
23+
} else {
24+
path = "nav_to.do?uri=" + table + ".do?sys_id=" + record;
25+
}
26+
// final URL
27+
var link = instance_url + path;
28+
var refLink = '<a href="' + link + '" target="_blank">' + display_text + '</a>';
29+
return refLink;
30+
31+
} else {
32+
gs.info('Record does not exist');
33+
return '';
34+
}
35+
} catch (e) {
36+
gs.info("Exception occured in getClickableURL method: " + e.message);
37+
return '';
38+
}
39+
},
40+
type: 'GetRecordDetails'
41+
};

0 commit comments

Comments
 (0)