Best answer

When using Callback, Output Not Defined when Callback not called

  • 13 October 2020
  • 9 replies
  • 1046 views

Userlevel 2

When the IF statement allows the callback I have no issues, but if the IF statement is false and proceeds to the ELSE then I get “Output not defined”. I have also tried without the ELSE. Same thing, says output not defined.

IN CASE BELOW IF estimateid.length is greater than 50 the Callback works, if it is less than 50 then I get the error shown below. I have tried any type of output even the basic hello world, same thing.

 

if (inputData.estimateid.length > 50) {

callback(null,{hello: "world"})

}

else {

output = {id: 1234, estimatelength: inputData.estimateid.length};

}

 

error

 

 

icon

Best answer by CraigNet3 14 October 2020, 20:27

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

9 replies

Userlevel 7
Badge +10

Mod note: I’m moving this to “Developer Discussion”.

Userlevel 7
Badge +8

Thanks @AndrewJDavison_Luhhu - Looks like this question is about Code by Zapier app. No worries though! I’ve moved it back to the right spot so we can get more attention on it. Thanks for the help!

Userlevel 4
Badge +4

@ITplumber when you use “output” it has to be the last statement in the code. If you use return you are more flexible. Example:

if (inputData.estimateid.length > 50) {
return {hello: "world"};
} else {
return {id: 1234, estimatelength: inputData.estimateid.length};
}

Try that and see if it works.

 

Userlevel 2

I have tried 

@ITplumber when you use “output” it has to be the last statement in the code. If you use return you are more flexible. Example:

if (inputData.estimateid.length > 50) {
return {hello: "world"};
} else {
return {id: 1234, estimatelength: inputData.estimateid.length};
}

Try that and see if it works.

 

 

, but I need the callback in there to break the task and that is where the problem is.

output/return issue with callback in code.
 

 

Userlevel 4
Badge +4

@ITplumber that’s no problem. All we have to do is remove the “else”. Here you go:

if (inputData.estimateid.length > 50) {
return {hello: "world"};
}
return {id: 1234, estimatelength: inputData.estimateid.length};

That should work. The else is not needed since execution stops if the condition is true.

Userlevel 2

But the task will go on to the next step in both cases, which would use up more Action Steps. That is the main problem I am trying to save on Action steps. I don’t just need the execution stopped I need the entire Zap stopped if the IF statement is TRUE.

 

Sorry, I am knew to JavaScript and Code by Zapier. I can get the code to work in every example except when I used CALLBACK() INSIDE OF IF()

Userlevel 4
Badge +4

@ITplumber if you use callback in this way you won’t get the desired result. It’s supposed to be used for asynchronous execution. If you’d like to test for a condition and then stop execution based on that condition then follow these 2 steps:

1. Use a Code step like you are doing now to evaluate the condition and set a variable based on the result of the condition. Example:

if (inputData.estimateid.length > 50) {
return {continue: false};
}
return {continue: true};

2. Next you add a filter step and filter for “continue” is true:

 

In doing so only if the condition is true will the Zap continue. If not it will filter out and stop execution. Does that work?

Userlevel 2

Thank you. That explains it well. Unfortunately it will still use the extra filter Step I am trying to avoid because this is an expensive Zap. But thank you for the good explanation.

Userlevel 4
Badge +4

@ITplumber you won’t be charged a task for a filter that doesn’t pass. So no need to worry.