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

 

2 REPLIES 2

Eshwar Reddy
Kilo Sage

Hi @shubhamsriv 

 

Try Below code


try {
var startDate = new GlideDateTime(inputs.created_on);
var endDate = new GlideDateTime(inputs.resolved_at);


var duration = GlideDateTime.subtract(endDate, startDate);
var calcDiff = duration.getDaysPart();


var assignedToName = inputs.assigned_to ? inputs.assigned_to.getDisplayValue() : "N/A";
var callerIdName = inputs.caller_id ? inputs.caller_id.getDisplayValue() : "N/A";
var resolvedByName = inputs.resolved_by ? inputs.resolved_by.getDisplayValue() : "N/A";


var report = "After Action Report\n" +
"Subject: After Action Report, " + assignedToName + " - " + 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 " + callerIdName + ". " +
"The Configuration Item affected was " + (inputs.cmdb_ci ? inputs.cmdb_ci.getDisplayValue() : "N/A") + ". " +
"Resolved by: " + resolvedByName + " with a Resolution code of " + inputs.close_code + ". " +
"Resolution notes are as follows: " + (inputs.close_notes || "N/A") + "\n\n" +
"Total time: " + calcDiff + " Days";


outputs.payload = report;
} catch (error) {
outputs.payload = "Error: " + error.message;
}


Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

 

Thanks 
Eshwar

Sandeep Rajput
Tera Patron
Tera Patron

@shubhamsriv Please try the following and see if it works for you.

 

(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.name + " - " + inputs.assignment_group.name + "\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.name + ". " +
                 "The Configuration Item affected was " + inputs.cmdb_ci + ". " +
                 "Resolved by: " + inputs.resolved_by.name + " 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);