Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Server Side/FetchJSONObject/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The `fetchJSONObject` function is designed to retrieve a JSON object containing specified field values from a GlideRecord instance in ServiceNow.
It allows for optional input of field names to fetch; if no fields are specified, all fields will be retrieved.
30 changes: 30 additions & 0 deletions Server Side/FetchJSONObject/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function fetchJSONObject(gr, desiredFields /* optional */) {
// Ensure this is a valid GlideRecord
if (!(gr instanceof GlideRecord) || !gr.isValidRecord()) {
return {};
}

// Determine fields to retrieve; if none specified, get all fields
if (!Array.isArray(desiredFields) || desiredFields.length === 0) {
var gRU = new GlideRecordUtil();
desiredFields = gRU.getFields(gr); // Retrieves all field names for the record
}

var fieldValues = {};
// Use forEach to loop through the desired fields and add their values to the JSON object
desiredFields.forEach(function(fieldName) {
if (gr.isValidField(fieldName)) { // Ensure the field exists in the GlideRecord
var fieldValue = gr.getValue(fieldName);
fieldValues[fieldName] = fieldValue ? fieldValue : ''; // Use empty string if value is null
}
});

return fieldValues;
}

// Usage example:
//var gr = new GlideRecord('incident');
//gr.get('sys_id'); // Replace with an actual sys_id or use query methods to get a record
//var jsonObject = fetchJSONObject(gr); // Optionally, add an array of specific fields as the second argument
//gs.info(JSON.stringify(jsonObject)); // Logs the JSON object

Loading