Best answer

How to know that Search has returned no results, to then automatically call a Create action

  • 16 June 2021
  • 1 reply
  • 79 views

Userlevel 1

I have a search function that I am using Custom code on. I have set the option to “Enable Create if not found”

 

Yet because I am using a custom code on the search I think zapier is not detecting that nothing has been found. How do I make search the function knows there are no results and therefore trigger the create?

 

Here is my code:

const options = {
url: `${bundle.authData.url}/search/Candidate?query=email:${bundle.inputData.email}&fields=id&count=1`,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${bundle.authData.access_token}`
},
params: {

}
}

return z.request(options)
.then((response) => {
response.throwForStatus();

const results = response.json;

// You can do any parsing you need for results here before returning them

return [results];
});

Here are my results that come back when there are no results. 

 

{"total":0,"count":0,"start":0,"data":[]}

 

icon

Best answer by ikbelkirasan 17 June 2021, 00:02

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.

1 reply

Userlevel 7
Badge +12

@cozza13 You’ll need to return an empty array to signal that nothing was found. Here’s how to do it in your case:

const options = {
url: `${bundle.authData.url}/search/Candidate?query=email:${bundle.inputData.email}&fields=id&count=1`,
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${bundle.authData.access_token}`,
},
params: {},
};

return z.request(options).then((response) => {
response.throwForStatus();

const results = response.json.data;
return results;
});