Copying a Request

bburdick
Mega Guru

I am trying to copy a request record and its related request item as a new request and new related item, keeping all the variables and options from the earlier request only changing the requested for for the new person.

The problem I am running into is creating a unique request number. I tried to follow the code from SNC Guru where he details out how to copy a change request, Copy Change Part 2, but that is not working. When the script does execute, it keeps copying over the same request. I have isolated it down that it is not creating a unique request number.

Here is my code:



/* current is the sc_req_item record I am on. This script runs in the sc_req_item's workflow */

function copyREQ(termClientArray){
//GET USER INFORMATION
var reqFor = new GlideRecord('sys_user');
reqFor.get(termClientArray);

//PREPARE TO COPY THE REQUEST
var parentRequest = new GlideRecord('sc_request');
parentRequest.addQuery('sys_id', current.request);
parentRequest.query();
if(parentRequest.next()){

//COPY PARENT REQUEST TO CHILD REQUEST
var newRequest = parentRequest;

//GET A NEW REQUEST NUMBER
/* I have tried both this method which uses packages... */
newRequest.number = Packages.com.glide.db.NumberManager.getNumber('sc_request');

/* and this method, which is a business rule. */
//newRequest.number = parentRequest.getNextObjNumberPadded()

newRequest.requested_for = reqFor;
newRequest.requested_by = current.opened_by;
parentRequest.insert();
}
}


Any suggestions?

10 REPLIES 10

cwhaley
Kilo Expert

What version of ServiceNow are you on? There should already be an out-of-box "Copy" UI action on the sc_request table that does this for you.

https://demo02.service-now.com/nav_to.do?uri=sys_ui_action.do?sys_id=923920ccc0a8010a0013cceac7f07994


bburdick
Mega Guru

That would be perfect, but what if I want to modify who the Requested For is? That is the only field I need to modify. Any suggestions on how to do that?

The Copy UI Action code is the following:



gs.print("START");
var helper = new Packages.com.glideapp.servicecatalog.ScriptHelper();
helper.copyRequest(current);
gs.print("END");


Thanks for the suggestion. I think this has some potential. All I want to do is copy the request but change the requested for.


cwhaley
Kilo Expert

Option 1 will do what you are looking for but it won't redirect the screen to the new record:
1. Create your own version fo the Copy UI Action on the sc_request table.
2. Update the object that points to the current record and then call the helper. Note, this doesn't actually update the current record, just the object.
3. Here is the code:



gs.print("START");
current.requested_for = gs.getUserID(); //<-------this will set the requeste for to the logged in user
var helper = new Packages.com.glideapp.servicecatalog.ScriptHelper();
var returned = helper.copyRequest(current);
gs.print("END");


Option 2 gives you much more control and lets you do a lot more than just change requested for and redirect:
1. Create your new UI Action
2. Use this code for the Script section


doCopy();
function doCopy() {
action.setReturnURL(current);
var oldNumber = current.number.toString();
var oldSysId = current.sys_id.toString();
current.newRecord();
current.requested_for = gs.getUserID(); //<-------this will set the requeste for to the logged in user
var newSysId = current.insert();

var items = new GlideRecord('sc_req_item');
items.addQuery('request', oldSysId);
items.query();
while(items.next()) {
_copyItem(oldSysId, newSysId, items);
}

action.setRedirectURL(current);
gs.addInfoMessage(current.number + " was copied from " + oldNumber + ", along with any requested items.");
}

function _copyItem(oldSysId, newSysId, item) {
var newItem = new GlideRecord('sc_req_item');
newItem.initialize();
var num = newItem.number;
newItem = item;
newItem.number = num;
newItem.request = newSysId;
var newItemSys = newItem.insert();
}



bburdick
Mega Guru

Option 2 didn't work for me because it didn't launch the associated workflows.

Option 1 is workable but I need to have the sys_id of the new request. Is there a way to call that back?

Thanks for the pointers.