How to ‘Show XML’ – the not so annoying way

General Add comments
by:

The one thing that possibly annoys me the most on Servicenow is the fact that the ‘Show XML’ pop-up is automatically closed when it is no longer in focus. Actually, I am not sure when the window is closed because two hours of investigating on how Servicenow accomplishes this behaviour yielded no results. Chrome developer tools do not show any event listeners on this, window, document, body or any other element for that matter. Especially while developing you might want to keep the XML open to go back and forth between the record (and all data that might not be shown on the form itself) and what other page or program you have open. The first idea that comes to mind is editing the UI Action itself. But it comes as no surprise that there actually is no UI Action called ‘Show XML’ and inspecting the link from the context menu on the form shows that the href property for the div points to ‘xmlView(table, sysId)’. And in true Servicenow fashion, this is undocumented and inaccessible for editing.


Now that changing the built-in ‘UI Action’ is off the table, let’s just build our own UI Action. This can come in very handy on the development instance you are working on. Admittedly it is not the prettiest, but the solution itself is actually quite simple: create a global UI action that uses the built-in XML web service to display the current record. In order to accomplish this do the following:

  • Create a new UI action and give it a sassy name like ‘Show XML – the not so annoying version’ (ok the last bit is not an actual requirement but it makes the UI Action more fun to use of course 😊).
  • Select ‘Global’ as table.
  • Deselect ‘Show Insert’.
  • Select ‘Form context menu’.
  • Select ‘Client’.
  • In the Onclick field, fill in ‘openXML()’.
  • In the script field put:

    function openXML(){
    var url = 'https://' + window.location.host + '/' + g_form.getTableName() + '.do?XML&useUnloadFormat=true&sysparm_query=sys_id=' + g_form.getUniqueValue();
    window.open(url, 'XML', 'height=640,width=960,toolbar=no');
    }
  • For good measure add ‘admin’ as a required role.

The above steps should give you something like the following:

Now save the UI Action and enjoy nuisance-free alt-tabbing back and forth between your XML pop-up and whatever program you are using by opening the context menu of a record and selecting your new UI Action. Have fun!

.img[at].img

5 Responses to “How to ‘Show XML’ – the not so annoying way”

  1. Christoph Lang Says:

    Or you could edit the “xmlView” function without modifying the code itself as it is inaccesible for us.

    How?

    Just create a new UI Script with the following settings:
    ————————————–
    Name: Better ShowXML
    Global: True
    Description: Whatever…
    Active: True:
    Script:
    /*********/
    window.setTimeout(_replaceXMLView, 2000);
    function _replaceXMLView()
    {
    //Keep pointer to the original function
    var orgxmlView = window.xmlView;
    //Change function with a substitute…
    window.xmlView = function(sTable, sSysID)
    {
    //orgxmlView(sTable, sSysID);
    var url = ‘https://’ + window.location.host + ‘/’ + sTable + ‘.do?XML&useUnloadFormat=true&sysparm_query=sys_id=’ + sSysID;
    window.open(url, ‘_blank’);
    };
    }
    /*********/
    ————————————–

    Using this technique, the xmlView method from ServiceNow is changed, but no source files from ServiceNow are touched (good for upgrades…).
    Plus, only one context menu entry exists…the one every devloper is used to.

    Hope this helps and that this is another idea on how to modify scripts, which are normally not editable for us ๐Ÿ˜‰

  2. Guido Millenaar Says:

    Hi Christoph,

    That is a great idea as well, maybe even better ๐Ÿ™‚ Thanks!

    Cheers,

    Guido

  3. Arnoud Says:

    Nice one to look into…

    Another client script mod would be to overwrite function popupClose:

    $j(function(){
    window.popupClose = function(){};
    });

    Or

    $j(function(){
    window.popupClose = function(){
    if (popupCurrent)
    g_form.addInfoMessage(“For your convenience, pop-up stays open ;-)”);
    popupCurrent = null;
    };
    });

    Moi!

  4. Guido Millenaar Says:

    Ah moi, aal goud?
    Thanks Arnoud, also a great an maybe even better suggestion. ๐Ÿ™‚
    Great to receive all this feedback!

  5. Anil Says:

    Wow.. nice solutions ๐Ÿ™‚

Leave a Reply