I’ve had to common use case in ServiceNow where I want to take an existing record, and then create a copy of that record via script. Sometimes I will want to dynamically set values, or hardcode certain values. So I wanted a helper that would let me take an existing record, and craft a Backgroud Script that would insert a new GlideRecord that matches what I’m currently seeing.
snUtils provides a nice slashcommand called /crn which will take the current record, construct a query string with the record values, and then take you to a new record form with those record values filled in by taking you to a URL like: table.do?sys_id=-1&sysparm_query=key=value&key2=value2&…
The syntax is that using sysparm_query in a new record (sys_id=-1) will pre-fill those field values on the form. I took this logic, and created a new function that will take all fields on the form, and construct a GlideRecord script to insert a new record with those values. There are two versions. One only shows the fields visible currently on the form, and the other shows all fields.
Copy All Fields to GlideRecord Script
javascript: (function () { if (!g_form) return; let blacklistedFields = ['number','sys_scope']; let scriptLines = [`var current = new GlideRecord("${g_form.getTableName()}");`]; g_form.elements.forEach(el => { if (el.fieldName.startsWith('sys') || blacklistedFields.includes(el.fieldName) || el.fieldName.indexOf('.') !== -1) return; { scriptLines.push(`current.${el.fieldName} = "${g_form.getValue(el.fieldName)}";`); } }); scriptLines.push('current.insert();'); window.open(`/sys.scripts.do?content=${encodeURIComponent(scriptLines.join('\n'))}`, '_blank'); })();
Copy Visible Fields to GlideRecord Script
javascript: (function () { if (!g_form) return; let blacklistedFields = ['number','sys_scope']; let scriptLines = [`var current = new GlideRecord("${g_form.getTableName()}");`]; g_form.elements.forEach(el => { if (el.fieldName.startsWith('sys') || blacklistedFields.includes(el.fieldName) || el.fieldName.indexOf('.') !== -1) return; if (g_form.isFieldVisible(el.fieldName) && g_form.getValue(el.fieldName) !== '') { scriptLines.push(`current.${el.fieldName} = "${g_form.getValue(el.fieldName)}";`); } }); scriptLines.push('current.insert();'); window.open(`/sys.scripts.do?content=${encodeURIComponent(scriptLines.join('\n'))}`, '_blank'); })();
You will need to create custom slashcommands using this code. Below is a screenshot where I created these as /bgcopy and /bgcopyall.

Then to use them, open a record, use one of the commands like “/bcgcopyall” and it will load you into a background script form, pre-filled with a GlideRecord script like this:
