towerofpower256 / davidmac.pro

My personal website
0 stars 1 forks source link

posts/2021-06-10-gliderecord-json/ #15

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

ServiceNow Script: GlideRecord to JSON

Get a plain JSON object from a ServiceNow record without hard-coding. Perfect for integrations!

https://davidmac.pro/posts/2021-06-10-gliderecord-json/

codaroma commented 2 years ago

Some other rough and ready simple options...

JSON.stringify(new XMLHelper(new GlideRecordXMLSerializer().serialize(gr)).toObject())

JSON.stringify(gs.xmlToJSON(new GlideRecordXMLSerializer().serialize(gr)))
APraestegaard commented 12 months ago

Thank you. I have made a Script Include revision that works in a Scoped application.


/**
 * @class
 * @classdesc Utility functions for serializing GlideRecord into JSON objects.
 */
var GlideRecordSerializer = Class.create();
GlideRecordSerializer.prototype = {
    initialize: function() {
    },

    /**
     * Create a JavaScript object from a GlideRecord object.
     *
     * @param {GlideRecord} gr - The GlideRecord object to convert to a JavaScript object.
     * @param {Array} [fields] - An optional array of field names to include. If not provided, includes all fields.
     * @returns {Object} The JavaScript object representing the GlideRecord.
     */
    SerializeGlideRecord: function (gr, fields) {
        if (!fields) {
            fields = new global.GlideRecordUtil().getFields(gr);
            fields.sort();
        }

        var obj = {};
        for (var field in fields) {
            var fieldName = fields[field];
            var element = gr.getElement(fieldName); // Get the Field object (element)
            var ed = element.getED(); // Get the Element Descriptor for this field
            var isChoiceField = ed.isChoiceTable(); // Check if this field is a choice field
            var fieldType = ed.getInternalType(); // Get the field's type name

            // Handle choice fields
            if (isChoiceField) {
                this._addElement(obj, fieldName, gr.getValue(fieldName), element.getChoiceValue());
                continue;
            }

            // Handle boolean and journal_input field types
            if (fieldType == 'boolean' || fieldType == 'journal_input') {
                obj[fields[field]] = gr.getDisplayValue(fields[field]);
                continue;
            }

            // Handle fields that should return a value and a display value
            var displayValueFields = ["reference", "glide_date", "glide_time", "glide_date_time"];
            if (displayValueFields.indexOf(fieldType) > -1) {
                this._addElement(obj, fieldName, gr.getValue(fieldName), gr.getDisplayValue(fieldName));
                continue;
            }

            this._addElement(obj, fieldName, gr.getValue(fieldName), "");
        }

        // Return the object
        return obj;
    },

    /**
     * Function supporting 'SerializeGlideRecord' to add an element to the object.
     *
     * @param {Object} obj - The object to add the element to.
     * @param {string} fieldName - The field name.
     * @param {*} value - The value of the field.
     * @param {string} displayValue - The display value of the field.
     */
    _addElement: function(obj, fieldName, value, displayValue) {
        obj[fieldName] = {};
        obj[fieldName]["value"] = value || "";
        obj[fieldName]["display_value"] = displayValue || "";
    },

    type: 'GlideRecordSerializer'
};