How to transfer attachment from custom table to Service Request

mp_10
Tera Contributor

Hi,

Please help!! I am facing some challenges while attempting to create a service request from a custom table.

 

1. I have created a business rule to submit the SR from a custom table - "stock_request," but I am unsure how to transfer the attachment of the custom table to the SR. I need it either to the variable "attachment" of the catalog item or to the header attachment of the service request. All other details are functioning correctly.

 

2)I have created a reference field in the "sc_task" table (which refers to the "stock_request" table) and is only visible to tasks created for stock requests. This reference field must be filled with the Stock Request number either from the variable- stock number or from any other options that is possible(not sure on what to do :)). I have attempted to use the advanced script from the catalog task of the workflow, but have been unsuccessful.

 

below is the code of condition 1

(function executeRule(current, previous /*null when async*/) {

createServiceRequest();
function createServiceRequest() {
var cart = new Cart();
var item = cart.addItem('5afd91d19791251004d4fef3a253afe9');//sys id of catalog item
cart.setVariable(item, 'requested_for', current.u_requestor);
cart.setVariable(item, 'submitted_by', gs.getUserID());
cart.setVariable(item, 'inventory_request', current.u_number);
cart.setVariable(item, 'quantity', current.u_total_stock_quantity);
cart.setVariable(item, 'additional_information', current.u_additional_details);
//GlideSysAttachment.copy('u_stock_request',current.sys_id,'sc_req_item',current.variables.attach_details);
//cart.setVariable(item, 'login', current.user_name);
var cartGR = cart.getCart();
//cartGR.requested_for = current.sys_id;
cartGR.requested_for = current.u_requestor;
cartGR.update();
var newSerReq = cart.placeOrder();
newSerReq.update();
var disMessage = 'Created request: ' + newSerReq.number;
gs.addInfoMessage(disMessage);
action.setRedirectURL(newSerReq);
//current.u_state = 'created_sr';
current.setDisplayValue('u_related_sr',newSerReq.number.toString());
current.setValue('u_additional_details',disMessage);
current.update();
action.setReturnURL(current);
}

})(current, previous);

 

 

Could someone please provide guidance on how to resolve these issues?

 

 

4 REPLIES 4

KB18
Tera Guru
Tera Guru

@mp_10Please refer the below article to transfer file from one table to another

Understanding Attachments in ServiceNow - ServiceNow Developer Pro-Tips (snprotips.com)

 

Thanks,

Kailas Bandiwar

Please hit the thumb Icon and mark as correct in case I help you with your query!!!
- Kailas

1. Better you can use attachment API and give the reference of your custom table and copied into required table.

2. use this code to copy attachment 

copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName);

//donorTable: The table to which the attachment is currently associated //donorID: The sys_id of the record to which the attachment is currently associated //recipientTable: The table to which the new/copied attachment should be associated //recipientID: The sys_id of the record to which the new/copied attachment should be associated //fileName: The name of the attachment file (this will remain the same after copying)

 

function copySpecificAttachment(donorTable, donorID, recipientTable, recipientID, fileName) {
var donorAttSysID;
var newAttRecord;
var linkToNewRecord;
var attDataRecord;
var newDocRecord;
var attRecord = new GlideRecord('sys_attachment');
attRecord.addQuery('table_name', donorTable);
attRecord.addQuery('table_sys_id', donorID);
attRecord.addQuery('file_name', fileName);
attRecord.query();
while (attRecord.next()) {
donorAttSysID = attRecord.getValue('sys_id');
newAttRecord = copyRecord(attRecord);
newAttRecord.setValue('table_name', recipientTable);
newAttRecord.setValue('table_sys_id', recipientID);
newAttRecord.update();
linkToNewRecord = gs.getProperty('glide.servlet.uri') + newAttRecord.getLink();
attDataRecord = new GlideRecord('sys_attachment_doc');
attDataRecord.addQuery('sys_attachment', donorAttSysID);
attDataRecord.query();
while (attDataRecord.next()) {
newDocRecord = copyRecord(attDataRecord);
newDocRecord.setValue('sys_attachment', newAttRecord.getValue('sys_id'));
newDocRecord.update();
}
}
}

Please hit the thumb Icon and mark as correct in case I help you with your query!!!
- Kailas

mp_10
Tera Contributor

Hi Kailas,

 

Thank you for the response. I will try to develop my requirements from the details you have shared.

 

Regards,

Shilpa

KB18
Tera Guru
Tera Guru

Hi Shilpa,

Please let me know in case you are facing any issues.

 

 

Please hit the thumb Icon and mark as correct in case I help you with your query!!!
- Kailas