Insert a Connected Record via API

Does anyone have any experience inserting a new record via an API, whereby the new record is connected to another object's record? For example Object A has a 'phone number' field. An API call then writes a new record to Object B that also includes a matching 'phone number' field. I need the new record in Object B to be connected to the record in Object A based on the matching 'phone number' fields in both Objects. Anyone know how to accomplish this? Any assistance would be appreciated.

Hi there,

Using javascript you would need to query the connected object for the phone number with one ajax call, if found the callback will include the record.Id (GUID) for that record, In the response, call another function which would PUT that record.Id to the connection field of the record you want updated. 

 

Here is an example of a 2 stage api call in knack:

 

//LIV CODE for Payperiod match:

 

//One Request after Another
$(document).on('knack-record-create.view_297', function (event, view, record) {

var newRecord = record.id;
lookupValue = record.field_341;
var UrlBase = "https://api.knack.com/v1/pages/page_280/views/view_499/records";

var filters = [{field: 'field_461',operator: 'is',value: lookupValue}];
/*
var filters= [{
field: field_461, //Field with date to match
operator: 'is',
value: lookupValue
}];
*/
console.log("new record is : ", record.id);
console.log("url base is : ", UrlBase);
console.log("Filters are : ", filters);
console.log("lookup value is : ", lookupValue);

var get_url = UrlBase + '?filters=' + encodeURIComponent(JSON.stringify(filters));

console.log(get_url);
//doing GET request

$.ajax({
url: get_url,
type: 'GET',
headers: {
'Authorization': Knack.getUserToken(),
'X-Knack-Application-Id': Knack.application_id,
'Content-Type': 'application/json'
},
success: function(data) {
console.log('Got records!');
console.log("data is: ", data);
put_url = 'https://api.knack.com/v1/pages/scene_275/views/view_494/records/' + record.id;

console.log("put Url is: ", put_url);

var myvar = data.records[0].field_460_raw[0].id;
console.log(" myvar is: ", myvar);
put_data = { field_477: myvar, field_338: record.id
};

$.ajax({
url: put_url,
type: 'PUT',
headers: {
'Authorization': Knack.getUserToken(),
'X-Knack-Application-Id': Knack.application_id,
'Content-Type': 'application/json'
},
data: JSON.stringify(put_data),
//data: put_data,

success: function () {
console.log("Updated");

}

});

}
});
});

You could do this with zapier or integromat in a fraction of the time by just dragging and dropping fields around.

Hope that helps.

Justin