简体   繁体   中英

How do I add this JavaScript code to Zapier, to issue a 3rd party gift?

My eCommerce website needs to run JavaScript code so a 3rd party can send the customer a gift. The JavaScript code worked on the Thank You page, but I need to have a few-day delay before the script runs, so I'm trying to do this with Zapier instead. But Zapier is giving the following error when the same code is used there:

The run javascript could not be sent to Code by Zapier. SyntaxError: Unexpected token <

Zapier Support could not help, and I couldn't find the solution in Zapier's documentation.

Can someone please help modify the short code below, so that it can be run by Zapier?

 <script src="https://members.thirdparty.com/jsapi/fbDE_Voucher.min.js"></script> <script> window.onload = function(){ var fdDE_detail = { "fbDE_sender":'12345-67890', "fbDE_fullname":"'"+inputData.CustName+"'", "fbDE_email":"'"+inputData.CustEmail+"'", "fbDE_amount":inputData.VoucherValue, "fbDE_business":54321, "fbDE_message":'Thank you for your purchase!' } fbDEVoucher(fdDE_detail); } </script>

The variables CustName, CustEmail, and VoucherValue are from prior Zapier steps. The first 2 need to be within single quotes, and the third is an integer (not requiring quotes). Please see image below:

JavaScript code for Zapier

Does anyone know how to fix the above error (in bold) so this code runs?

Appreciate your help! Thanks.

Zapier's code runs in Node.js , which is a different environment than the browser-based JS your code was running in before.

First off, <script> tags aren't available, since that's HTML. Whatever code comes in through fbDE_Voucher.min.js will need to be loaded separately. If it's not too long, you can paste it into the code window (but there's a better solution, see below).

There's also no window global, but you don't need it anyway. So to get this close to working, it would be something like:

// doesn't work, fbDEVoucher is undefined
fbDEVoucher({
  fbDE_sender: "12345-67890",
  fbDE_fullname: inputData.CustName, // doesn't need to be re-wrapped in a string
  fbDE_email: inputData.CustEmail,
  fbDE_amount: inputData.VoucherValue,
  fbDE_business: 54321,
  fbDE_message: "Thank you for your purchase!",
});

That's all you would need, assuming your function is already defined.


The best option for you here is to create a custom Zapier integration. This gives you a more full-featured JS environment than a Code by Zapier step, plus dramatically more control. You can include the aforementioned file and call the function as needed. There's docs about that here: https://platform.zapier.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM