Question

Help with a trigger on nested array string value

  • 11 January 2023
  • 4 replies
  • 208 views

I’m trying to setup a custom Zapier app to a CRM system with an existing API, to monitor “Jobs” for a change to a specific status value. But the value I need to poll on for the trigger is in a nested JSON array.

The JSON response from the API is as follows:

[
{
"id": 987654321,
"jobNumber": "12345",
"projectId": null,
"customerId": 4567890,
"locationId": 2345678,
"jobStatus": "Completed",
"completedOn": "2023-01-07T02:08:09.972Z",
"businessUnitId": 123,
"jobTypeId": 1098765,
"priority": "Urgent",
"campaignId": 3573028,
"summary": "This is a test",
"customFields": [
{
"typeId": 11111111,
"name": "Auto-Sync Status",
"value": "Hold Sync"
}
],
"notificationsEnabled": true,
"createdOn": "2023-01-06T21:40:18.547645Z",
"createdById": 905124,
"modifiedOn": "2023-01-11T01:40:08.5405171Z",
"leadCallId": null,
"bookingId": null,
"soldById": null,
"externalData": null
}
]

My custom code in the Zapier app trigger API endpoint code is:

const options = {
url: `https://api.url/${api_user}/jobs`,
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': access_token,
'App-Key': '12jod9823hdu293djoq9'
}
}

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

const jobs = results["data"].map((item) => {
return Object.assign(item, {
id: item["id"]
});
});

return jobs;
});

 

 

The string value I need to check/trigger on with Zapier is the customFields > value

How could I configure the custom code to read a user input value from the input designer, and return the jobs matching the input value?

 

Thanks :)


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

4 replies

Userlevel 7
Badge +14

Hi @KJA 

Good question.

Perhaps consider a different approach.

Use a Zap with a Webhooks Retrieve Poll trigger step: https://zapier.com/apps/webhook/help

Then follow that with a Filter step: https://zapier.com/apps/filter/help

 

Hi @KJA 

Good question.

Perhaps consider a different approach.

Use a Zap with a Webhooks Retrieve Poll trigger step: https://zapier.com/apps/webhook/help

Then follow that with a Filter step: https://zapier.com/apps/filter/help

 

Hi Troy,

Thanks for the suggestion.

I haven’t thoroughly looked into how webhooks work, but from first glance of attempting to configure a zap, I don’t think I can use webhooks as the URL/endpoint of the CRM API is dynamic and contains values set by the custom app oauth authentication/access tokens.

Userlevel 7
Badge +12

Hi @KJA 

Looks like you are nearly there! i think you are on the right track with your custom trigger I would suggest making the following changes. This code assumes that the customFields key always exists. 

 

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json; //these are the results of the api call

const jobs = data.filter(item => {
item.custom_fields = item.customFields.reduce((a, v) => ({ ...a, [v.name]: v.value}), {}); //this line turns the custom field array into an object. Always nice to give Zapier end users mappable fields over arrays. This outputs the custom field name and value.
if (item.custom_fields["Auto-Sync Status"]) {
//assuming this is the custom field you want to search
if (item.custom_fields["Auto-Sync Status"] == bundle.inputData.value) {
//assume you will add a user input field with a key 'value'. or you could hardcode the right side
return true
}
}
return false
})

return jobs;
});

 

Hi @KJA 

Looks like you are nearly there! i think you are on the right track with your custom trigger I would suggest making the following changes. This code assumes that the customFields key always exists. 

 

 

Thanks for this! I haven’t had a chance to go back to the app to test properly, but I was initially getting syntax errors inside Zapier code view on the “item.custom_fields = item.customFields.reduce((a, v) => ({ ...a, [v.name]: v.value}), {});” line