Attach an attachment to a SCTASK using a script

Tony103
Tera Contributor

I am creating a Scheduled Job using a custom script and want to attach a static document every time a ticket is created.  The catalog item workflow creates the REQ, RITM, and SCTASK and is working as desired.  However, I cannot seem to get the attachment (attached to scheduled job) to be added to the ticket.  Preferably to the SCTASK, but REQ would be acceptable.  I have the sys_id of the attachment (0982d13f1ba62510062e0d0fdc4bcba8).  Please reply as if I know nothing as I struggle with scripting.

1 ACCEPTED SOLUTION

@Tony103 Use the following script in your script job (where the attachment is present) as it is and do not make any changes to it. 

 

createServiceRequest();
function createServiceRequest() {
var cart = new Cart();
var item = cart.addItem('7b42df94db27e010ef5fd426ca9619f8');// IT Service Request
cart.setVariable(item, 'short_description', 'Perform Windows Updates on 3 Matrix PCs - Zanesville');
cart.setVariable(item, 'description', 'Main hospital --------------------  GHGSOFG161D01        ( 10.32.101.92 ) \r Mab 1 (security office)  --------  MAB1ST0202D03          ( 10.17.40.39 )');
cart.setVariable(item, 'assignment_group', '0f2157691b2b585000a9ea4cbc4bcb7d'); //PC Analyst
var cartGR = cart.getCart();
cartGR.update();
var newSerReq = cart.placeOrder();
newSerReq.opened_by = current.caller_id;
newSerReq.update();

var attachment = new GlideSysAttachment();
var glideRITM = new GlideRecord('sc_req_item');
glideRITM.addQuery('request', newSerReq.getValue('sys_id'));
glideRITM.query();
while (glideRITM.next()) {
    var glideSCTask = new GlideRecord('sc_task');
    glideSCTask.addQuery('request_item', glideRITM.getValue('sys_id'));
    glideSCTask.query();
    while (glideSCTask.next()) {
        //Copy attachment here
        var attachmentSysID = attachment.copy('sysauto_script', current.getValue('sys_id'), 'sc_task', glideSCTask.getValue('sys_id'));
        gs.info('Attachment with sys_id ' + attachmentSysID + ' to SCTask ' + glideSCTask.number);
    }
}
}

 

This script will copy the attachment from the scheduled job to the SC Task of RITM.

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Tony103 Here is my solution. You can use GlideSysAttachment API to copy attachments from the scheduled job to SC Task.

 

Screenshot 2023-05-09 at 12.21.51 AM.png

 

Here is how I implemented this method on my scheduled job.

My attachment to be copied is attached on the scheduled job here.

Screenshot 2023-05-09 at 12.19.45 AM.png

 

Here is the script I have written to accomplish this requirement.

var attachment = new GlideSysAttachment();//API for attachment
var scheduledJob = current.getValue('sys_id');//Get the sys_id of current scheduled job
var glideSCTask = new GlideRecord('sc_task');//SC Task
glideSCTask.addQuery('number','SCTASK0010001');
glideSCTask.query();
if(glideSCTask.next()){
	var attachmentSysID = attachment.copy('sysauto_script',scheduledJob,'sc_task',glideSCTask.getValue('sys_id'));
	//Copy(source table, source object sysid,target table, target table sys id );
	gs.info('Attachment with sys_id '+attachmentSysID+' to SCTask '+glideSCTask.number);

}



Here is how the end result looks, attachment is copied from the scheduled job to the SC Task.

Screenshot 2023-05-09 at 12.20.07 AM.png

Hope this helps.

Tony103
Tera Contributor

Added your code to my scheduled job but it is not working.  I obviously have something wrong.  Code provided.

 

createServiceRequest();

var attachment = new GlideSysAttachment();//API for attachment
var scheduledJob = current.getValue('ba001d7b1ba62510062e0d0fdc4bcb4e');//Get the sys_id of current scheduled job
var glideSCTask = new GlideRecord('sc_task');//SC Task
glideSCTask.addQuery('number','SCTASK0010001');
glideSCTask.query();
if(glideSCTask.next())
	var attachmentSysID = attachment.copy('sysauto_script',scheduledJob,'sc_task',glideSCTask.getValue('sys_id'));
	//Copy(source table, source object sysid,target table, target table sys id );
	gs.info('92d1d57b1ba62510062e0d0fdc4bcb3e'+attachmentSysID+' to SCTask '+glideSCTask.number);  //sys_ID of attacment from attachment table
	
function createServiceRequest() {
var cart = new Cart();
var item = cart.addItem('7b42df94db27e010ef5fd426ca9619f8');// IT Service Request
cart.setVariable(item, 'short_description', 'Perform Windows Updates on 3 Matrix PCs - Zanesville');
cart.setVariable(item, 'description', 'Main hospital --------------------  GHGSOFG161D01        ( 10.32.101.92 ) \r Mab 1 (security office)  --------  MAB1ST0202D03          ( 10.17.40.39 )');
cart.setVariable(item, 'assignment_group', '0f2157691b2b585000a9ea4cbc4bcb7d'); //PC Analyst
var cartGR = cart.getCart();
cartGR.update();
var newSerReq = cart.placeOrder();
newSerReq.opened_by = current.caller_id;
newSerReq.update();
}

 

@Tony103 Use the following script in your script job (where the attachment is present) as it is and do not make any changes to it. 

 

createServiceRequest();
function createServiceRequest() {
var cart = new Cart();
var item = cart.addItem('7b42df94db27e010ef5fd426ca9619f8');// IT Service Request
cart.setVariable(item, 'short_description', 'Perform Windows Updates on 3 Matrix PCs - Zanesville');
cart.setVariable(item, 'description', 'Main hospital --------------------  GHGSOFG161D01        ( 10.32.101.92 ) \r Mab 1 (security office)  --------  MAB1ST0202D03          ( 10.17.40.39 )');
cart.setVariable(item, 'assignment_group', '0f2157691b2b585000a9ea4cbc4bcb7d'); //PC Analyst
var cartGR = cart.getCart();
cartGR.update();
var newSerReq = cart.placeOrder();
newSerReq.opened_by = current.caller_id;
newSerReq.update();

var attachment = new GlideSysAttachment();
var glideRITM = new GlideRecord('sc_req_item');
glideRITM.addQuery('request', newSerReq.getValue('sys_id'));
glideRITM.query();
while (glideRITM.next()) {
    var glideSCTask = new GlideRecord('sc_task');
    glideSCTask.addQuery('request_item', glideRITM.getValue('sys_id'));
    glideSCTask.query();
    while (glideSCTask.next()) {
        //Copy attachment here
        var attachmentSysID = attachment.copy('sysauto_script', current.getValue('sys_id'), 'sc_task', glideSCTask.getValue('sys_id'));
        gs.info('Attachment with sys_id ' + attachmentSysID + ' to SCTask ' + glideSCTask.number);
    }
}
}

 

This script will copy the attachment from the scheduled job to the SC Task of RITM.