API - Post - Error when posting data

It is currently returning the below (I have limited it to 2 for testing):
note, I have erased part of the telephone numebrs as these are customer detials

image

Please also see an image of the state pane below:

Sorry, also forgot to add - MobileNumbers.data.MobileNumber returns ["07123456789","01234567891"]

Thanks again :slight_smile:

Ah, cool!

Then I think you may just have had an extra set of array brackets in your code.

Would you mind trying this?

{{ MobileNumbers.data.MobileNumber.map(str => Object.assign({"number": str})) }}

1 Like

You and Dave are both wizards!

Using this along like {{JSON.stringify({data:MobileNumbers.data.MobileNumber.map(str => Object.assign({"number": str}))})}} works perfectly.

There is one final piece to this puzzle however, the results returned from the api are:

{
"number": "07123456789",
"result": "safe"
},
{
"number": "01234567891",
"result": "safe"
}
],
"reference": "63d84176c9730",
"valid_until": "2023-02-27T22:15:18Z",
"matches": 0,
"credits": 2
}

I now want to send these results to another table in the database, I am trying something like the below:

but I dont get any data back from either of the below:

{{PostTo121ProData.data.number}}
{{PostTo121ProData.data.status}}

any advice how I would take this response and insert them into the table?

Many thanks,

Michael :slight_smile:

Woohoo!

In your screenshot, do you want to pass along the first number and the first result? Or both numbers and both results?

It seems like your api is returning

[{
"number": "07123456789",
"result": "safe"
},
{
"number": "01234567891",
"result": "safe"
}
],
"reference": "63d84176c9730",
"valid_until": "2023-02-27T22:15:18Z",
"matches": 0,
"credits": 2
}]

which is an array of objects.

To grab the first number, for example, we could do {{PostTo121ProData.data[0].number}}

1 Like

Woohoo indeed! :smiley:

Ideally, I would like to insert all the numbers returned along with the valid_until date.

So each row inserted would be like Number, Result, Valid Until

The column names which I would insert to are PhoneNumber, Result and ExpiryDate so the number would go into PhoneNumber, Result into Result and Valud Until would go into Expiry Date.

Hope I'm making sense.

Many thanks,

Michael.

Makes sense! So you'd like to loop through your results and pass each result into another query? Something like this? http://community.retool.com/t/how-to-run-a-rest-api-query-for-each-item-in-an-array-and-return-all-results-together/2352/4

Ahh, I've done this before. Is there a way to bulk insert everying in one go for speed?

Only if your API is built to handle a bulk insert! If your API can handle something like this (an array of values for each key), then we can definitely get it formatted to bulk insert in one go :slight_smile:

This is being written to MS SQL. I can bulk insert directly from SSMS, so I assume I can do the same in Retool? Let me know, and I'll give it a try.

I also realised it is in 'Insert a record' for action type. This should be bulk insert records I believe?

This is the current data structure of the returned data:

And I assume I need either transform the data before inserting or can I do something here?

Many thanks,

Michael.

Ah! Cool. Bulk insert is definitely the action you're looking for.

We just need to pass in an array of objects with the correct keys. Would you mind sharing a screenshot of what PostTo121ProData.data looks like? Ideally from the left panel's State tab:

Sure, is this what you're looking for? :smiley:

Yes!! Thank you.

So it looks like {{ PostTo121ProData.data.data }} will grab us an array of these two objects:

which is exactly what a bulk insert will accept in the "Array of records to insert" field!

Let me know how {{ PostTo121ProData.data.data }} works for you.

I got the below error:

the column is PhoneNumber - do I need to transfrom it somehow?

Yes! Something like this:

{{ PostTo121ProData.data.data.map(({number, result}) =>({PhoneNumber: number, result:result})) }}

should work?

1 Like

And just like that...it works!!!

If there any way of getting the valid_until value from 121ProData and use this as part of the insert so we can get the ExpiryDate part populated on the table too?

Thank you so much for all your support. You don't realise how much I appreciate this.

Always happy to help :slight_smile:

Could you try this? Note: I’m on mobile and walking to my train home, so I haven’t fact-checked my work :sweat_smile:

{{ PostTo121ProData.data.data.map(({number, result}) =>({PhoneNumber: number, result:result, ExpiryDate: PostTo121ProData.data.valid_until})) }}

1 Like

Hi Victoria,

Sorry for the delay in my reply.

The above worked fine, with a minor edit to the date format, so good work whilst walking to your train home! :laughing:

I have been working through an issue today on this project, now that the above worked - I tested it with a large volume of data. Whilst it works, there is an issue with the Bulk Insert function having too many parameters, so I will need to 'batch' the uploads.

I have been testing this:

const batchSize = 50;
const batchedData = _.chunk(DataPreview.data, batchSize);
batchedData.forEach(databatch => BulkInsert.trigger({additionalScope: {databatch}}));

which works, but I need it to trigger onSuccess: DateClean.trigger();

after the last batch has been completed. I am trying to work out how to make it only trigger this DateClean query after the last batch has been processed.

Any guidance would be greatly apprecaited.

Many thanks,

Michael.

Hey Michael! Top of the morning :sun_with_face:

What about an On Success event handler attached to your batch query?

Hi Victoria,

Got it, I've made it dynamic based on the number of rows and it's working as expected.

Thank you again for all your and Dave's support. You've both been great!

Have a nice day! :slight_smile:

2 Likes