save xml as attachment ?

Kumar35
Tera Expert

hi there!
any pointers to store the xml of current record as a related attachment record in attachments table ?

cheers!

5 REPLIES 5

subash_biswas
Kilo Expert

Option 1: External Tool/Code

https://wiki.servicenow.com/index.php?title=Exporting_Data#Calling_URL_Exports_Programmatically
1.Call the unload URL from an external source
https://instanceName.service-now.com/incident.do?XML&sys_id=ed391d31293201002e93940592a4052e&useUnloadFormat=true
2.Write to a file location and upload file back to a destination of choice
3.This option may be slower because of disk IO involved.

Option 2: Use ECC Queue
1.Generate XML manually for the table/record you are sitting on; I am sure there are better ways to export the current record into XML
2.base64Encode your xml string before you write to the ecc queue payload
3.The sample business rule below will send the xml file to an Incident record specified.

attachXML();

function attachXML() {
//create the xml unload of the record manually 😞
var xmlString = 'xmlFileHere';
var StringUtil = Packages.com.glide.util.StringUtil;
var gr = new GlideRecord('ecc_queue');
gr.initialize();
//OOB attaches the payload in base64 to the table defined in source field below
gr.agent = 'AttachmentCreator';
gr.topic = 'AttachmentCreator';
gr.name = current.number + '.xml:text/xml';
//update table name & sys_id as needed
gr.source = 'incident:ed137f4e45ae45009673bd09e0974961';
gr.queue = 'input';
gr.state = 'ready';
gr.payload = StringUtil.base64Encode(xmlString);
gr.insert();
}

4.HTH
Subash Biswas


Thanks for the suggestion. currently I am working on a alternate solution to my problem which avoids xml attachment.
I will try this later, thanks again.


instead of having "var xmlString = 'xmlFileHere';" how can I put the entire XML of the CI that I want to export?

adiddigi
Tera Guru

There is actually a very simple way, Here is the way you can do it:




var xml = '<xml>'+
'<number>'+current.number+'</number>'+
'<state>'+current.state+'</state>'+
'</xml>';
var attachment = new Attachment();
var attachmentRec = attachment.write('incident', current.sys_id, "record.xml", "text/xml", xml);
gs.addInfoMessage('An attachment is created with sys_id and attached to this incident'+ attachmentRec);



This is the idea.