Cant get order Connection field updated when copying Line items to a new Order

Can any Senior Pros assist me out. I m new to Javascripting.


Below is my code for Copying a Parent Child ( Order and Item List ) to a new Order.
Field 27 which is my Order Connection Field for copying all the the item list(child records) is not getting updated.


// CHANGE: the view key to your own view
$(document).on('knack-view-render.view_24', function (event, view, data) {

/* Add all of the fields you want copied and match them with detail data */


var duplicateData = {

field_29: data.field_29_raw, // totl
field_47: data.field_47_raw, // ordered by
field_26: data.field_26, //Date
field_18: data.field_18_raw, //PO

}

// Add a button to execute the action
$('#' + view.key).prepend('<button id="copy-button">Duplicate Record</button>');

// Add the duplicate function to the button we just added
document.getElementById('copy-button').addEventListener('click', function () {
Knack.showSpinner();
$.ajax( {

// Change object here to match your record's object #
// Replace XXX with your own Application ID and API Key found in your Knack Builder
url: 'https://api.knackhq.com/v1/objects/object_4/records/',
type: 'POST',
headers: {'X-Knack-Application-Id': 'XXXX', 'X-Knack-REST-API-Key':'XXXXX'},
data: duplicateData,
success: function(response) {
alert('Record Copied!');
Knack.hideSpinner();



var models = Knack.models['view_27'].data.models; //The list of Invoice Items to copy
var copieditems = []; //Variable to store them before Ajax

for(var i = 0; i < models.length; i++){ //start Item loop
copieditems = {

field_27: data.records[i].id, // Order Connection Field - FAILURE POINT

field_21: models[i].attributes.field_21_raw, //Item No
field_22: models[i].attributes.field_22_raw, //product
field_50: models[i].attributes.field_50_raw, //vendor
field_55: models[i].attributes.field_55_raw, //price
field_23: models[i].attributes.field_23, //Qty Used
field_28: models[i].attributes.field_28_raw, //subtotal

} //End of copied Items

$.ajax({
url: "https://api.knackhq.com/v1/objects/object_5/records/", //Must be a Table or List
type: "POST",
headers: {'X-Knack-Application-Id': 'XXX', 'X-Knack-REST-API-Key':'XXXXX'},
data: copieditems,
success: function (response) {
alert("Item Copied!"); //Locating here will alert every item
Knack.hideSpinner();

}
})


}

}
});
});
});

Hey Keerthi,

I believe this is caused by a couple of things.

First, view_24 appears to only contain a single record, but you are accessing trying to get the records attribute from it, which does not exist. Connection field values are also always stored as arrays, so you need to put your field value in an array like so: [my_connection_value]. Try replacing data.records[i].id with [data.id] and that should fix your issue.

More importantly, however, is that you are using your app's API key in your code. You should never put this in your app's JavaScript, as this allows anyone who comes across your app, whether logged in or not, to access all the records contained within it and do anything you can do in the builder. I strongly recommend switching your code to view-based requests, as outlined here: https://www.knack.com/developer-documentation/#view-based-requests

If you would like further help with this, switching to view-based requests, or anything else, feel free to contact me at david@hmnd.io! :)