- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2024 06:28 PM
Greetings from a humbled colleague.
My assignment is to create a "copy to new record" UI action button on a custom table (and its related tables records. I thought it would be simple, following the example given by Mark Stanger in 2009; nope. I was able to build an array with the interesting column names, but when I create a new record with initialize then try to copy the values in a for in loop, it doesn't work. If I find the solution I will post it here, and welcome any guidance offered. Here is the code thus far:
Kind Regards,
Woody
//This phase collects the field names '.element' from the table
var columns = new GlideRecord('sys_dictionary');
columns.addEncodedQuery('name=x_197547_ocio_purc_x_197547_task_ocio_purchase_requests^ORname=task^elementNOT LIKEapproval');
columns.query();
var prFields = [];
while (columns.next()) {
prFields.push("." + columns.element.toString());
}
gs.info("prFields array contains these values: " + prFields);
gs.info("prfields length is " + prFields.length);
// https://servicenowguru.com/ui-actions-system-ui_copy-ui-action-change-requests-part-2/
// https://servicenowguru.com/ui-actions-system-ui_copy-ui-action-change-requests/
//Get the current sys_id value for querying
var prID = current.sys_id.toString();
var rec = new GlideRecord('x_197547_ocio_purc_x_197547_task_ocio_purchase_requests');
rec.get(prID);
gs.info("copied request is: " + rec.number);
copyRequest();
function copyRequest() {
//Initialize new record for insertion
var newRequest = new GlideRecord('x_197547_ocio_purc_x_197547_task_ocio_purchase_requests');
newRequest.initialize();
gs.info("new request number is " + newRequest.number);
gs.info("new request sysid is " + newRequest.sys_id);
//newRequest.newRecord(); //sets new record values automatically?
var newPrId = newRequest.number.toString();
//set new record field values below
newRequest.opened_at = new GlideDateTime().getDisplayValue();
newRequest.opened_by = gs.getUserID();
newRequest.sys_created_on = new GlideDate().getDisplayValue();
newRequest.sys_created_by = current.sys_created_by;
newRequest.copied_request = rec.number;
newRequest.copied_progress = 100;
//loop through original request, copy values to empty fields in new request
for (var item in prFields) {
if (newRequest[item] == '' | rec[item] != '') {
newRequest[item] = rec[item];
}
}
//Add the new record to the table
newRequest.insert();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2024 06:43 PM
I noticed the following in your code.
1. copyRequest() cannot recognize the object "rec" because it was not initialized within the function. The same is true for "pfFields".
2. "item" variable in the loop is not the value but the index of the array. It will return nothing because the newRequest object doesn't have a Key value that matches the given index.
For example: newRequest[item] is newRequest[0], not newRequest['short_description'].
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2024 05:42 AM
Updating with a *mostly* working code snippet; this one creates the new record, then builds an array of column names using an encodedquery, then uses forEach to go through each value in the array to assign the original record values to the new record's corresponding columns. I chose this approach to preserve current fields (in case any columns get added or deleted in future versions of the app). Next I must copy attachments, and then copy related records. Hope this helps someone.
Kind Regards,
Woody
copyRequest();
// https://servicenowguru.com/ui-actions-system-ui_copy-ui-action-change-requests-part-2/
// https://servicenowguru.com/ui-actions-system-ui_copy-ui-action-change-requests/
function copyRequest() {
var prID = current.sys_id.toString();
//Initialize new record for insertion
var newRequest = new GlideRecord('x_197547_ocio_purc_x_197547_task_ocio_purchase_requests');
newRequest.newRecord();
//set new record field values below
newRequest.opened_at = new GlideDateTime().getDisplayValue();
newRequest.opened_by = gs.getUserID();
newRequest.sys_created_on = new GlideDate().getDisplayValue();
newRequest.sys_created_by = current.sys_created_by;
newRequest.copied_request = prID;
newRequest.copied_progress = 100;
//Add the new record to the table
newRequest.insert();
var newPrId = newRequest.sys_id.toString();
//gs.info("newPrId variable is " + newPrId); //working
//build array of fields from sys_dictionary using encodedquery
var columns = new GlideRecord('sys_dictionary');
columns.addEncodedQuery('name=x_197547_ocio_purc_x_197547_task_ocio_purchase_requests^ORname=task^elementNOT LIKEapproval^elementNOT LIKEduration^elementNOT LIKEtime^elementNOT LIKEcount^elementNOT LIKE_state^elementNOT LIKE_stage^elementNOT LIKEnumber^elementNOT LIKEcancel^elementNOT LIKEactive^elementNOT LIKEclose^elementNOT LIKEreject^elementNOT LIKEcopied^elementNOT LIKEopened^elementNOT LIKEsys_');
columns.query();
var prFields = [];
while (columns.next()) {
prFields.push(columns.element.toString()); //working
}
gs.info("prFields array contains these values: " + prFields); //working
gs.info("prfields length is " + prFields.length); // working, 195
//assign interesting requests, loop through columns
var origReq = new GlideRecord('x_197547_ocio_purc_x_197547_task_ocio_purchase_requests');
origReq.get(prID);
var newReq = new GlideRecord('x_197547_ocio_purc_x_197547_task_ocio_purchase_requests');
newReq.get(newPrId);
gs.info("the origReq number is " + origReq.number);
gs.info("the newReq number is " + newReq.number);
gs.info("Beginning for-item loop.");
prFields.forEach(myFunction);
function myFunction(value) {
//if (newReq.getValue(value) == '') {
newReq.setValue(value, origReq.getValue(value)); //works but not consistently with the logic condition
//gs.info("origReq.getValue(value) is " + origReq.getValue(value));
//gs.info("value in prFields is " + value);
//}
}
newReq.update();
gs.addInfoMessage('Purchase Request ' + newReq.number + ' created.');
action.setRedirectURL(newReq);
}