Getting [object GlideRecord] for some of Input variables in Script Step of Action called from Flow.

shubhamsriv
Tera Contributor

Hello ,Please find my code and Output ,could you please help out me where is the error ?

My Code :

(function execute(inputs, outputs) {
    // Create GlideDateTime objects for the start and end dates
    var startDate = new GlideDateTime(inputs.created_on);
    var endDate = new GlideDateTime(inputs.resolved_at);
   
    // Calculate the difference in days
    var calcDiff = GlideDateTime.subtract(endDate, startDate).getRoundedDayPart();
   
    // Construct the report string
    var report = "After Action Report\n" +
                 "Subject: After Action Report, " + inputs.assigned_to + " - " + inputs.assignment_group + "\n" +
                 "Created On: " + inputs.created_on + " - Resolved At: " + inputs.resolved_at + "\n\n" +
                 inputs.number + " with " + inputs.short_description + " was entered into ServiceNow by " +
                 inputs.created_by + " for " + inputs.caller_id + ". " +
                 "The Configuration Item affected was " + inputs.cmdb_ci + ". " +
                 "Resolved by: " + inputs.resolved_by + " with a Resolution code of " + inputs.close_code + ". " +
                 "Resolution notes are as follows: " + inputs.close_notes + "\n\n" +
                 "Total time: " + calcDiff + " Days";

    // Set the output payload
    outputs.payload = report;
})(inputs, outputs);
 
Output :
 
After Action Report
Subject: After Action Report, [object GlideRecord] - undefined
Created On: 2024-09-28 07:09:19 - Resolved At: 2024-10-01 07:08:55

INC0010054 with 3D Pinball is broken was entered into ServiceNow by admin for [object GlideRecord]. The Configuration Item affected was undefined. Resolved by: [object GlideRecord] with a Resolution code of undefined. Resolution notes are as follows: undefined

Total time: -2 Days

 

1 REPLY 1

Uncle Rob
Kilo Patron

What's the difference between the fields that worked and the fields that said [object GlideRecord]?
Answer:  The fields that worked weren't references.

Open up your background scripts on Dev and do the following

var rotisserieChicken = new GlideRecord('incident');
rotisserieChicken.setLimit(1);
rotisserieChicken.query();

while (rotisserieChicken.next()){
	gs.print(rotisserieChicken.caller_id);
}


What does it output?   A sys_id right?
But you were EXPECTING it to be "the person".  Remember, all reference fields store sys_ids ("use this key to look up in another table and let me borrow all that record's data).  In the ServiceNow UI it doesn't show us the sys_id, it shows us the DISPLAY VALUE.

So lets revisit our script....

var rotisserieChicken = new GlideRecord('incident');
rotisserieChicken.setLimit(1);
rotisserieChicken.query();

while (rotisserieChicken.next()){
	//adding getDisplayValue()
	gs.print(rotisserieChicken.caller_id.getDisplayValue());
}


So all you need to do is put the .getDisplayValue() on any of those parts of your code that are dropping reference fields.  Kuddos to you for taking on custom flow actions.  BE SURE TO DOCUMENT WHAT YOU BUILT!!

Screenshot 2023-03-16 09-03-21.png