GlideSysAttachment.copy doesn't seem to be working in my script, any pointers?

Tate Roth
Tera Contributor

I created a workflow script to create a record in a scoped application from a sc_request that is working great.  The problem I am having is I'd like to copy any attachments that are added to the Request onto the new record.  I tried to use the GlideSysAttachment.copy function but I must be doing something wrong.  See the script below.  Any suggestions on what I am doing wrong?

Thanks in advance.

var gr = new GlideRecord('x_smcit_siq_pet_table');
gr.initialize();
if (current.variables.customer_name == '') {
	var name = new GlideRecord('x_smcit_siq_pet_siq_customers');
	name.addQuery('name', current.variables.first_name + ' ' + current.variables.last_name);
	name.query();
	while (name.next()) {
		gr.setValue('customer', name.sys_id);
	}
	gr.setValue('customer_email', current.variables.email_address);
	gr.setValue('customer_number', current.variables.customer_id);
	gr.setValue('sleeper_id', current.variables.sleeper_id);
	}
else {
	gr.setValue('customer', current.variables.customer_name);
	var name = new GlideRecord('x_smcit_siq_pet_siq_customers');
	name.addQuery('sys_id', current.variables.customer_name);
	name.query();
	while (name.next()) {
		gr.setValue('customer_email', name.getValue('email'));
		gr.setValue('customer_number', name.getValue('customer_id'));
		gr.setValue('sleeper_id', name.getValue('sleeper_id'));
	}
}

gr.setValue('description', current.variables.description);
gr.setValue('issue_type', current.variables.issue_type.getDisplayValue());
gr.setValue('issue_sub_type', current.variables.issue_subtype);
gr.setValue('current_mac_address', current.variables.current_mac_address);
gr.setValue('new_mac_address', current.variables.new_mac_address);
gr.setValue('type_of_router', current.variables.router_type);
gr.setValue('number_of_routers', current.variables.number_of_routers);
gr.setValue('pump_lights', current.variables.pump_lights);
gr.setValue('pump_on_for_investigation', current.variables.pump_on);
gr.setValue('firewall', current.variables.firewall);
gr.setValue('cascaded_network', current.variables.cascaded_network);
gr.setValue('order_number', current.variables.order_number);
gr.setValue('components_for_product_name', current.variables.components_for_product_name);
gr.setValue('line_not_added_to_sleepiq', current.variables.line_not_added_to_sleepiq);
gr.setValue('component_to_be_added_to_siq', current.variables.component_to_be_added_to_siq);
gr.setValue('customer_number_mismatch', current.variables.customer_number_mismatch);
gr.setValue('use_siq_customer_number', current.variables.use_siq_customer_number);
gr.setValue('component_out_of_bom', current.variables.component_out_of_bom);
gr.setValue('missing_component', current.variables.missing_component);
gr.setValue('line_number_of_component', current.variables.line_number_of_component);

if (current.variables.description.length > 80) {
	gr.setValue('short_description', current.variables.description.substring(0, 79));
}
else {
	gr.setValue('short_description', current.variables.description);
}

var sysID = gr.insert();

GlideSysAttachment.copy('sc_req_item', current.sys_id, 'x_smcit_siq_pet_table', sysID);
1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Where is this code within the workflow?  I ask because it is important to understand the execution order of the catalog and I suspect maybe the requested item may not be inserted yet thus the attachments aren't available to copy from.  If this script is at the beginning of the workflow the sc_req_item hasn't been inserted yet and nor has the sc_request.  Once the workflow reaches a stopping point like waiting for an approval or wait for condition or timer then the sc_req_item and then sc_request is inserted.  By putting in a short timer, you are giving the records a chance to insert.

View solution in original post

14 REPLIES 14

Awesome glad that was it.  I have been burnt by this many times.

You are the best! Thanks Grandmaster!!!

That was the reason I asked Tate to add gr.update() at the end. But Glad that the issue is resolved.

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

gr.update() would not solve it because gr is the x_smcit_siq_pet_table record and it is already inserted.  The issue was that the current record (sc_req_item) hasn't been inserted at the time of the copy.  There are no attachments to copy as a result.

Dawid2
Giga Guru

I had similar issue but with attachments from email being sent to instance. What I found out that following is working for me.

GlideSysAttachment.copy('sc_req_item', sc_req_item.sys_id, 'x_smcit_siq_pet_table', x_smcit_siq_pet_table.sys_id);