Help with UI Action & UI Page to relink records from one user to another

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 09:31 AM - edited 08-23-2024 09:42 AM
Hi all,
I am trying to create a UI Action--a Related Link (RL)--on the sys_user table that will take records linked to one user (the current user, a duplicate) and move them to another (primary) user so that I can delete the duplicate. I've been using a Fix script for this and it works fine, but requires me to update the script each time I use it.
In a PDI I have created a UI Action, "Prepare for Deletion," and a UI Page, "get_primary_user".
When the RL is clicked (from the duplicate user record), a prompt should open requiring one field to be populated with the sys_id of the primary user. When that is entered, I need a script that will look up records linked to the duplicate user and relink them to the primary user.
I have read that the "Processing script" is a server-side script, and am attempting to add a function there to move the records. (I am well aware that there can be many types of records linked to users, but to keep things simple here I am only including the lines that move Incidents, nothing else.)
There are a few issues I'm facing now, as follows:
- In the Processing script field, the function lines are all showing yellow warning underlines and, when I hover over these lines, a message appears saying, "Move function declaration to program root," though it does let me save it this way.
- When I click the RL, I am prompted for the sys_id input, but when I click OK, that same UI Page loads in place of the Incident record, taking up the entire page.
- When I check the test user records, the Incidents are not moved.
I have searched for similarly functioning records, reviewed myriad community posts, SNGurus, ServiceNow Elite, and so on, but have found nothing close enough to what I need to help me figure it out.
I would really appreciate some help with this if anyone is willing and able. Thank you all in advance!
UI Action
Name: Prepare for Deletion
Table: User [sys_user]
Active: true
Show insert: true
Show update: true
Client: true
Form link: true
Onclick: getPrimaryUserSysID()
Script:
function getPrimaryUserSysID() {
var sysId = typeof rowSysId == 'undefined' ? gel('sys_uniqueValue').value : rowSysId;
var gDialog = new GlideDialogWindow("get_primary_user");
gDialog.setTitle("Primary User");
gDialog.setPreference('sysparm_sysID', sysId);
gDialog.setPreference('sysparm_table', "sys_user");
gDialog.render();
}
UI Page
Name: get_primary_user
HTML:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:ui_form>
<input type="hidden" id="cancelled" name="cancelled" value="false" />
<input type="hidden" id="currentUserSysID" name="currentUserSysID" value="$" />
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td nowrap="true">
<g:ui_input_field label="Paste the Sys ID of the primary user:" name="sys_id" id="sys_id" mandatory="true" size="50" />
</td>
</tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="left" nowrap="true"><br />
<g:dialog_buttons_ok_cancel ok="return validateForm();" cancel="return onCancel();" />
</td>
</tr>
</table>
</g:ui_form>
</j:jelly>
Client script:
function onCancel() {
var c = gel('cancelled');
c.value = "true";
GlideDialogWindow.get().destroy();
return false;
}
function validateForm() {
if (gel('sys_id').value == '') {
alert("${JS:gs.getMessage('Please enter the primary user's Sys ID')}");
return false;
} else {
var grUser = new GlideRecord("sys_user");
var primaryUser = grUser.get(gel('sys_id').value);
if (primaryUser) {
return true;
} else {
alert("${JS:gs.getMessage('Primary user not found. Please verify the Sys ID and try again.')}");
return false;
}
}
}
Processing script:
if (cancelled == "false") {
var grPrimaryUser = new GlideRecordSecure("sys_user");
var primaryUser = grPrimaryUser.get(request.getParameter("sys_id"));
var wrongUserSid = currentUserSysID;
var rightUserSid = primaryUser.sys_id;
relinkRecords(wrongUserSid, rightUserSid);
function relinkRecords(wrong, right) {
// incident
var inc = new GlideRecord('incident');
inc.addEncodedQuery('caller_id=' + wrong + '^ORu_reported_by=' + wrong + '^ORopened_by=' + wrong);
inc.query();
while (inc.next()) {
if (inc.caller_id == wrong) {
inc.caller_id = right;
}
if (inc.u_reported_by == wrong) {
inc.u_reported_by = right;
}
if (inc.opened_by == wrong) {
inc.opened_by = right;
}
inc.setWorkflow(false);
inc.autoSysFields(false);
inc.update();
}
}
gs.addInfoMessage("All records relinked to " + primaryUser.first_name + " " + primaryUser.last_name + "(" + primaryUser.user_name + ").");
response.sendRedirect("sys_user.do?sys_id=" + currentUserSysID);
}