Sorry for the title; didn’t know what to call this! Ever so often I’ll run a fix script to perform some sort of action on a GlideRecord (updating an attribute on a number of records for example).
With this idea, I created a UI action that generates a fix script template based on the current record. This script includes the basic structure for loading the specific record and leaves space for you to define the operations you want to perform on it.
To implement this, you need to create a new UI action and use the provided script below. You can customize it according to your needs. I’m sharing this in case anyone else finds it as useful as I do. Let me know if you have any thoughts or suggestions for improvement! This will only show for admins due to the conditions.
Name | Create fix script to get this record |
Table | global |
Form link | true |
Show update | true |
Conditions | gs.hasRole(“admin”) |
In the script area, add the following:
// Retrieving the relevant data from the current context: table name, record ID, and user var tableName = current.getTableName(); var recordSysId = current.sys_id.toString(); var username = gs.getUser().getName(); // Creating a unique name for the fix script, using the retrieved username and table name var fixScriptName = "Auto Fix Script: " + username + " - " + tableName; // Generating the fix script, which will load a record from the table and // then perform operations on it (operations to be defined by the user) var fixScript = "// Generated Fix Script\n" + "var record = new GlideRecord('" + tableName + "');\n" + "if (record.get('" + recordSysId + "')) {\n" + " // perform operations on the record here\n" + "}\n"; // Logging the generated script to the system log gs.info("Generated Fix Script: \n" + fixScript); // Creating a new record in the 'sys_script_fix' table, to save the generated fix script var fix_script = new GlideRecord('sys_script_fix'); fix_script.name = fixScriptName; fix_script.description = "Automatically created via UI action."; fix_script.script = fixScript; // Inserting the new fix script record into the system and storing the returned sys_id var fixScriptSysId = fix_script.insert(); // Redirecting the user to the newly created fix script record in the system action.setRedirectURL(fix_script);
Once done, submit the UI action. You should now see a new UI action appear in the form of a link on the various records. It should look something like this:
When you run the UI action on a record you should get something like this: