Flow Designer - Custom Action to Parse the JSON response and insert in custom Table

geet
Tera Guru

Hi All,

I have created a custom action as follows where everything is fine, I am getting the response in the execution details and status code as 200 but the data is not inserted in the custom table.

geet_0-1695943540151.png

Here is my execution glimpse:

geet_1-1695943580150.png


I am not sure why it is not inserting the data in the table.
Also, is script step the correct step? or I should use JSON parser step.
Kindly guide with the best practice.

1 ACCEPTED SOLUTION

@geet Can you try the following code and let me know if it works for you.

 

var jsonString = '{"data":[{"id":"rs-4ebd12446515b108","name":"jumpbox dev","tags":[787025,787027,787029],"typeAlias":"ssh","typeName":"ssh","hash":"a276a08591a55f2ae17718875d866b9e"},{"id":"rs-6d44ec9d651473a7","name":"dev cluster","tags":[787025,787026],"typeAlias":"aurora-postgres","typeName":"postgres","hash":"1236edc93afab533de3123eee4297d87"},{"id":"rs-771cc8536515b2b3","name":"jumpbox prod","tags":[787027,787028,787029],"typeAlias":"sshCert","typeName":"sshCert","hash":"8ddea064d0d8974698db7d6234f5a1bc"},{"id":"rs-7f69cf376504c5d5","name":"dogfood db","tags":[787025],"typeAlias":"postgres","typeName":"postgres","hash":"f0bece9698a2ac23a123ed477c637fa8"}],"totalCount":4,"pageInfo":{"cursor":null,"hasNextPage":false}}';

var jsonObj = JSON.parse(jsonString);

for (var i=0;i<jsonObj.data.length;i++) {
    var id = jsonObj.data[i].id;
    var name = jsonObj.data[i].name;
	gs.info("ResID " +id);
	gs.info("RESName " +name);
	
	
}

Hint: In your for loop you are running the loop upto the length of your JSON object whereas it should run upto the length of nested data object.

 

Hope this helps.

View solution in original post

6 REPLIES 6

JohnnySnow
Kilo Sage

Hi @geet ,

 

Can you try printing the values before insert and check if the data is proper. Also, make sure you are inserting the mandatory fields if any with the above fields.

 

Thanks

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

DYCM
Mega Sage

Hi @geet ,

I would suggest to check if it has the privilege to access that table (x_stdm_access_mgmt_resources) from this flow designer's application scope. Please check logs right after the flow designer is executed, you should see some error/warning messages there if my guess is correct. 

 

Community Alums
Not applicable

Hi Geet,

 

The code seems to be correct. You might want to check following things:

  1. Check the Application Access in custom table record and it should allow to create
  2. Check if any ACLs are interfering in creation of record (Run this script in background script from same scope of flow)
  3. Check if there are any Data Policy defined on the table
  4. Check if the user from which you are trying to run FLOW has access

Please let me know if the issue still persists.

 

Please mark this comment as Helpful/Correct Answer if it helped you.

 

Cheers,

Hardit Singh

geet
Tera Guru

Hi @JohnnySnow @DYCM @Community Alums @Community Alums 

I have tried what you all suggested and nothing worked for me.
Hence I tried to check the code through fix script and by picking the responsebody from execution details of flow designer.
I wrote the below fix script and I see that the for loop has an issue but I don't understand why it is creating issue:

var jsonString = '{"data":[{"id":"rs-4ebd12446515b108","name":"jumpbox dev","tags":[787025,787027,787029],"typeAlias":"ssh","typeName":"ssh","hash":"a276a08591a55f2ae17718875d866b9e"},{"id":"rs-6d44ec9d651473a7","name":"dev cluster","tags":[787025,787026],"typeAlias":"aurora-postgres","typeName":"postgres","hash":"1236edc93afab533de3123eee4297d87"},{"id":"rs-771cc8536515b2b3","name":"jumpbox prod","tags":[787027,787028,787029],"typeAlias":"sshCert","typeName":"sshCert","hash":"8ddea064d0d8974698db7d6234f5a1bc"},{"id":"rs-7f69cf376504c5d5","name":"dogfood db","tags":[787025],"typeAlias":"postgres","typeName":"postgres","hash":"f0bece9698a2ac23a123ed477c637fa8"}],"totalCount":4,"pageInfo":{"cursor":null,"hasNextPage":false}}';




var jsonObj = JSON.parse(jsonString);
gs.info("RESJSON"+jsonObj);
for (var i=0;i<jsonObj.length;i++) {
    var id = jsonObj.data[i].id;
    var name = jsonObj.data[i].name;
	gs.info("ResID " +id);
	gs.info("RESName " +name);
	
	
}
//     var gr = new GlideRecord("x_stdm_access_mgmt_resources");
//     gr.initialize();
//     gr.resource_id = id;
//     gr.resource_name = name;
//     gr.insert();


Logs of this script:

geet_0-1696008099194.png

Now if I comment the for looop:

var jsonString = '{"data":[{"id":"rs-4ebd12446515b108","name":"jumpbox dev","tags":[787025,787027,787029],"typeAlias":"ssh","typeName":"ssh","hash":"a276a08591a55f2ae17718875d866b9e"},{"id":"rs-6d44ec9d651473a7","name":"dev cluster","tags":[787025,787026],"typeAlias":"aurora-postgres","typeName":"postgres","hash":"1236edc93afab533de3123eee4297d87"},{"id":"rs-771cc8536515b2b3","name":"jumpbox prod","tags":[787027,787028,787029],"typeAlias":"sshCert","typeName":"sshCert","hash":"8ddea064d0d8974698db7d6234f5a1bc"},{"id":"rs-7f69cf376504c5d5","name":"dogfood db","tags":[787025],"typeAlias":"postgres","typeName":"postgres","hash":"f0bece9698a2ac23a123ed477c637fa8"}],"totalCount":4,"pageInfo":{"cursor":null,"hasNextPage":false}}';




var jsonObj = JSON.parse(jsonString);
gs.info("RESJSON"+jsonObj);
//for (var i=0;i<jsonObj.length;i++) {
    var id = jsonObj.data[0].id;
    var name = jsonObj.data[0].name;
	gs.info("ResID " +id);
	gs.info("RESName " +name);
	
	
//}
//     var gr = new GlideRecord("x_stdm_access_mgmt_resources");
//     gr.initialize();
//     gr.resource_id = id;
//     gr.resource_name = name;
//     gr.insert();

Here is the log: It is successfully print the values:

geet_2-1696008286847.png

Could someone please help me understand why for loop is a problem and how to fix it.

P.S: This payload is currently small because I am just testing it. I am going to have a larger payload than this.
Please help me and guide me on how to parse this payload to my custom table.
The flowdesigner , tables everything is in scoped app.

Just for reference, I am sharing the table picture:

geet_3-1696008452432.png