Data Stream Action Script Parser Step

Oscar Mateos
Tera Contributor

I am creating a data stream actions with page pagination setup. I am running issues into the output display/ script parser step. 

 

Here's an example of what the payload returns from the REST call:

{
    "Records": [
        {
            "TimeCreated": "2024-03-11T05:36:35Z",
            "ApprovalRequests": [
                {
                    "State": "Approved",
                    "ApprovalRequired": true,
                    "Approver": {
                        "UniqueName": "JMANN1",
                        "PasswordAdapter": "PasswordAdapter1"
                    }
                }
            ],
            "Active": true,
            "ApprovedState": "Approved",
            "StatusString": "Paid",
            "InitialUniqueName": "IRTEST123",
            "TimeUpdated": "2024-03-21T21:06:39Z",
            "UniqueName": "IRTEST123",
            "Name": "IRTEST123"
        }], 
 
"PageToken": "QUYzOEFRT06DTW9hNlVz"
}
 
 
I'm able to get a return of all the fields except anything that's inside of: "ApprovalRequests",
I want to get the state, ApprovalRequired, and UniqueName. I'm getting this issue: Error: Cannot read property "UniqueName" from undefined,Detail: Cannot read property "UniqueName" from undefinedijiejijfifjeifj-parse.PNGdata-stream-action.PNG
1 ACCEPTED SOLUTION

Nick Parsons
Mega Sage

Assuming your objectRecord is an object from within your Records array, your issue is that ApprovalRequests holds an array, not an object (see your JSON):

 "ApprovalRequests": [ // <--- This square bracket means ApprovalRequests is an array

Arrays don't have properties like Approver on it, so when you access this, you get undefined, and when you subsequentially try and access UniqueName on this undefined value you get your error. So you need to index into the array to access the object, for example:

outputs.targetObject.approvalrequest = objectRecord.ApprovalRequests[0].Approver.UniqueName;

I imagine you'll need to change this in other places that you refer to ApprovalRequests otherwise you'll get undefined for those values.

Keep in mind that this is just a fix for the data you've shown. You should consider what happens when the ApprovalsRequests array has multiple objects if that can ever occur, or if the array might be empty.

View solution in original post

4 REPLIES 4

Abhay Kumar1
Giga Sage

@Oscar Mateos Try this if works because you have to go through record and get first array object which is a jason and from there you can get approval required.

var records=output.targetObject.Records[0]:

var approvalRequest=records['ApprovalRequests'][0];

var approvalRequired=approvalRequests['approvalRequired'];

 

you can test this if works then this code can be minimised further.

hope this will help you,thanks

 

Nick Parsons
Mega Sage

Assuming your objectRecord is an object from within your Records array, your issue is that ApprovalRequests holds an array, not an object (see your JSON):

 "ApprovalRequests": [ // <--- This square bracket means ApprovalRequests is an array

Arrays don't have properties like Approver on it, so when you access this, you get undefined, and when you subsequentially try and access UniqueName on this undefined value you get your error. So you need to index into the array to access the object, for example:

outputs.targetObject.approvalrequest = objectRecord.ApprovalRequests[0].Approver.UniqueName;

I imagine you'll need to change this in other places that you refer to ApprovalRequests otherwise you'll get undefined for those values.

Keep in mind that this is just a fix for the data you've shown. You should consider what happens when the ApprovalsRequests array has multiple objects if that can ever occur, or if the array might be empty.

Thank you @Nick Parsons ! This worked and saved a lot of headaches. I am now getting the approver uniqueName.

 

I'm having pagination setup issues at the moment. 

It does not require limit/offset.

 

The payload returns a PageToken:(example below)

{
    "Records": [
        {
            "TimeCreated": "2024-03-11T05:36:35Z",
            "ApprovalRequests": [
                {
                    "State": "Approved",
                    "ApprovalRequired": true,
                    "Approver": {
                        "UniqueName": "JMANN1",
                        "PasswordAdapter": "PasswordAdapter1"
                    }
                }
            ],
            "Active": true,
            "ApprovedState": "Approved",
            "StatusString": "Paid",
            "InitialUniqueName": "IRTEST123",
            "TimeUpdated": "2024-03-21T21:06:39Z",
            "UniqueName": "IRTEST123",
            "Name": "IRTEST123"
        }], 
 
"PageToken": "QUYzOEFRT06DTW9hNlVz"
}

 

 

Here's what I have in the pagination setup step(see image). In the rest step, I have a query parameter name "PageToken" and for the value I'm using the data pill pickers to get the pageToken variable in the pagination setup step.

 

 

In my execution details I'm only getting 1 page count even though I know I should be getting a couple of pages. Also I'm getting an empty pageToken in the execution details. Any Ideas what the issue could be?

Florian11
Kilo Sage

Hey @Oscar Mateos I have another question that I noticed in your screenshots. Did you use the data stream action in an import set, if so, how did you resolve the approval request object in the transform map?