- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2022 06:54 AM - edited 11-09-2022 06:54 AM
Hi all,
When I tried to encode the GlideRecord with JSON.stringify, I received the output like below
code:
var grinc= new GlideRecord("incident");
grinc.get("your_sys_id");
gs.print(JSON.stringify(grinc))
Output:
{"sys_meta":{"active":"1","array":"0","attributes":"live_feed=true","audit":"1","calculation":"","choice":"0","choice_field":"","choice_table":"","create_roles":"","default_value":"","delete_roles":"itil_admin","dependent":"","dependent_on_field":"","display":"number","dynamic_creation":"0","dynamic_creation_script":"","dynamic_default_value":"","dynamic_ref_qual":"","element_reference":"0","filterable":"1","foreign_database":"","function_definition":"","function_field":"0","groupable":"1","help":"","hint":"","i18n_sortable":"1","internal_type":"collection","label":"Problem","language":"en","mandatory":"0","matchable":"1","max_length":"40","multi_text":"0","name":"problem","plural":"Problems","primary":"0","read_only":"0","read_roles":"","reference":"","reference_cascade_rule":"","reference_floats":"0","reference_key":"","reference_qual":"","reference_qual_condition":"","reference_type":"","sizeclass":"917","sortable":"1","spell_check":"0","staged":"0","sys_package":"24a88cf121920110a866589604c20d87","sys_scope":"global","table_reference":"0","text_index":"1","type":"0","type_description":"collection","unique":"0","url":"","url_target":"","use_dynamic_default":"0","use_reference_qualifier":"simple","virtual":"0","widget":"","write_roles":"","xml_view":"0"},"parent":{},"watch_list":{},"fix_communicated_by":{},"upon_reject":{},"sys_updated_on":{},"approval_history":{},"skills":{},"number":{},"state":{},"sys_created_by":{},"knowledge":{},"order":{},"major_problem":{},"delivery_plan":{},"cmdb_ci":{},"cmdb_ci_business_app":{},"impact":{},"contract":{},"active":{},"work_notes_list":{},"priority":{},"sys_domain_path":{},"rejection_goto":{},"group_list":{},"business_duration":{},"approval_set":{},"wf_activity":{},"needs_attention":{},"universal_request":{},"short_description":{},"correlation_display":{},"delivery_task":{},"work_start":{},"known_error":{},"additional_assignee_list":{},"service_offering":{},"sys_class_name":{},"workaround":{},"follow_up":{},"closed_by":{},"reopened_by":{},"reassignment_count":{},"confirmed_at":{},"related_incidents":{},"assigned_to":{},"variables":{},"variable_pool":{},"hierarchical_variables":{},"sla_due":{},"comments_and_work_notes":{},"escalation":{},"upon_approval":{},"confirmed_by":{},"correlation_id":{},"first_reported_by_task":{},"made_sla":{},"sn_esign_document":{},"cause_notes":{},"task_effective_number":{},"fix_at":{},"resolved_by":{},"sys_updated_by":{},"user_input":{},"opened_by":{},"sys_created_on":{},"sys_domain":{},"route_reason":{},"closed_at":{},"business_service":{},"rfc":{},"time_worked":{},"expected_start":{},"opened_at":{},"work_end":{},"resolved_at":{},"subcategory":{},"work_notes":{},"reopened_at":{},"assignment_group":{},"description":{},"calendar_duration":{},"fix_by":{},"close_notes":{},"sys_id":{},"sn_esign_esignature_configuration":{},"contact_type":{},"resolution_code":{},"urgency":{},"company":{},"activity_due":{},"workaround_communicated_by":{},"action_status":{},"review_outcome":{},"comments":{},"fix_communicated_at":{},"approval":{},"problem_state":{},"due_date":{},"sys_mod_count":{},"reopen_count":{},"duplicate_of":{},"sys_tags":{},"location":{},"workaround_communicated_at":{},"category":{},"fix_notes":{},"workaround_applied":{}}
Why the output don't include any value?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2022 10:30 PM
Hi @Trung ,
The reason you can't stringify GlideRecord is because the attributes that you see inside the GlideRecord object are not javascript object rather it's a reference to GlideElement object and, what is not a javascript object stringify() method of JSON can't convert to string.
Even if you look at the solution the you posted before in that you are using GlideRecordUtil(), in that the function that you are using populateFromGr that is also using the getter method only.
function (hashmap, gr, ignore) {
var flds = this.getFields(gr);
for (var i = 0; i < flds.length; i++) {
var fldName = flds[i];
var fldValue = gr.getValue(fldName);
if (ignore && ignore[fldName]) {
continue;
}
if (!fldValue) {
continue;
}
hashmap[fldName] = fldValue.toString();
}
}
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2022 06:56 AM
Hi @Trung ,
You can use below code for that :-
var util= new global.GlideRecordUtil();
var grinc= new GlideRecord("incident");
grinc.get("your_sys_id");
var hashmap={};
util.populateFromGR(hashmap,grinc);//use this function to populate all fields to object hashmap and after that we will encode the hashmap object
gs.print(JSON.stringify(hashmap));
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2022 07:02 AM
That is my solution in my previous post :))))))))))))
But in this post my question is why
Why I cannot encode GlideRecord with JSON.stringify()?
:))))))))))))))))))))))))))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2022 07:06 AM
Hi @Trung ,
JSON.stringify() actually converts JSON object into string then how we can say that it is encoded? It is actually a string.
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2022 10:30 PM
Hi @Trung ,
The reason you can't stringify GlideRecord is because the attributes that you see inside the GlideRecord object are not javascript object rather it's a reference to GlideElement object and, what is not a javascript object stringify() method of JSON can't convert to string.
Even if you look at the solution the you posted before in that you are using GlideRecordUtil(), in that the function that you are using populateFromGr that is also using the getter method only.
function (hashmap, gr, ignore) {
var flds = this.getFields(gr);
for (var i = 0; i < flds.length; i++) {
var fldName = flds[i];
var fldValue = gr.getValue(fldName);
if (ignore && ignore[fldName]) {
continue;
}
if (!fldValue) {
continue;
}
hashmap[fldName] = fldValue.toString();
}
}
Regards,
Kamlesh