Copying attachments from one sc_task to another sc_task

Chad Wilhelm1
Tera Expert

Hello,

We have a business requirement to copy attachments from one sc_task to another sc_task.   We currently have a catalog item that creates a sc_task for a request for quote then once the sc_task is closed complete the UI Action Purchase is available which then creates another request copying the variables from the first request.   I tried to add the copy attachment code below in yellow.   Any assistance would be appreciated

UI Action

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('de44ce8013d41bc0fa7c73076144b09d'); //GTO Procurement - Quote/Purchase/Return Request

//iterate through all properties of current.request_item.variables set them as the values for the new request
for (var property in current.variables){
if (property == 'request_type')
cart.setVariable(item, property, "Purchase");
else
cart.setVariable(item, property, current.variables[property]);
}

var rc = cart.placeOrder();
gs.addInfoMessage("Purchase request has been submitted. Request number is " + rc.number);
GlideSysAttachment.copy('sc_task',current.getUniqueValue(),'sc_task',rc.sys_id);
rc.update();
action.setRedirectURL(current);

1 ACCEPTED SOLUTION

Chad Wilhelm1
Tera Expert

I was able to go it to work.

var newSerReqTask = new GlideRecord('sc_task');
var taskQueryString = "request=" + rc.sys_id;
newSerReqTask.addEncodedQuery(taskQueryString);
newSerReqTask.query();

//While there is a record found from the above query, add all attachments from the Quote Catalog Task to the new Purchase Catalog Task
while (newSerReqTask.next()){
var gsAttach = new GlideSysAttachment().copy('sc_task', current.sys_id, 'sc_task', newSerReqTask.sys_id);
}

View solution in original post

12 REPLIES 12

Michael Fry1
Kilo Patron

Hello Michael,

I saw this but it uses the parent field off task which is the RITM number, in this case since we are generating a new request the RITM will be a different number.

Thanks,

Chad

dvp
Mega Sage
Mega Sage

rc.sys_id will return you request sys_id, not the catalog task 

Shweta KHAJAPUR
Tera Guru

Please refer below thread as  cart.placeOrder();  returns request object not sc object

https://community.servicenow.com/community?id=community_question&sys_id=c6b28361dbd8dbc01dcaf3231f96...