# Javascript/Ajax update a connected field

**URL:** <https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569>\
**Category:** API & Custom Code\
**Created:** [August 27, 2016, 1:21pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569 "2016-08-27T13:21:11Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteinarSta96433](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@SteinarSta96433](https://forums.knack.com/u/SteinarSta96433)\
**Post date:** [August 27, 2016, 1:21pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/1 "2016-08-27T13:21:11Z")

</div>

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?

 ![](https://s3.amazonaws.com/cdn.freshdesk.com/data/helpdesk/attachments/production/5070075332/original/unnamed.jpg?1472303962)

 ![](https://s3.amazonaws.com/cdn.freshdesk.com/data/helpdesk/attachments/production/5070075361/original/unnamed%20(1).jpg?1472304049)

---

<div class="post-metadata">

**Author:** ![Justin11773](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@Justin11773](https://forums.knack.com/u/Justin11773)\
**Post date:** [August 25, 2017, 8:22pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/2 "2017-08-25T20:22:04Z")

</div>

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](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/](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");

}

});  
   
}  
});  
});

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [November 3, 2016, 9:41pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/3 "2016-11-03T21:41:21Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [November 3, 2016, 9:33pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/4 "2016-11-03T21:33:16Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [November 3, 2016, 9:25pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/5 "2016-11-03T21:25:22Z")

</div>

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
}

...
```

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [November 3, 2016, 9:21pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/6 "2016-11-03T21:21:46Z")

</div>

Its the connection field to object\_1

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [November 3, 2016, 9:20pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/7 "2016-11-03T21:20:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [November 3, 2016, 9:19pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/8 "2016-11-03T21:19:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [November 3, 2016, 9:08pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/9 "2016-11-03T21:08:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [November 3, 2016, 9:02pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/10 "2016-11-03T21:02:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [November 3, 2016, 8:54pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/11 "2016-11-03T20:54:23Z")

</div>

Thanks! Updated and very much appreciated.

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [November 3, 2016, 8:51pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/12 "2016-11-03T20:51:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [November 3, 2016, 8:47pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/13 "2016-11-03T20:47:03Z")

</div>

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();
}
});
});
```

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [November 3, 2016, 6:38pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/14 "2016-11-03T18:38:39Z")

</div>

## 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](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](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  
   
 })

})

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [November 3, 2016, 6:38pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/15 "2016-11-03T18:38:01Z")

</div>

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\*

---

<div class="post-metadata">

**Author:** ![undefinedu16608](https://avatars.discourse-cdn.com/v4/letter/u/9f8e36/32.png) [@undefinedu16608](https://forums.knack.com/u/undefinedu16608)\
**Post date:** [November 3, 2016, 5:37pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/16 "2016-11-03T17:37:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [August 30, 2016, 7:40pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/17 "2016-08-30T19:40:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SteinarSta96433](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@SteinarSta96433](https://forums.knack.com/u/SteinarSta96433)\
**Post date:** [August 30, 2016, 6:36pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/18 "2016-08-30T18:36:50Z")

</div>

Yes, field\_128 is not in Object 1 but Object 4.

---

<div class="post-metadata">

**Author:** ![BennetSt.L83204](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@BennetSt.L83204](https://forums.knack.com/u/BennetSt.L83204)\
**Post date:** [August 30, 2016, 3:30pm UTC](https://forums.knack.com/t/javascript-ajax-update-a-connected-field/7569/19 "2016-08-30T15:30:52Z")

</div>

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