Copy an Invoice (parent) and Items (children)-Nesting Alerts and Ajax

Objective: For myself to understand where to nest the Ajax and Alerts in order to pass Variables from one to another.

I greatly appreciate every post. It is invaluable to me.

This is very basic as I am not very proficient at script. After hours of reading genius posts, I finally assembled some code that rips quickly through an Invoice style document copy with 100% View Based code.

It does not contain proper error capture or user alerts but it may help someone like myself with the critical placement of your {,',",), etc...

// Start-Copy Invoice and Details Table Item to New Quote Order

// Open Parent Record Details View with an Items List Table
$(document).on("knack-view-render.view_2647", function (event, view, data) { //The Invoice data view
// Get data from current view
var oldwostring = data.field_363; //Not critical variable for use later
var womunbersonly = oldwostring; //Not critical
var requestnote = "Quote created for: "; //Added description to concatenate a field
var substatus = "Quote Requested";
var duplicateData = {
field_363: womunbersonly + "Q", // Use var vs fields where you want to change it's contents
field_230: data.field_230, // Bill attention
field_231: data.field_231, // Bill Company Name
field_232: data.field_232_raw, // Bill address
field_263: requestnote + " "+ oldwostring + " '"+ data.field_263 + "'", //var used to append
field_264: data.field_264, //Requested by
field_223: data.field_223_raw, //Location ID Connection field
field_281: data.field_281_raw, //Account ID Connection field
field_247: data.field_247, //Location Name
field_396: data.field_396_raw, //Location address
field_240: data.field_240_raw, //Equipment ID Connection Field
field_227: true, //Quote Enable Yes-No is True
field_771: substatus, // Sub Status
field_819: data.field_819, // Labor
field_820: data.field_820, // Parts
field_824: data.field_824, // Net Quoted

}; //End of variable DuplicateData

// Add a button to execute the action
$("#" + view.key).prepend('<button id="copy-button">Copy Workorder Details to New Quote</button>');
// Add the duplicate function to the button we just added
document.getElementById("copy-button").addEventListener("click", function () { //Click
Knack.showSpinner();
// Ajax 1 starts and does not close until 1st Success Alert complete
$.ajax({
url: "https://api.knack.com/v1/pages/scene_608/views/view_1122/records/", //Must be a Table or List
type: "POST",
headers: {
"X-Knack-Application-Id": Knack.application_id,
"X-Knack-REST-API-Key": "knack",
"Authorization": Knack.getUserToken(),
"Content-Type": "application/json"
},
data: JSON.stringify(duplicateData),

// The first Alert Starts here and does not close until after 2nd Ajax closes
success: function(data) { alert('Created Copy of this Workorder to ' + data.record.field_363);
var newwoid = data.record.id; //This is the ID of the new Parent Record
var markup4 = "4"; //Non critial variable
var models = Knack.models['view_2648'].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_515: newwoid, // New Parent Record ID
field_495: models[i].attributes.field_495_raw, //OKAY Gator number
field_558: models[i].attributes.field_558_raw, //OKAY Name
field_487: models[i].attributes.field_487_raw, //Labor-Part-Travel Type L,P,T
field_499: models[i].attributes.field_499_raw, //description
field_503: models[i].attributes.field_503_raw, //Qty Used
field_504: models[i].attributes.field_504, //Cost Each
field_524: models[i].attributes.field_524_raw, //Cost Total-Equation Field
field_505: models[i].attributes.field_505, //List Each
field_1148: models[i].attributes.field_505, //Write In list Price = List Each
field_516: models[i].attributes.field_516_raw, //List Total-Equation Field
field_488: models[i].attributes.field_488_raw, //Yes-No Show on Quote
field_491: markup4,
field_950: false, //Yes-No Bill
field_921: models[i].attributes.field_921, //Part Number Requested
field_1322: models[i].attributes.field_1322_raw, //Vendor quote PDF attachment
field_754: models[i].attributes.field_754, //Special notes
} //End of copied Items

//2nd Ajax Starts

$.ajax({
url: "https://api.knack.com/v1/pages/scene_1258/views/view_2618/records/", //Must be a Table or List
type: "POST",
headers: {
"X-Knack-Application-Id": Knack.application_id,
"X-Knack-REST-API-Key": "knack",
"Authorization": Knack.getUserToken(),
"Content-Type": "application/json"
},
data: JSON.stringify(copieditems),
success: function (response) {
alert("Item Copied!"); //Locating here will alert every item
Knack.hideSpinner();

}

}); //Ajax 2 End

} //End of the Items Loop

} //End of the 1st success Alert

}); //Ajax 1 End

}); //End of the Click Function

}); // End of the View Render