Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting string value from an array.Object in Flow designer action

Hussain Marakar
Tera Contributor

This is a response I get from an action in flow designer:

This is of the type array.Object

{
    "Response": {
        "temporaryAttachments": [
            {
                "fileName": "Requirement Analysis for Generic ID Creation CHG0092436.docx",
                "temporaryAttachmentId": "430a93eb-d1e7-4683-8b18-dd3c5731dd83"
            }
        ]
    }
}


 

 From this I want to fetch only the temporary attachment ID.

To achieve this I have created another action, which takes input as this from the previous action and given an inline script to fetch the ID from the .Object,

Script:

(function execute(inputs, outputs) {
// Input: array.object containing temporaryAttachments
// Output: string containing temporaryAttachmentId

// Check if the input is valid and contains temporaryAttachments
if (inputs && inputs.Response && inputs.Response.temporaryAttachments) {
// Get the first temporary attachment object
const tempAttachment = inputs.Response.temporaryAttachments[0];

// Check if the temporary attachment object exists
if (tempAttachment && tempAttachment.temporaryAttachmentId) {
// Return the temporary attachment ID as a string
outputs.attachmentId = tempAttachment.temporaryAttachmentId.toString();
} else {
outputs.errorMessage = "Temporary attachment ID not found.";
}
} else {
outputs.errorMessage = "Input does not contain temporary attachments.";
}
})(inputs, outputs);



I have defined the outputs also, although the execution shows successful, the attcahment ID is returing blank.
Kindly help me solve this, or a way to extract the ID from the object.

8 REPLIES 8

Vishwa Pandya19
Mega Sage

Hello,

 

Please change below line and try again.

From:

const tempAttachment = inputs.Response.temporaryAttachments[0];

 

to:

var tempAttachment = inputs.Response.temporaryAttachments[0];

 

This worked for me in a background script.

 

If my answer helps you in any way please mark it as correct or helpful.

Tried this, but in flow the output is still blank,

HussainMarakar_0-1713430115767.png

Input given:

HussainMarakar_3-1713430202227.png

 


Logs:

HussainMarakar_1-1713430146854.png

 Script:

HussainMarakar_2-1713430173392.png

 

Code looks right. 
Can you share the screenshots of the output mapping?

Sure,

Script step output variavles:

HussainMarakar_0-1713432279644.png


Output:

HussainMarakar_1-1713432304487.pngHussainMarakar_2-1713432320085.png



Also Inputs:


HussainMarakar_3-1713432360670.png