"for in loop" to copy specific field's values using array of elements UI Action: copy

woodyfairley
Tera Guru

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();

 

1 ACCEPTED SOLUTION

Lhora Alvarez
Tera Sage

@woodyfairley 

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'].

View solution in original post

5 REPLIES 5

Lhora Alvarez
Tera Sage

@woodyfairley 

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'].

Your guidance on the code has been very helpful, thank you. Updated code snippet below works, mostly.

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();

    //Copy attachments for this change
    if (typeof GlideSysAttachment != 'undefined')
        GlideSysAttachment.copy('x_197547_ocio_purc_x_197547_task_ocio_purchase_requests', prID, 'x_197547_ocio_purc_x_197547_task_ocio_purchase_requests', newRequest.sys_id);
    else
        Packages.com.glide.ui.SysAttachment.copy('x_197547_ocio_purc_x_197547_task_ocio_purchase_requests', prID, 'x_197547_ocio_purc_x_197547_task_ocio_purchase_requests', newRequest.sys_id);

    //Copy associated orders and CIs
    //copy associated order items and accounting codes
    //copyOrder(prID);
    //copyAcctng(prID);

    gs.addInfoMessage('Purchase Request ' + newReq.number + ' created.');
    action.setRedirectURL(newReq);
}

 

Maik Skoddow
Tera Patron
Tera Patron

Hi
have not the time for a complete solution but converting to JSON is possible via OOTB functions with ease:

MaikSkoddow_0-1715565684727.png

 

Thank you for your guidance, but JSON did not facilitate copying the values. I added working code to a comment, hope this helps someone in the future, and I hope you will keep contributing.

Kind Regards,

Woody