It has been nothing but struggle to get the data out using the knack functions. The past post do not work any more and even the example api dont help. Anyone out there who has built this function successfully share your experiences?
End goal of this exercise is to insert the child records along with the master record.
var addCheckboxes = function(view) { //This works as expected
// add the checkbox to to the header to select/unselect all
$('#' + view.key + '.kn-table thead tr').append('<th><input type="checkbox">Copy All Data</th>');
$('#' + view.key + '.kn-table thead input').change(function() {
$('.' + view.key + '.kn-table tbody tr input').each(function() {
$(this).attr('checked', $('#' + view.key + '.kn-table thead input').attr('checked') != undefined);
});
});
// add a checkbox to each row in the table body
$('#' + view.key + '.kn-table tbody tr').each(function() { // This works as expected
$(this).append('<td><input type="checkbox"></td>');
});
};
/**** CHANGE VIEW_ID TO YOUR OWN VIEW ID ****/
$(document).on('knack-view-render.view_193', function(event, view) { //This works as expected
// Add checkboxes to our table
addCheckboxes(view);
// Add a copy button
$("#view_193").append("<button id='copydata'style='padding: 1 10px; line-height: 32px;'class='kn-button-menu'>Copy Data</button>");
// Click event for the update button
$('#copydata').click(function() { // This works partly up until getting record id after that it fails
console.log(view.key);
//Knack.showSpinner();
//First let's define the Keys needed to call the API functions
var AppID = 'XXXXXXXXXXXXXX';
var APIKey = 'XXXXXXXXXXXXX';
//var ObjUrl = 'https://builder.knack.com/yourownurl/'
// Declare empty variables, which will be set and used at time of calling the function below
var TableID = '';
var FilterFieldID = '';
var TargetFieldID = '';
// Set up parameters for the first call of the function, which will update the first table
var TableID1 = 'object_10'; // This is the DAR Worksheets table
var FilterFieldID1 = 'field_68'; // This is the DAR Name field
var TargetFieldID1 = 'field_69'; // This is the Activity Date field
// Now get the user name to to use as the filter to retrieve the record from the table
var UserName = Knack.getUserAttributes().name;
// Set the parameters for table 1 update
TableID = TableID1; // This is the User Worksheets table
FilterFieldID = FilterFieldID1; // This is the Name field
TargetFieldID = TargetFieldID1; // This is the Works field
alert('Before func for tab1 call ' + UserName);
$('#' + view.key + ' tbody input[type=checkbox]:checked').each(function() { // This works as expected
var RecID = $(this).closest('tr').attr('id'); // record id
alert('Rec Id is '+ RecID);
var BaseRequestURL = 'https://builder.knack.com/yourownurl/' + TableID + '/records';
var RequestFilter = [{
'field': FilterFieldID,
'operator': 'is',
'value': RecID
}];
var get_url = BaseRequestURL + '?filters=' + encodeURIComponent(JSON.stringify(RequestFilter));
console.log('Record Id is '+ RecID); // This works
console.log('get_url is '+ get_url);
$.ajax({ //Start of FAILURE POINT
url: get_url,
type: 'GET',
//async: false,
headers: {
'Authorization': Knack.getUserToken(),
'X-Knack-Application-Id': AppID,
'X-Knack-REST-API-Key': APIKey,
'Content-Type': 'application/json'
}
}).done(function(Dardata) { // Everything after this point gives an "Undefined error"
// alert('Records Retrieved' + data.records.length);
// console.log('Got record! total records is:'+ Dardata.records.length);
for (var i = 1; i < Dardata.records.length; i++){
var newdata = {
//field_68: Dardata.records[i].field_68; // Auto-increment so commented
field_67: Dardata.record[i].field_67,
field_69: Dardata.record[i].field_69,
field_83: Dardata.record[i].field_83,
field_99: Dardata.record[i].field_99,
field_94: Dardata.record[i].field_94_raw,
field_79: Dardata.record[i].field_79,
field_80: Dardata.record[i].field_80,
field_84: Dardata.record[i].field_84,
field_85: Dardata.record[i].field_85_raw.id,
field_86: Dardata.record[i].field_86,
field_87: Dardata.record[i].field_87,
field_88: Dardata.record[i].field_88,
field_107: Dardata.record[i].field_107_raw.id,
field_108: Dardata.record[i].field_68_raw.id,
field_111: Dardata.records[i].field_24_raw.id,
field_112: Dardata.records[i].field_27_raw.id,
field_113: Dardata.records[i].field_28_raw.id
};
alert('Processing Record data:' + Dardata.records[i]);
} //end of FOR
}// get function data
}); // For each check box function
Knack.hideSpinner();
}); // -- end of click function
}); // This is the end of the view render