Easy Cross domain API calls using code by Zapier (Get and Put) one call after another

//Knack's zapier integration lacks search and update. You can use this code to get around that.

//Find a record and update it using fetch in code by zapier.


var url = 'https://api.knack.com/v1/objects/object_15/records'; //update object #
var identifier = inputData.recordId; //get this from the first step of your zap
var date = inputData.completed_at;

//The date part doesn't work yet - need to reformat date string as an object - anyone??
var filters = [{
field: 'field_313', //This is a unique field that identifies the record.
operator: 'is',
value: identifier
}];
var date = date;
var get_url = url + '?filters=' + encodeURIComponent(JSON.stringify(filters));
var data = { field_487: "Signed", field_312: date,
}; //change field #'s and values to whatever payload you want in the put request

fetch(get_url, {
headers: {
'X-Knack-Application-ID' : 'Your ID',
'X-Knack-REST-API-Key' : Your Key',
},
}).then(function(resp){
if(resp.ok) return resp.json()
return resp.text().then(function(t){
throw JSON.stringify({
url: get_url,
filters: filters,
body: data,
statusCode: resp.status,
resptext: t
}, null, 2)
});
}).then(function(data){
return Promise.all(data.records.map(function(r){
return fetch(url + "/" + r.id, {
method: 'put',
body: JSON.stringify(data),
headers: {
'X-Knack-Application-ID' : 'Your App ID',
'X-Knack-REST-API-Key' : 'Your App Key',
'content-type': 'application/json'
},
}).then(function(resp){
if(resp.ok) return resp.json()
return resp.text().then(function(t){
throw JSON.stringify({
url: url + "/" + r.id,
body: data,
statusCode: resp.status,
resptext: t
}, null, 2)
});
})
}))
}).then(function(j){
callback(null, j);
})
.catch(function (error) {
callback(error);

});