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

You could always start the workflow yourself:
http://wiki.service-now.com/index.php?title=Workflow_Script#startFlow.28workflowId.2C_current.2C_operation.2C_vars.29

3.2 startFlow(workflowId, current, operation, vars)
Starts a specified workflow. See script include WorkflowScheduler and Business Rule "Start Workflow" on table sc_req_item for examples of use.

3.2.1 Input Fields
Parameters:
workflowId - The sys_id of the workflow to start. This sys_id refers to table wf_workflow.
current - GlideRecord of the record to use as current in this workflow. This is normally from the "Table" field of the workflow properties for this workflow.
operation - The operation being performed on current. Possible values: "insert", "update", "delete".
vars - Collection of variables to add to the workflow.


bburdick
Mega Guru

Thanks for the advice, Chad. I ended up using a cart call. Here is a sample of my code and this worked:



function copyREQ(termClientArray){

var parentRequest = new GlideRecord('sc_request');
parentRequest.addQuery('sys_id', current.request);
parentRequest.query();
if(parentRequest.next()){

var cart = new Cart();
cart.cart.requested_for = termClientArray;
//Identify this is copied from the parent
cart.cart.special_instructions = 'PARENT:' + parentRequest.sys_id;
cart.cart.update();

var exReqItem;
var newReqItem;
// Duplicate all of the non-New User Requested Items within this Request
exReqItem = new GlideRecord('sc_req_item');
exReqItem.get(current.sys_id);

newReqItem = cart.addItem(exReqItem.cat_item, 1);
for (var varName in exReqItem.variables) {
cart.setVariable(newReqItem, varName, exReqItem.variables[varName]+'');
}
cart.placeOrder();
}
}


hartr
Giga Contributor

I've tried using the cat.setVariable method described by bburdck .. but am running into issues with the order of the variables on the new request item.
It doesn't seem to copy the sc_item_option.order value from the old variables so the variables are all misplaced on the new item.

Has anyone else experienced this ? / know how to fix ?

thanks


ruzbehv
Mega Guru

Hi bb,



This might be a couple years to late but SN has OOB functionality called 'Bulk Request' which does exactly what you need.   Have you tried that?



- RV


Last time I looked at Bulk Request it only did it for 10 additional users.   http://wiki.servicenow.com/index.php?title=Enabling_Bulk_Requests



We needed the ability to do up to a 100.