Universal function for Zapier action scripts

Hey everyone! I’m trying to streamline my Zapier scripts. Right now, I’m repeating the same function for different trigger keys. It looks like this:

TRIGGERKEY1_pre_custom_trigger_fields: function(bundle) {
  doSomething();
}
TRIGGERKEY2_pre_custom_trigger_fields: function(bundle) {
  doSomething();
}
TRIGGERKEY3_pre_custom_trigger_fields: function(bundle) {
  doSomething();
}

I was hoping to simplify it to something like:

all_pre_custom_trigger_fields: function(bundle) {
  doSomething();
}

Is there a way to create a catch-all function that runs for every action? I’ve looked through the docs but couldn’t find anything. Any ideas or workarounds? Thanks for your help!

I’ve encountered a similar issue in my Zapier projects. While the perform function is useful, it might not be the best fit for pre-trigger field operations. Instead, consider using a shared function approach. You could define a single function and reference it for each trigger key:

function commonPreTriggerLogic(bundle) {
  doSomething();
}

module.exports = {
  TRIGGERKEY1_pre_custom_trigger_fields: commonPreTriggerLogic,
  TRIGGERKEY2_pre_custom_trigger_fields: commonPreTriggerLogic,
  TRIGGERKEY3_pre_custom_trigger_fields: commonPreTriggerLogic
}

This method maintains the specific trigger key structure Zapier expects while reducing code duplication. It’s been quite effective in my experience, offering both clarity and efficiency.

I’ve found a neat workaround for this situation that’s been really useful in my Zapier projects. Instead of trying to create a universal function, you can use a higher-order function to generate the pre-custom trigger fields for each key. Here’s how I’ve implemented it:

function createPreCustomTriggerFields(doSomething) {
  return function(bundle) {
    doSomething();
    // Add any common logic here
    return bundle.inputData;
  };
}

const commonLogic = () => {
  // Your common logic here
};

module.exports = {
  TRIGGERKEY1_pre_custom_trigger_fields: createPreCustomTriggerFields(commonLogic),
  TRIGGERKEY2_pre_custom_trigger_fields: createPreCustomTriggerFields(commonLogic),
  TRIGGERKEY3_pre_custom_trigger_fields: createPreCustomTriggerFields(commonLogic)
};

This approach keeps the specific trigger key structure intact while allowing you to reuse the same logic across multiple triggers. It’s been a game-changer for me in terms of code maintainability and reducing repetition. Give it a shot and see if it works for your use case!

hey avamtz, ive been there! zapier can be tricky. have u tried using the perform function? it runs for all actions. might look like this:

perform: function(z, bundle) {
  doSomething();
  // rest of ur logic
}

this could save u some headaches. lmk if it helps!