AndersBGS
Tera Patron
Tera Patron

In record producers for e.g. incident creation”, we often ask users to give a description of their problem. But did you know that you can provide a dynamic template for the user to fill out directly in the description and which can change dynamically based on form selection.

 

How to do this:

 

In a record producer create a catalog client script which calls a glideajax/script include:

    var ga = new GlideAjax('UIMessageAjax');
    ga.addParam('sysparm_name', 'getMappingForValue');
    ga.addParam('sysparm_application_service', newValue); // sys_id
    ga.getXMLAnswer(function(response) {
        var answer = JSON.parse(response || '{}');
        var templateKey = answer.templateKey;

        if (!templateKey) {
            g_form.setValue('description', '');
            return;
        }

The script include will then utilize the decisiontableAPI to fetch the unique key value from sys_ui_message based on the input from the e.g. selected application on the form or whatever input you chose and whatever you have mapped in your decision table. 

var appServiceSysId = this.getParameter('sysparm_application_service');
        appServiceSysId = String(appServiceSysId);

        try {

            var inputs = {};
            inputs['u_application'] = appServiceSysId;

            var dtSysId = '(SYSID of the decision table)';

            var dt = new sn_dt.DecisionTableAPI();
            var response = dt.getDecision(dtSysId, inputs);
            var result_elements = response.result_elements;

            var u_templateKey = result_elements.u_templatekey.getValue();

            //Return value to the client script
            var result = {
                templateKey: u_templateKey
            };
            return JSON.stringify(result);

After they key has been fetched, the template text will be fetched based on the key and inserted into the description field on the form (the actual template) for the user to fill out by another glideajax:

    function fetchMessageAjax(key, variableName) {
        var ga = new GlideAjax('UIMessageAjax');
        ga.addParam('sysparm_name', 'getMessageByKey');
        ga.addParam('sysparm_key', key);
        ga.getXMLAnswer(function(response) {
            g_form.setValue(variableName, response || '');

And then by the script include:

    getMessageByKey: function() {
        var key = (this.getParameter('sysparm_key') || '').trim();
        if (!key) return '';

        var gr = new GlideRecord('sys_ui_message');
        gr.addQuery('sys_id', key);
        gr.query();
        if (gr.next()) {
            var msg = gr.getValue('message'); // returns raw message HTML
            return msg;

This approach is quite handy when there is something that is nice to be informed about but not needed to have and it's scalable, as you only need to enhance your decision table and off course the message in the sys_ui_message table.

 

For information that is needed, I would always go for a mandatory variable.

Version history
Last update:
3 hours ago
Updated by:
Contributors