Created
August 4, 2020 19:15
-
-
Save chaorace/c25a18612cbdfbd5cacafcd52f2375c1 to your computer and use it in GitHub Desktop.
ServiceNow GlideElement to JS native data conversion function (Global Scope)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Processes a given GlideElement into the closest available JS type | |
* | |
* Will return null if the value is empty or if the field is misconfigured. | |
* Will return undefined if value is exactly equal to undefined. | |
* | |
* Note that journals will only yield HTML output if both preferFullJournal and preferHtml options are true. | |
* | |
* @param {GlideElement_proto} value The GlideElement to convert | |
* @param {Object} [options] Options container. All provided options are off by default | |
* @param {boolean} [options.preferChoiceLabel] For choice items, return the label instead of the value | |
* @param {boolean} [options.preferFullReference] For References/Lists, return full GlideRecords instead of ids | |
* @param {boolean} [options.preferCurrencyReference] For Currency2, return a reference instead of a display value | |
* @param {boolean} [options.preferFullJournal] For Journals, return the full text instead of the latest entry | |
* @param {boolean} [options.preferHtml] For HTML emitting fields, return HTML instead of plain text | |
* @returns {(any|null|undefined)} The JS equivalent of the given value | |
*/ | |
function toJs(value, options){ | |
if(value === undefined) return undefined; | |
if(options === undefined) options = {}; | |
var ed = value.getED(); | |
if(options.preferChoiceLabel && ed.isChoiceTable()) | |
return value.getChoiceValue(); | |
if(ed.list){ | |
if(!ed.reference) return null; | |
var referenceList = value.split(',') || []; | |
if(!options.preferFullReference) return referenceList; | |
// Process the list of ids into a list of GlideRecords | |
// null is returned for any items that were invalid | |
return referenceList.map(function(reference){ | |
var refGr = new GlideRecord(ed.reference); | |
refGr.get(reference); | |
return refGr.isValidRecord() | |
? refGr | |
: null; | |
}); | |
} | |
if(!options.preferCurrencyReference && ed.internalType == 'currency2') | |
return value.getDisplayValue(); | |
if(ed.reference){ | |
if(options.preferFullReference){ | |
var myReference = value.getRefRecord(); | |
// An invalid record indicates an empty field | |
return myReference.isValidRecord() | |
? myReference | |
: null; | |
} else { | |
return value.getValue(); | |
} | |
} | |
if( | |
ed.dateType || | |
ed.duration || | |
ed.timeType || | |
ed.internalType == 'calendar_date_time' | |
){ | |
var dateNumeric = value.dateNumericValue(); | |
// A numeric value of 0 corresponds to an empty field | |
return dateNumeric === 0 | |
? null | |
: new Date(dateNumeric); | |
} | |
if(ed.journal || ed.journalList){ | |
if(options.preferFullJournal){ | |
return options.preferHtml | |
? value.getHTMLValue() | |
: value.getJournalEntry(-1); | |
} | |
// Return the current unsubmitted value if present | |
// Otherwise, yield the last submitted entry | |
return value.getJournalEntry(0) || value.getJournalEntry(1); | |
} | |
// LongInt doesn't trigger the trulyNumber flag for some reason | |
if( ed.trulyNumber || ed.internalType == 'longint') { | |
return value.nil() ? null : Number(value); | |
} | |
if(ed['boolean']) return Boolean(value); | |
if(ed.internalType == 'simple_name_values'){ | |
// Faster than JSON.parse and just fine for this | |
var out = {}; | |
for(var key in value){ | |
out[key] = value[key]; | |
} | |
return out; | |
} | |
if(ed.internalType == 'data_structure') return JSON.parse(value) | |
var stringResult = options.preferHtml | |
? value.getHTMLValue() || value.getValue() | |
: value.getValue(); | |
return stringResult === '' | |
? null | |
: stringResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment