Announcing the Global SNUG Board of Directors. Learn more here

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
sabell2012
Mega Sage
Mega Sage

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL: BEGINNER
Assumes good intermediate level knowledge and/or familiarity of Scripting in ServiceNow.


I have been poking around with this for quite awhile, but had not really come up with something that I liked.   Recently I was reading, cover-to-cover, the book "The Principles of Object-Oriented JavaScript" by Nicholas C. Zakas (ISBN-13: 978-1593275402). I turned a page and lo! The clouds parted! The light shone down (on the book), and the angels sang! The answer was there! On the page! The Holy Grail! I had found it! Well...okay, maybe not so dramatic, but I was excited.

 

So, what I was needing was not only a way to iterate through the properties of a GlideRecord (without knowing what they all were), but also then pulling the values of the GlideRecord and stuffing them into a modifiable object.   GlideRecords, by design, are immutable (a fancy term for unmodifiable). Frankly, I have needed a way to rapidly convert a GlideRecord into a mutable object array.

 

Grabbing my penny pencil and a scrap of napkin I quickly figured out what I needed to do and began to model it in Scripts - Background.   After getting the model mostly there, I then moved to a Scheduled Job and really went to work. 47 hours later...um, 1 hour later I had my solution.

 

Here is my Script:

 

test();

function test() {
    var incidentRecords = new GlideRecord('incident');
    incidentRecords.addQuery('state', '!=', 7); // closed
    incidentRecords.setLimit(5); // note that this comes from your inputs
    incidentRecords.orderByDesc('number');
    incidentRecords.query();

    var incidentList = []; // this will be an array of objects

    while (incidentRecords.next()) {
        incidentList.push(toObject(incidentRecords));
    }

    gs.info('---> Length: ' + incidentList.length);

    for (var i=0; i < incidentList.length; i++) {
        gs.info(JSON.stringify(incidentList[i]));
    }
}

// ----------------------------------------------
// GlideRecord to Object Converter
//
function toObject(recordToPackage) {
    var packageToSend = {};

    for (var property in recordToPackage) {
        try {
            packageToSend[property] = recordToPackage[property].getDisplayValue();
        }
        catch(err){} // throw away any problem properties
    }

    return packageToSend;
}

 

Results:

*** Script: ---> Length: 5
*** Script: {"promoted_by":"","parent":"","caused_by":"","watch_list":"","upon_reject":"Cancel all future Tasks","sys_updated_on":"2023-06-03 04:30:29","origin_table":"em_alert","approval_history":"","skills":"","proposed_by":"","number":"INC0010693","u_requester":"","lessons_learned":"","u_rca":"","state":"Active","sys_created_by":"admin","knowledge":"false","order":"","delivery_plan":"","cmdb_ci":"Impact Calculation","cmdb_ci_business_app":"","impact":"3 - Low","contract":"","active":"true","work_notes_list":"","priority":"3 - Moderate","sys_domain_path":"/","rejection_goto":"","group_list":"","business_duration":"","approval_set":"","wf_activity":"","major_incident_state":"","universal_request":"","short_description":"There is delay in the impact jobs. The longest delay is 3342 seconds.\n Additional Info \nThe job: Event Management - Impact Calculator Trigger is idle for 5 seco","correlation_display":"","delivery_task":"","work_start":"","trigger_rule":"","additional_assignee_list":"","notify":"Do Not Notify","service_offering":"","sys_class_name":"Incident","follow_up":"","closed_by":"","parent_incident":"","reopened_by":"","reassignment_count":"0","assigned_to":"","variables":"","variable_pool":"","hierarchical_variables":"variable_pool","sla_due":"UNKNOWN","comments_and_work_notes":"2023-06-03 04:28:27 - System Administrator (Work notes)\nThe related alert Alert0010722 state is now Open\n\n","escalation":"Overdue","upon_approval":"Proceed to Next Task","timeline":"","correlation_id":"","made_sla":"true","promoted_on":"","child_incidents":"0","task_effective_number":"INC0010693","resolved_by":"","sys_updated_by":"system","user_input":"","opened_by":"System Administrator (admin)","sys_created_on":"2023-06-03 04:28:25","sys_domain":"global","proposed_on":"","actions_taken":"","route_reason":"","calendar_stc":"","closed_at":"","u_source_table":"","business_service":"","business_impact":"","rfc":"","time_worked":"","expected_start":"","opened_at":"2023-06-03 04:28:25","work_end":"","resolved_at":"","reopened_time":"","caller_id":"Event Management (Event Management)","subcategory":"","work_notes":"2023-06-03 04:28:27 - System Administrator (Work notes)\nThe related alert Alert0010722 state is now Open\n\n","x_0462_loaner_app_steve":"","close_code":"","assignment_group":"","business_stc":"","cause":"","description":"There is delay in the impact jobs. The longest delay is 3342 seconds.\n Additional Info \nThe job: Event Management - Impact Calculator Trigger is idle for 5 seconds\nThe job: Event Management - Impact Calculator for BS is idle for 3337 seconds\nThe job: Event Management - Impact Calculator for Alert Groups and SLA is idle for 3337 seconds\nThe job: Event Management - Impact Calculator for BS is idle for 3337 seconds\nThe job: Event Management - Impact for service groups is idle for 3342 seconds\nThe job: Event Management - Impact Calculator for BS is idle for 3337 seconds\nThe job: Event Management - Impact Calculator for BS is idle for 3337 seconds\n","origin_id":"","calendar_duration":"","close_notes":"","sys_id":"d0ad29409713211091807f200153af74","contact_type":"Alert","incident_state":"Active","urgency":"1 - High","problem_id":"","company":"","activity_due":"UNKNOWN","severity":"3 - Low","overview":"","comments":"","approval":"Not Yet Requested","due_date":"","sys_mod_count":"4","reopen_count":"0","sys_tags":"","u_document_id":"","location":"","category":"Inquiry / Help"}
*** Script: {"promoted_by":"","parent":"","caused_by":"","watch_list":"","upon_reject":"Cancel all future Tasks","sys_updated_on":"2023-02-27 15:35:41","origin_table":"em_alert","approval_history":"","skills":"","proposed_by":"","number":"INC0010569","u_requester":"","lessons_learned":"","u_rca":"","state":"Active","sys_created_by":"admin","knowledge":"false","order":"","delivery_plan":"","cmdb_ci":"Impact Calculation","cmdb_ci_business_app":"","impact":"3 - Low","contract":"","active":"true","work_notes_list":"","priority":"3 - Moderate","sys_domain_path":"/","rejection_goto":"","group_list":"","business_duration":"","approval_set":"","wf_activity":"","major_incident_state":"","universal_request":"","short_description":"There is delay in the impact jobs. The longest delay is 24551 seconds.\n Additional Info \nThe job: Event Management - Impact Calculator Trigger is idle for 3 sec","correlation_display":"","delivery_task":"","work_start":"","trigger_rule":"","additional_assignee_list":"","notify":"Do Not Notify","service_offering":"","sys_class_name":"Incident","follow_up":"","closed_by":"","parent_incident":"","reopened_by":"","reassignment_count":"0","assigned_to":"","variables":"","variable_pool":"","hierarchical_variables":"variable_pool","sla_due":"UNKNOWN","comments_and_work_notes":"","escalation":"Overdue","upon_approval":"Proceed to Next Task","timeline":"","correlation_id":"","made_sla":"true","promoted_on":"","child_incidents":"0","task_effective_number":"INC0010569","resolved_by":"","sys_updated_by":"system","user_input":"","opened_by":"System Administrator (admin)","sys_created_on":"2023-02-27 15:33:40","sys_domain":"global","proposed_on":"","actions_taken":"","route_reason":"","calendar_stc":"","closed_at":"","u_source_table":"","business_service":"","business_impact":"","rfc":"","time_worked":"","expected_start":"","opened_at":"2023-02-27 15:33:40","work_end":"","resolved_at":"","reopened_time":"","caller_id":"Event Management (Event Management)","subcategory":"","work_notes":"","x_0462_loaner_app_steve":"","close_code":"","assignment_group":"","business_stc":"","cause":"","description":"There is delay in the impact jobs. The longest delay is 24551 seconds.\n Additional Info \nThe job: Event Management - Impact Calculator Trigger is idle for 3 seconds\nThe job: Event Management - Impact Calculator for BS is idle for 24546 seconds\nThe job: Event Management - Impact Calculator for Alert Groups and SLA is idle for 24546 seconds\nThe job: Event Management - Impact Calculator for BS is idle for 24546 seconds\nThe job: Event Management - Impact for service groups is idle for 24551 seconds\nThe job: Event Management - Impact Calculator for BS is idle for 24546 seconds\nThe job: Event Management - Impact Calculator for BS is idle for 24546 seconds\n","origin_id":"","calendar_duration":"","close_notes":"","sys_id":"080b39519711211091807f200153af2e","contact_type":"Alert","incident_state":"Active","urgency":"1 - High","problem_id":"","company":"","activity_due":"UNKNOWN","severity":"3 - Low","overview":"","comments":"","approval":"Not Yet Requested","due_date":"","sys_mod_count":"3","reopen_count":"0","sys_tags":"","u_document_id":"","location":"","category":"Inquiry / Help"}
*** Script: {"promoted_by":"","parent":"","caused_by":"","watch_list":"","upon_reject":"Cancel all future Tasks","sys_updated_on":"2022-08-17 09:16:52","origin_table":"","approval_history":"","skills":"","proposed_by":"","number":"INC0010314","u_requester":"","lessons_learned":"","u_rca":"","state":"Active","sys_created_by":"admin","knowledge":"false","order":"","delivery_plan":"","cmdb_ci":"Event Management","cmdb_ci_business_app":"","impact":"3 - Low","contract":"","active":"true","work_notes_list":"","priority":"3 - Moderate","sys_domain_path":"/","rejection_goto":"","group_list":"","business_duration":"","approval_set":"","wf_activity":"","major_incident_state":"","universal_request":"","short_description":"Job 'Event Management - Insert Health Monitor' is delayed more than 20422.053seconds\n\n To remediate the issue follow these steps:\n\n1. Check When 'Event Manageme","correlation_display":"","delivery_task":"","work_start":"","trigger_rule":"","additional_assignee_list":"","notify":"Do Not Notify","service_offering":"","sys_class_name":"Incident","follow_up":"","closed_by":"","parent_incident":"","reopened_by":"","reassignment_count":"0","assigned_to":"","variables":"","variable_pool":"","hierarchical_variables":"variable_pool","sla_due":"UNKNOWN","comments_and_work_notes":"","escalation":"Overdue","upon_approval":"Proceed to Next Task","timeline":"","correlation_id":"","made_sla":"true","promoted_on":"","child_incidents":"0","task_effective_number":"INC0010314","resolved_by":"","sys_updated_by":"system","user_input":"","opened_by":"System Administrator (admin)","sys_created_on":"2022-08-17 09:14:38","sys_domain":"global","proposed_on":"","actions_taken":"","route_reason":"","calendar_stc":"","closed_at":"","u_source_table":"","business_service":"","business_impact":"","rfc":"","time_worked":"","expected_start":"","opened_at":"2022-08-17 09:14:38","work_end":"","resolved_at":"","reopened_time":"","caller_id":"Event Management (Event Management)","subcategory":"","work_notes":"","x_0462_loaner_app_steve":"","close_code":"","assignment_group":"","business_stc":"","cause":"","description":"Job 'Event Management - Insert Health Monitor' is delayed more than 20422.053seconds\n\n To remediate the issue follow these steps:\n\n1. Check When 'Event Management - Insert Health Monitor' is supposed to run : see Next Action field in sys_trigger table\n2. Check logs\n3. Check thread.do\n","origin_id":"","calendar_duration":"","close_notes":"","sys_id":"1d1291762f11111097fb206df699b620","contact_type":"Alert","incident_state":"Active","urgency":"1 - High","problem_id":"","company":"","activity_due":"UNKNOWN","severity":"3 - Low","overview":"","comments":"","approval":"Not Yet Requested","due_date":"","sys_mod_count":"3","reopen_count":"0","sys_tags":"","u_document_id":"","location":"","category":"Inquiry / Help"}
*** Script: {"promoted_by":"","parent":"","caused_by":"","watch_list":"","upon_reject":"Cancel all future Tasks","sys_updated_on":"2022-08-17 09:16:39","origin_table":"","approval_history":"","skills":"","proposed_by":"","number":"INC0010313","u_requester":"","lessons_learned":"","u_rca":"","state":"Active","sys_created_by":"admin","knowledge":"false","order":"","delivery_plan":"","cmdb_ci":"Event Management","cmdb_ci_business_app":"","impact":"3 - Low","contract":"","active":"true","work_notes_list":"","priority":"3 - Moderate","sys_domain_path":"/","rejection_goto":"","group_list":"","business_duration":"","approval_set":"","wf_activity":"","major_incident_state":"","universal_request":"","short_description":"Job 'Event Management - Update stuck connectors' is delayed more than 279.853seconds\n\n To remediate the issue follow these steps:\n\n1. Check When 'Event Managem","correlation_display":"","delivery_task":"","work_start":"","trigger_rule":"","additional_assignee_list":"","notify":"Do Not Notify","service_offering":"","sys_class_name":"Incident","follow_up":"","closed_by":"","parent_incident":"","reopened_by":"","reassignment_count":"0","assigned_to":"","variables":"","variable_pool":"","hierarchical_variables":"variable_pool","sla_due":"UNKNOWN","comments_and_work_notes":"","escalation":"Overdue","upon_approval":"Proceed to Next Task","timeline":"","correlation_id":"","made_sla":"true","promoted_on":"","child_incidents":"0","task_effective_number":"INC0010313","resolved_by":"","sys_updated_by":"system","user_input":"","opened_by":"System Administrator (admin)","sys_created_on":"2022-08-17 09:14:21","sys_domain":"global","proposed_on":"","actions_taken":"","route_reason":"","calendar_stc":"","closed_at":"","u_source_table":"","business_service":"","business_impact":"","rfc":"","time_worked":"","expected_start":"","opened_at":"2022-08-17 09:14:21","work_end":"","resolved_at":"","reopened_time":"","caller_id":"Event Management (Event Management)","subcategory":"","work_notes":"","x_0462_loaner_app_steve":"","close_code":"","assignment_group":"","business_stc":"","cause":"","description":"Job 'Event Management - Update stuck connectors' is delayed more than 279.853seconds\n\n To remediate the issue follow these steps:\n\n1. Check When 'Event Management - Update stuck connectors' is supposed to run : see Next Action field in sys_trigger table\n2. Check logs\n3. Check thread.do\n","origin_id":"","calendar_duration":"","close_notes":"","sys_id":"590211762f11111097fb206df699b666","contact_type":"Alert","incident_state":"Active","urgency":"1 - High","problem_id":"","company":"","activity_due":"UNKNOWN","severity":"3 - Low","overview":"","comments":"","approval":"Not Yet Requested","due_date":"","sys_mod_count":"3","reopen_count":"0","sys_tags":"","u_document_id":"","location":"","category":"Inquiry / Help"}
*** Script: {"promoted_by":"","parent":"","caused_by":"","watch_list":"","upon_reject":"Cancel all future Tasks","sys_updated_on":"2022-08-17 09:16:31","origin_table":"","approval_history":"","skills":"","proposed_by":"","number":"INC0010312","u_requester":"","lessons_learned":"","u_rca":"","state":"Active","sys_created_by":"admin","knowledge":"false","order":"","delivery_plan":"","cmdb_ci":"Event Management","cmdb_ci_business_app":"","impact":"3 - Low","contract":"","active":"true","work_notes_list":"","priority":"3 - Moderate","sys_domain_path":"/","rejection_goto":"","group_list":"","business_duration":"","approval_set":"","wf_activity":"","major_incident_state":"","universal_request":"","short_description":"Job 'Event Management - Update Health Monitor' is delayed more than 433.505seconds\n\n To remediate the issue follow these steps:\n\n1. Check When 'Event Management","correlation_display":"","delivery_task":"","work_start":"","trigger_rule":"","additional_assignee_list":"","notify":"Do Not Notify","service_offering":"","sys_class_name":"Incident","follow_up":"","closed_by":"","parent_incident":"","reopened_by":"","reassignment_count":"0","assigned_to":"","variables":"","variable_pool":"","hierarchical_variables":"variable_pool","sla_due":"UNKNOWN","comments_and_work_notes":"","escalation":"Overdue","upon_approval":"Proceed to Next Task","timeline":"","correlation_id":"","made_sla":"true","promoted_on":"","child_incidents":"0","task_effective_number":"INC0010312","resolved_by":"","sys_updated_by":"system","user_input":"","opened_by":"System Administrator (admin)","sys_created_on":"2022-08-17 09:14:05","sys_domain":"global","proposed_on":"","actions_taken":"","route_reason":"","calendar_stc":"","closed_at":"","u_source_table":"","business_service":"","business_impact":"","rfc":"","time_worked":"","expected_start":"","opened_at":"2022-08-17 09:14:05","work_end":"","resolved_at":"","reopened_time":"","caller_id":"Event Management (Event Management)","subcategory":"","work_notes":"","x_0462_loaner_app_steve":"","close_code":"","assignment_group":"","business_stc":"","cause":"","description":"Job 'Event Management - Update Health Monitor' is delayed more than 433.505seconds\n\n To remediate the issue follow these steps:\n\n1. Check When 'Event Management - Update Health Monitor' is supposed to run : see Next Action field in sys_trigger table\n2. Check logs\n3. Check thread.do\n","origin_id":"","calendar_duration":"","close_notes":"","sys_id":"91f19d362f11111097fb206df699b6c5","contact_type":"Alert","incident_state":"Active","urgency":"1 - High","problem_id":"","company":"","activity_due":"UNKNOWN","severity":"3 - Low","overview":"","comments":"","approval":"Not Yet Requested","due_date":"","sys_mod_count":"3","reopen_count":"0","sys_tags":"","u_document_id":"","location":"","category":"Inquiry / Help"}

The .toObject() function spins through all of the properties in the GlideRecord.   There were a couple that refused to cough up with info and actually caused an error situation, but they were not really important for general usage; so I skipped them with the Try/Catch.   As I noted in a previous article (Community Code Snippets - GlideRecord - Bracket Notation Reference vs. eval) you can reference any given property value by using the object's array reference capability.

 

And there you have it! BTW, I will be doing a code-review article later on Zakas' book.

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

find_real_file.png

Originally published on: 09-24-2015 08:22 AM

I updated the code and brought the article into alignment with my new formatting standard.

3 Comments