Skip to main content

Export update set to include its name - UI action

code a new processor:


 script

var sysid = g_request.getParameter('sysparm_sys_id'); var exporter = new ExportWithRelatedLists2('sys_remote_update_set', sysid); exporter.addRelatedList('sys_update_xml', 'remote_update_set'); exporter.exportRecords(g_response); var del = g_request.getParameter('sysparm_delete_when_done'); if (del == "true") { var ugr = new GlideRecord("sys_remote_update_set"); ugr.addQuery("sys_id", sysid); ugr.query(); if (ugr.next()) { ugr.deleteRecord(); } }









code a new UI action on the local update set table:



script:

var sURL = CUSTOMUTIL_ExportUpdate.exportUpdate(current.getUniqueValue()); action.setRedirectURL(sURL);



(alternatively use this script:

var updateSetExport = new UpdateSetExport();

var sysid = updateSetExport.exportUpdateSet(current);


action.setRedirectURL("export_update_set2.do?sysparm_sys_id=" + sysid + "&sysparm_delete_when_done=true");

)

code a script include.


var CUSTOMUTIL_ExportUpdate = Class.create(); CUSTOMUTIL_ExportUpdate.exportUpdate = function(sSysiD){ var grGlideRecord = new GlideRecord("sys_update_set"); if (grGlideRecord.get(sSysiD)){ var updateSetExport = new UpdateSetExport(); var sysid = updateSetExport.exportUpdateSet(grGlideRecord); return("export_update_set2.do?sysparm_sys_id=" + sysid + "&sysparm_delete_when_done=true"); } }; MTHDUTIL_ExportUpdate.prototype = { initialize: function() { }, type: 'MTHDUTIL_ExportUpdate' };

code a script include.



var ExportWithRelatedLists2 = Class.create(); ExportWithRelatedLists2.prototype = { initialize: function(parent_table, sys_id){ this.parent_table = parent_table; this.sys_id = sys_id; this.related_lists = []; this.query_sets = []; }, /** * Add related records * @param String child_table - table to query * @param String reference_field - field to query on */ addRelatedList: function(child_table, reference_field){ var related_list = [child_table, reference_field]; this.related_lists.push(related_list); }, /** * Add a query definition to define a collection of records to export, allows for custom queries that cannot be met with addRelatedList * @param String table * @param String query - encoded query string */ addQuerySet: function(table, query){ var set = [table, query]; this.query_sets.push(set); }, getFileName: function(){ var sResult = this.parent_table; var gr = new GlideRecord(this.parent_table); if (gr.get(this.sys_id)){ sResult = gr.getValue("name"); } return (sResult + '_' + this.sys_id + '.xml'); }, /** * Enable attachment support for all records being exported * @param boolean b */ setAttachments: function(b){ this.includeAttachments = b; }, /** * Required XML Headers */ setHeaders: function(response){ response.setHeader('Pragma', 'public'); response.addHeader('Cache-Control', 'max-age=0'); // setting the content type response.setContentType('application/octet-stream'); response.addHeader('Content-Disposition', "attachment;filename=\"" + this.getFileName() + "\""); }, /** * Process the request * @param {Object} response */ exportRecords: function(response){ this.setHeaders(response); var outputStream = response.getOutputStream(); this.hd = this.beginExport(outputStream); var gr = new GlideRecord(this.parent_table); gr.get(this.sys_id); this.exportRecord(gr); this.exportChildren(); this._exportQuerySets(); this.endExport(outputStream); }, /** * Export an individual GlideRecord * @param GlideRecord record */ exportRecord: function(record){ record = new GlideScriptRecordUtil.get(record).getRealRecord(); var recordSerializer = new GlideRecordXMLSerializer(); recordSerializer.setApplySecurity(true); recordSerializer.serialize(record, this.hd, new Packages.java.lang.String('INSERT_OR_UPDATE')); if (this.includeAttachments && record.getTableName().substring(0, 14) != "sys_attachment") { this.exportAttachments(record); } }, /** * Export attachments for a given GlideRecord * @param GlideRecord target */ exportAttachments: function(target){ var sa = new GlideSysAttachment(); //get sys_attachments var attach = new GlideRecord("sys_attachment"); attach.addQuery("table_name", target.getTableName()); attach.addQuery("table_sys_id", target.getUniqueValue()); attach.query(); while (attach.next()) { this.exportRecord(attach); var parts = sa.getAttachmentParts(attach.getUniqueValue()); while (parts.next()) { this.exportRecord(parts); } } }, /** * Get attachment document parts * @param GlideRecord attach - sys_attachment record */ _exportAttachDoc: function(attach){ var doc = new GlideRecord("sys_attachment_doc"); doc.addQuery("sys_attachment", attach.getUniqueValue()); doc.query(); }, /** * Process related lists */ exportChildren: function(){ for (var key = 0; key < this.related_lists.length; key++) { var table = this.related_lists[key]; this.exportTableChildren(table); } }, /** * Get the records from the related list entries * @param Array table - array of tableName, fieldName */ exportTableChildren: function(table){ var child = new GlideRecord(table[0]); child.addQuery(table[1], this.sys_id); child.query(); while (child.next()) { this.exportRecord(child); } }, /** * Process query sets and export results */ _exportQuerySets: function(){ for (var i = this.query_sets.length - 1; i >= 0; i--) { var querySet = this.query_sets[i]; var table = querySet[0]; var query = querySet[1]; var gr = new GlideRecord(table); gr.addEncodedQuery(query); gr.query(); while (gr._next()) this.exportRecord(gr); } }, /** * Initialize the result * @param {Object} outputStream */ beginExport: function(outputStream){ var streamResult = new Packages.javax.xml.transform.stream.StreamResult(outputStream); var tf = Packages.javax.xml.transform.sax.SAXTransformerFactory.newInstance(); var hd = tf.newTransformerHandler(); var serializer = hd.getTransformer(); serializer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.ENCODING, 'UTF-8'); serializer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, 'yes'); hd.setResult(streamResult); hd.startDocument(); var attr = new GlidesoftGlideAttributesImpl(); attr.addAttribute("unload_date", GlideSysDateUtil.getUMTDateTimeString()); hd.startElement("", "", 'unload', attr); return hd; }, endExport: function(outputStream){ this.hd.endElement("", "", 'unload'); this.hd.endDocument(); outputStream.close(); }, }



















Comments

Popular posts from this blog

ServiceNow timeout settings for virtual agent chat

  After connecting to live agent User is connected to live agent and not responding to live agent chat or distracted. The below properties controls the timeout settings, the OOB settings for reminder is [180 sec] and for cancelling the chat is [360 sec]. The job is default configured to 2 min so I believe no tweaking is required here.  Property -   com.glide.cs.idle_chat_reminder_timeout                  com.glide.cs.idle_chat_cancel_timeout Scheduled job   - Idle Chat Timer Task   https://community.servicenow.com/community?id=community_article&sys_id=1453b03bdbaad0109e691ea668961929   (ServiceNow ) 

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...