Skip to content

Commit 4782f24

Browse files
authored
Gr Fetch Record Fields JSON (#1568)
* Create README.md * Create script.js
1 parent 5a253fb commit 4782f24

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<h2>Get Record Fields in JSON</h2>
2+
3+
<h3>Description</h3>
4+
<p>This code fetches all field values from a specified record in ServiceNow and returns them in a JSON format. This is ideal for exporting data or logging record details in a structured format. By simply passing in a table name and a <code>sys_id</code>, it retrieves and formats the fields, allowing for easy data handling.</p>
5+
6+
<h3>Usage</h3>
7+
<p>Call the function and provide:</p>
8+
<ul>
9+
<li><strong>tableName</strong> (String): The name of the table where the record is stored.</li>
10+
<li><strong>sysId</strong> (String): The <code>sys_id</code> of the record to retrieve.</li>
11+
</ul>
12+
13+
14+
15+
<h3>Example</h3>
16+
<p>In this example, we fetch all fields from a record in the <code>incident</code> table with a specified <code>sys_id</code> and log the result to the console.</p>
17+
<pre><code>
18+
var recordData = grFetchRecordFields('incident', 'sys_id_value');
19+
gs.info('Record Data: ' + recordData);
20+
</code></pre>
21+
22+
<h4>Output</h4>
23+
<p>If the record with the specified <code>sys_id</code> exists, the output will be a JSON string containing all field names and their values. For example:</p>
24+
<pre><code>{
25+
"number": "INC0010001",
26+
"short_description": "Sample incident description",
27+
"priority": "3",
28+
"state": "New",
29+
"assigned_to": "System Administrator",
30+
...
31+
}
32+
</code></pre>
33+
34+
<h3>Benefits</h3>
35+
<ul>
36+
<li><strong>Simplifies data retrieval</strong>: Quickly fetch all field values from a record without manually specifying each field.</li>
37+
<li><strong>Easy JSON output</strong>: Ideal for logging or API responses where JSON format is needed.</li>
38+
<li><strong>Reduces errors</strong>: Avoids repetitive code and minimizes the risk of missing fields during retrieval.</li>
39+
</ul>
40+
41+
<h3>Notes</h3>
42+
<ul>
43+
<li>Ensure <code>tableName</code> and <code>sysId</code> are valid to prevent runtime errors.</li>
44+
<li>Use this with caution on tables with many fields to avoid performance issues.</li>
45+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//# Fetch all fields of a record and return as JSON
2+
function grFetchRecordFields(tableName, sysId) {
3+
var result = {};
4+
var gr = new GlideRecord(tableName);
5+
if (gr.get(sysId)) {
6+
var fields = gr.getFields();
7+
for (var i = 0; i < fields.size(); i++) {
8+
var fieldName = fields.get(i).getName();
9+
result[fieldName] = gr.getValue(fieldName);
10+
}
11+
}
12+
return JSON.stringify(result);
13+
}
14+
15+
// Example Usage:
16+
var recordData = grFetchRecordFields('incident', 'sys_id_value');
17+
gs.info('Record Data: ' + recordData);

0 commit comments

Comments
 (0)