Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy attachments from request item to change

tkh
Kilo Expert

I am fairly new to Service Now so please have patience with me.  

I have created a Service Catalog Request for a firewall request that once you click Order Now button creates a Request Item.   Currently all the variables and attachments copy over from the Req to the Request Item without issue.

Next a script runs to create a Change and all the variables are copied over, again without issue, except the attachments don't copy over.   I have tried various solutions from the community and have yet to get it work successfully.   I feel I am either missing steps everyone else just assumes is known, or that I am not quite getting all the vocabulary yet.

Since I am not really sure the proper path to take to get the attachments to copy over, I am including my script without any attachment parts in hopes someone can direct me to the best method.   Please keep in mind I am a newb and may need more detailed instructions.

CODE:

var affectedCIs = (current.variables.affected_cis + '').split(',');

var gr = new GlideRecord('change_request');

gr.initialize();

gr.requested_by = current.request.requested_for;

gr.short_description = current.variables.short_description + '';

gr.u_subscribed_offering = current.variables.site_id;

gr.description ='Source IP/Subnet - ' + current.variables.Source_IP + '\n' + 'Destination IP/Subnet - ' + current.variables.Destination_IP + '\n' + 'Port(s) - ' + current.variables.Firewall_Port + '\n' + 'Protocol - ' + current.variables.Firewall_Protocol + '\n' + 'Bi-Directional? - ' + current.variables.Bi_Directional + '\n' + '\n' +

'Originating Request: ' + current.request.number + '\n' +

'Originating Sys_ID: ' + current.request.sys_id +'\n' + '\n' +

current.variables.description + '';

gr.cmdb_ci = affectedCIs[0] + '';

gr.end_date = current.variables.planned_end;

gr.company = current.variables.Company;

gr.insert();

for (var i = 0; i < affectedCIs.length; i++)

relateAffectedCI(gr.sys_id + '', affectedCIs[i] + '');

current.comments = 'Change request ' + gr.getDisplayValue() + ' has been created'+ '\n' +

current.variables.change_request = gr.sys_id + '';

function relateAffectedCI(task, item) {

var ci = new GlideRecord('task_ci');

ci.addQuery('ci_item', item);

ci.addQuery('task', task);

ci.query();

if (!ci.hasNext()) {

ci.initialize();

ci.ci_item = item;

ci.task = task;

ci.insert();

}

}

Your help is appreciated!

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Trudy,



Include below line after gr.insert(); and replace gr.insert(); with var chgsysId = gr.insert();


GlideSysAttachment.copy('sc_req_item', current.sys_id, 'change_request', chgsysId);



http://wiki.servicenow.com/index.php?title=Copy_Attachments_from_Record_to_Record#gsc.tab=0


View solution in original post

4 REPLIES 4

ark6
Mega Guru

Check the below post it will help.



In your code, i cant see the lines to add attachments.



Showing Requested Item Attachments on the Catalog Task Form


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Trudy,



Include below line after gr.insert(); and replace gr.insert(); with var chgsysId = gr.insert();


GlideSysAttachment.copy('sc_req_item', current.sys_id, 'change_request', chgsysId);



http://wiki.servicenow.com/index.php?title=Copy_Attachments_from_Record_to_Record#gsc.tab=0


OMG that worked like a charm!!!!!!!!!!!!!!!!!!!!1     Thank you , thank you, thank you!!!!!!!!!



I was real close in what I was trying before but in the whole wrong part of the script.   Now that I can see it working I get a better understanding of the logic and what I was missing.   I learned a lot in addition to solving my issue.  



Thank you so much!


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

You are very welcome Tudy. Thanks for participating in the community!