Skip to main content
Best answer

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

  • June 16, 2021
  • 1 reply
  • 86 views

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":[]}

 

Best answer by ikbelkirasanBest answer by ikbelkirasan

@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;
});

 

View original
Did this topic help you find an answer to your question?
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

ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • Answer
  • June 16, 2021

@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;
});