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