-
Goal: Trigger a Lambda function when a button is pressed, passing a dynamic payload based on the contents of form fields
-
Steps: Create a Lambda query. Add necessary form fields and button. Add a custom script that builds the required payload and triggers the query. Configure the button to trigger the query when pressed.
-
Details: I'm trying to create an application in Retool that includes a simple form and a custom (JS) script. There's also a Lambda integration to a specific function as a query. The user would input data into the form and then the script would compile this into a payload, trigger the Lambda function sending the payload to it. I can trigger the Lambda function successfully using the 'query.trigger()' but I'm having an issue figuring out how to send the dynamic payload. How can this be achieved?
Hi @Ollie_F - welcome to the forums. Can you give a little more context to the "dynamic payload" and what makes it dynamic / what challenge you are having with it? Can you add an "additional scope" variable to the lambda function that you pass the payload to when you trigger the function?
Hi @jg80
Thanks for your response.
Regarding the payload, by dynamic I mean that its contents are based on what the user enters into the form and this could be different each time the form is submitted. For example
let payload = {
attribute1: textArea1.value,
attribute2: textArea2.value
}
query1.trigger({
additionalScope: {
functionPayload: JSON.stringify(payload)
},
onSuccess: function (data) {
console.log("Successfully run!", data);
},
onFailure: function (error) {
console.log(error)
},
});
Here query1
refers to the Lambda function query. This does trigger the function, however, logging the event payload in the function shows nothing at all so I'm wondering whether there's a different way to pass the payload to the function at trigger time?
Thanks
A quick and dirty test seemed to work fine with your code:
But the query I am triggering is just a simple one:
// functionPayload is set as an additional scope variable
console.log(functionPayload)
return functionPayload
How are you triggering the script you provided when you click the button? Also maybe add console.log(JSON.stringify(payload))
to the script prior to the trigger so that you can see what should be passed.
Okay, I see. It seems that the query you're triggering is another JS query, whereas the query I'm triggering represents a Lambda function, that's AWS Lambda.
Right, but same concept. Presumably query1 is an API call to AWS Lambda with an additional scope variable called functionPayload
that is incorporated into the call in some way? Simplest mockup I can think of like this:
Note: you have to put {{functionPayload}} somewhere otherwise you won't be passing anything. I put it in the body, but just as easy to put it in the headers, URL params, etc.
The payload was passed to the (admittedly overly simple) API