Runs your custom action when an automation is triggered.
Wix calls this function when an automation is triggered, for example by a form submission or a completed order. Therefore, write
any code for your action in invoke()
. Use the Velo APIs to create your
action in the same way as you use them in your site's page code.
For example, let's say you want to save a site visitor's contact data to a collection when they submit a form. In this case,
Wix calls invoke()
when the form is submitted. Write the code to save the
contact data, using the relevant wix-data methods, in the invoke()
function.
invoke()
accepts the trigger payload as a parameter. This payload changes depending on what your trigger is. You can access the payload
fields and use them in your action code.
function invoke(options: Options, context: Context): Promise<object>;
The payload received from the trigger.
Metadata about the request.
import wixData from "wix-data";
export const invoke = async ({ payload }) => {
const toInsert = {
plan_id: payload.plan_id,
plan_name: payload.plan_title,
name: payload.contact.name,
email: payload.contact.email,
phone: payload.contact.phone,
};
// Insert the customer data into our site collection
// of contact data for plan purchasers
try {
const results = await wixData.insert("CustomerData", toInsert);
console.log("Data inserted successfully:", results._id); // Log the ID of the inserted item
} catch (error) {
console.error("Error inserting data:", error);
// Handle the error
}
return {}; // The function must return an empty object
};
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.