Copy attachments from Interaction to Request on click of Create Request button

rishabh31
Mega Sage

Dear Team,

 

I have a requirement to copy/transfer attachments of Interaction Record to Request record when Create Request UI Action button clicked on associated Interaction Record.

 

It is working fine with Incident record due to the line 28 and 29 on the below Create Incident UI Action script

rishabh31_0-1714377982925.png

as it refers to below script include 

rishabh31_1-1714378036810.png

I have tried the below solution provided in community for same issue but not working properly

https://www.servicenow.com/community/itsm-forum/how-to-transfer-attached-files-from-interaction-to-r...

 

Currently I have the custom UI Action for Create Request button on Interaction table, which redirects directly to REQ record for a particular catalog item, it is woking fine but I stuck here on click of Create request button it should copy the attachmnets of Interaction to Request created, same as it is working for Incident created.

 

Below is my UI Action 'Create Request' script

 

createServiceRequest();
createM2M();

function createServiceRequest() {
    var cart = new Cart();
    var item = cart.addItem('d97d2beb97ae3110e6e3bb5ef053af85'); // sys_id of General request catalog item
    cart.setVariable(item, 'description', current.short_description);
    cart.setVariable(item, 'requested_for', current.opened_for);
    var cartGR = cart.getCart();
    cartGR.requested_for = current.opened_for;
    cartGR.update();
    var newSerReq = cart.placeOrder();
    newSerReq.opened_by = current.opened_for;
    newSerReq.update();
    current.parent = getRITM(newSerReq.sys_id);
    current.update();
    var disMessage = 'Interaction ' + current.number + ' has been converted into Service Request ' + newSerReq.number;
    gs.addInfoMessage(disMessage);
    action.setRedirectURL(newSerReq);
    action.setReturnURL(current);
}

function getRITM(serReq) {
    var ritm = '';
    var grRITM = new GlideRecord('sc_req_item');
    grRITM.addQuery('request', serReq);
    grRITM.query();
    if (grRITM.next()) {
        ritm = grRITM.sys_id;
    }
    return ritm;
}

function createM2M(grRITM) {
    var gr = new GlideRecord('interaction_related_record');
    gr.newRecord();
    gr.document_table = 'sc_req_item';
    gr.interaction = current.sys_id;
    gr.document_id = grRITM.sys_id();
    //gr.task = grRITM.sys_id();
    gr.type = 'task';
    gr.insert();

	GlideSysAttachment.copy('interaction', current.interaction, 'sc_req_item', ritm.sys_id);
	//'interaction', current.interaction, 'sc_req_item', ritm.sys_id
}
/*When Create Request button clicked from respective IMS record, then State of IMS sets to 'Closed Complete' and 
worknotes populated as 'Request created'*/
current.state = "closed_complete"; // add closed state value
current.work_notes = "Request created";
current.update();

 

I have tried almost all similar community posts to get this resolved but no luck.

 

Please help!

 

Thanks

 

  

1 ACCEPTED SOLUTION

Ramz
Mega Sage

Hi @rishabh31 

Your function createM2M(grRITM) is not configured properly . Also the calling of interaction record and ritm is wrong.Please paste this script and let me know if its working. To be sure please delete all records created in the 

'interaction_related_record' table.

 

createServiceRequest();


function createServiceRequest() {
    var cart = new Cart();
    var item = cart.addItem('d6c2273c47010200e90d87e8dee49004'); // sys_id of General request catalog item
    cart.setVariable(item, 'description', current.short_description);
    cart.setVariable(item, 'requested_for', current.opened_for);
    var cartGR = cart.getCart();
    cartGR.requested_for = current.opened_for;
    cartGR.update();
    var newSerReq = cart.placeOrder();
    newSerReq.opened_by = current.opened_for;
    newSerReq.update();
    current.parent = getRITM(newSerReq.sys_id);
    current.update();
    var disMessage = 'Interaction ' + current.number + ' has been converted into Service Request ' + newSerReq.number;
    gs.addInfoMessage(disMessage);
    action.setRedirectURL(newSerReq);
    action.setReturnURL(current);
	
}

function getRITM(serReq) {
    var ritm = '';
    var grRITM = new GlideRecord('sc_req_item');
    grRITM.addQuery('request', serReq);
    grRITM.query();
    if (grRITM.next()) {
        ritm = grRITM.sys_id;
	createM2M(grRITM.sys_id);//call the function here so you get the ritm number correctly
    }
    return ritm;
	
}

function createM2M(grRITM) {
    var gr = new GlideRecord('interaction_related_record');
    gr.newRecord();
    gr.document_table = 'sc_req_item';
    gr.interaction = current.sys_id;
    gr.document_id = grRITM;
    //gr.task = grRITM.sys_id();
    gr.type = 'task';
    gr.insert();
	

	GlideSysAttachment.copy('interaction', gr.interaction, 'sc_req_item', grRITM);
}
/*When Create Request button clicked from respective IMS record, then State of IMS sets to 'Closed Complete' and 
worknotes populated as 'Request created'*/
current.state = "closed_complete"; // add closed state value
current.work_notes = "Request created";
current.update();

 

Please mark my answer correct/helpful if it resolved your query.Thanks

View solution in original post

10 REPLIES 10

rishabh31
Mega Sage

Can anyone please help on this!

OlaN
Giga Sage
Giga Sage

Hi,

Do you know there is an OOB ui action to use for this, "Create request" that navigates the user (fulfiller) to the service catalog, and from there can select the correct item to submit.

Only thing is that it doesn't copy attachments from the Interaction, because at that point it doesn't know if the selected Catalog item allows attachments.

 

SOW-create-request.png

Thanks @OlaN Sir for your response, but this is not my requirement, here in my case there is a fixed catalog item directly opening its REQ record which clicks on Create Request Button.

 

Please help to achieve the actual requirement

Ramz
Mega Sage

Hi @rishabh31 

Your function createM2M(grRITM) is not configured properly . Also the calling of interaction record and ritm is wrong.Please paste this script and let me know if its working. To be sure please delete all records created in the 

'interaction_related_record' table.

 

createServiceRequest();


function createServiceRequest() {
    var cart = new Cart();
    var item = cart.addItem('d6c2273c47010200e90d87e8dee49004'); // sys_id of General request catalog item
    cart.setVariable(item, 'description', current.short_description);
    cart.setVariable(item, 'requested_for', current.opened_for);
    var cartGR = cart.getCart();
    cartGR.requested_for = current.opened_for;
    cartGR.update();
    var newSerReq = cart.placeOrder();
    newSerReq.opened_by = current.opened_for;
    newSerReq.update();
    current.parent = getRITM(newSerReq.sys_id);
    current.update();
    var disMessage = 'Interaction ' + current.number + ' has been converted into Service Request ' + newSerReq.number;
    gs.addInfoMessage(disMessage);
    action.setRedirectURL(newSerReq);
    action.setReturnURL(current);
	
}

function getRITM(serReq) {
    var ritm = '';
    var grRITM = new GlideRecord('sc_req_item');
    grRITM.addQuery('request', serReq);
    grRITM.query();
    if (grRITM.next()) {
        ritm = grRITM.sys_id;
	createM2M(grRITM.sys_id);//call the function here so you get the ritm number correctly
    }
    return ritm;
	
}

function createM2M(grRITM) {
    var gr = new GlideRecord('interaction_related_record');
    gr.newRecord();
    gr.document_table = 'sc_req_item';
    gr.interaction = current.sys_id;
    gr.document_id = grRITM;
    //gr.task = grRITM.sys_id();
    gr.type = 'task';
    gr.insert();
	

	GlideSysAttachment.copy('interaction', gr.interaction, 'sc_req_item', grRITM);
}
/*When Create Request button clicked from respective IMS record, then State of IMS sets to 'Closed Complete' and 
worknotes populated as 'Request created'*/
current.state = "closed_complete"; // add closed state value
current.work_notes = "Request created";
current.update();

 

Please mark my answer correct/helpful if it resolved your query.Thanks

Ramz
Mega Sage

Hi @rishabh31 ,

Did my solution work for you.I tried in my PDI and it worked properly.Let me know if you have any questions

Please mark my answer correct/helpful if it resolved your query.Thanks