Javascript/Ajax update a connected field

Hi, I've managed to use the CheckBox on tables and when pressed it updates fields in the same Object, but I can't make it update a connected field in another Object.


I think it has something to do with Ajax not posting to multiple URLS?

Any suggestions how to update the other Object?



Here's a modification of this code I just put together to lookup a value on a connected object, return a connection field and copy it to your newly created record.. It's handy for calendar popups to (when form rules are not available)

//Upon creation of a record (trigger record)
//1. search for the matching date on another object,
//2. return the record.id of a third object connected to the matched record
//3. Update our trigger record with the matched records' connection


$(document).on('knack-record-create.view_297', function (event, view, record) {


lookupValue = record.field_341; //This is the date entered
var UrlBase = "https://api.knack.com/v1/pages/page_280/views/view_499/records";

var filters = [{field: 'field_461',operator: 'is',value: lookupValue}]
//look for matching date in your table reference object


//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
//the record id is nested in an array inside an array
//even though the first array has only one record in it
//console.log(" myvar is: ", myvar);

put_data = { field_477: myvar, field_338: record.id
};

//also recording our newly created record's record id into a field because -why not?

//make put request

$.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),

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

}

});

}
});
});

Nope - it actually worked - you only missed the comma after cust_conn but I figured that out :) thanks! this is awesome! Just haven't confirmed if all is working as Knack is super slow today (at least here in EU)

Ehh I probably messed up writing this from memory: Just went back and looked at my code that does a similar thing.

At the very least I think its more messy than it needs to be:

Delete the line:

var cust_conn = [data.records[i].id]

And Change:

field_104: cust_conn, //Customer connection


To be just:

field_104: data.records[i].id, //Customer connection

Ok got - now I see where we're going with this -

Try this (full data code posted) since making a couple little changes:

for (var i = 0; i < data.total_records; i++) {//alert(data.records[i].field_1);
//Added line below, rename variable as you see fit:
var cust_conn = [data.records[i].id]
var newdata = {
field_99: i + 1,
//changed line below
field_104: cust_conn, //Customer connection
field_102: data.records[i].field_25_raw,
field_97: record.field_97,
field_101: record.field_101_raw[0].id,
field_108: data.records[i].field_107_raw
}

...

Its the connection field to object_1

Ahh I think I see - what type of field is Field 104 on Object 15?

This did not work. I know the GET is working because I created field_107 in object_1 with a text formula that makes it equal to field_1 and then copy it to object_15 dropping it on field_108 and it works but I need to populate the connection... thanks

If field_1 on object 1 is just a normal text field (not a connection or address field, or other like ones) - then you shouldn't need anything of the _raw[0].id stuff at the end.

Should just be:

field_104: data.records[i].field_1,

Generally the only time you need to the _raw stuff is for fields like, connections, or Address, time/date,etc that have multiple pieces of information stored within them. (Ex Date Field: 11/03/16 , 8 hours, 55 minutes, AM) - Can't remember off the top of my head exactly how Knack breaks it out so that probably not correct, but you get the idea.

Exactly, that line is the Problem.

You are spot on, field_1 is the Display Field for Object 1 and it is a normal text field which was retrieved via the GET request.

Thank you

Thanks! Updated and very much appreciated.

I'll look it over - FYI - you left your App ID and Key towards the bottom - recommend editing your post.

Thanks Bennet,

Very much appreciated, I think I understand what you posted (learning code here) but I am not sure it is the best way to address my problem, I am sharing what I have bellow hoping you can give me an extra nudge in the right direction. Result of bellow is that all fields are copied from Object 1 to Object 15, except field 104, which remains blank because its a connection field to Object 1.

I understand this is because knack expects an Object. I believe (not sure) that this means that we have to POST a JSON Object instead of the raw data (please correct me if I am wrong).

Also, I can see from your second example that you use PUT instead of POST.

As I want to copy all the fields I do not see the need to apply filters (or am I missing something?)

So my conclusion is that I only need to POST JSON, which I am guessing is done via JSON.stringify ?

Again, many thanks for sharing!

$(document).on('knack-record-create.view_94', function(event, view, record) {

Knack.showSpinner();

var KnackAppID = "xxxx"
var KnackAPIKey = "xxxxx"

//Get Customer
$.ajax({url: 'https://api.knackhq.com/v1/objects/object_1/records', type: "GET",
headers: {"X-Knack-Application-Id": KnackAppID, "X-Knack-REST-API-Key":KnackAPIKey},
success: function(data) { //alert('Records Retrieved!' + data.total_records);

//Loop through data, build newdata and insert new record into Customer Group
for (var i = 0; i < data.total_records; i++) {//alert(data.records[i].field_1);
var newdata = {
field_99: i + 1,
field_104: data.records[i].field_1_raw[0].id, //Customer connection
field_102: data.records[i].field_25_raw,
field_97: record.field_97,
field_101: record.field_101_raw[0].id,
field_108: data.records[i].field_107_raw
}

//Add data to CustomerGroup
$.ajax({
url: "https://api.knackhq.com/v1/objects/object_15/records/",
type: "POST",
headers: {'X-Knack-Application-Id': 'xxxx', 'X-Knack-REST-API-Key':'xxxxx'},
data: newdata,
//success: function(response) {alert('Records Added!');}
});
}
Knack.hideSpinner();
}
});
});

One Request after Another

$(document).on('knack-record-create.view_6', function (event, view, record) {

first_post_data = {
//Whatever data you need to post to the First table
}

$.ajax({
url: 'https://api.knackhq.com/v1/objects/object_5/records',
type: 'POST',
headers: {
'X-Knack-Application-Id': app_id,
'X-Knack-REST-API-Key': app_key
},

data: first_post_data

})

second_post data = {
//Whatever data you need to post to the second table
}
$.ajax({
url: 'https://api.knackhq.com/v1/objects/object_8/records',
type: 'POST',
headers: {
'X-Knack-Application-Id': app_id,
'X-Knack-REST-API-Key': app_key
},

data: second_post_data

})

})

Depending on what your doing; if its just using the same data and posting to 2 different spots, you should just be able to run one right after the other:

You can also do a GET request and then turn around and use that data to another API call.

I will post examples in separate posts, as the code snippet option appears to be gone with this new format of Knacks. *sad*

I have the same issue, can you share an example of how you combine the 2 API calls in the same code? Much appreciated, thank you

Then yes, you would need to do a second API call to object 4 with the record ID that you're wanting to update to update that field.

Yes, field_128 is not in Object 1 but Object 4.

Just to make sure I understand - that field_128 is not in Object 1, correct?