Skip to main content

Write a HTML table as an additional comment to a RITM

 

        var table_html = '<table style="width: 90%; margin: 20px auto; border-collapse: collapse;">';

        var rowIndex = 0;

        // Add table headers with styles

        table_html += '<thead><tr style="background-color: #0F52A2; color: white; font-weight: bold;">' +

            '<th style="padding: 10px; text-align: left;">Name</th>' +

            '<th style="padding: 10px; text-align: left;">Value</th>' +

            '</tr></thead>';

        for (var key in json) {

 

            if (json.hasOwnProperty(key)) {

                rowIndex++;

                var rowColor = (rowIndex % 2 === 0) ? '#f2f2f2' : '#ffffff'; // Alternate colors

 

                table_html += '<tr style="background-color: ' + rowColor + ';">' +

                    '<td style="padding: 8px; border: 1px solid #ddd;">' + key + '</td>' +

                    '<td style="padding: 8px; border: 1px solid #ddd;">' + json[key] + '</td>' +

                    '</tr>';

 

            }

        }

        table_html += '</table>';

        return table_html;

    },

 

    getInfoAttributes: function(json_payload) {

        /*

        Takes a JSON payload with key value pairs and strips out only those with 'info' in their name.

        */

        var json_payload2 = json_payload.replace(/\\"/g, '"'); //strip out the escape backslash

 

        var jsonPayload3 = json_payload2.replace(/\[(.*?)\]/g, function(match) {

            return match.replace(/"/g, "'"); //replace double quotes within square brackets (array) with single quotes

        });

        var result = {};

        var jsonPayload4 = JSON.parse(jsonPayload3);

 

        // Loop through all keys in the JSON object

        for (var key in jsonPayload4) {

            if (jsonPayload4.hasOwnProperty(key) && key.toLowerCase().includes("info")) {

                result[key] = jsonPayload4[key];

            }

        }

 

        return result;

 

    },

   

 

Flow action:

 

 





Comments

Popular posts from this blog

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 )){...

Code a pause/wait - gs.sleep or gs.wait alternative, pause script for specified seconds (timer)

Code a pause/wait - gs.sleep / gs.wait alternative, pause script for specified seconds (timer)  e.g. 10 seconds: do_sleep ( 10000 ); function do_sleep ( milliseconds ) { var start = new Date (). getTime (); for ( var i = 0 ; i < 1e7 ; i ++) { if (( new Date (). getTime () - start ) > milliseconds ){ gs . print ( 'waking up!' ); break ; } } }