Save Knack Record ID
How to save the behind-the-scenes Knack Record ID to relate records or send child record emails
Last updated
How to save the behind-the-scenes Knack Record ID to relate records or send child record emails
Last updated
In order to the save the Knack Record ID, we use a View Based PUT with the Knack API.
/******************************/
/**** Save Knack Record ID ****/
/******************************/
// Function to Save Record ID
function saveRecordId(recordId) {
$.ajax({
// Scene/View of Second Form
url: "https://api.knack.com/v1/pages/scene_269/views/view_740/records/" + recordId,
type: "PUT",
headers: {
"X-Knack-Application-Id": Knack.application_id,
"X-Knack-REST-API-Key": `knack`,
"Authorization": Knack.getUserToken(),
"ContentType": "application/json"
},
data: {
// Store Record ID in Knack Record ID field on TIA Request object
field_824 : recordId,
},
tryCount: 0,
retryLimit: 3,
success: function(response) {
// Success Message in Console
console.log("Captured Record ID");
Knack.hideSpinner();
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
this.tryCount++;
let tryCount = this.tryCount, retryLimit = this.retryLimit, seconds;
if (tryCount <= retryLimit) { //try again
switch(tryCount) {
case 1:
case 2: seconds = 5; break;
case 3: seconds = 10; break; }
let timeout = seconds * 1000;
console.log("Error: " + XMLHttpRequest.status + " " + XMLHttpRequest.statusText + "\nRetry Count: " + tryCount + "\nRetrying in " + seconds + " seconds")
let ajaxObject = this;
window.setTimeout(function(){
$.ajax(ajaxObject);
}, timeout);
} else {
// Failure Message in Console
console.log("Failed to Capture Record ID");
}
}
});
}
// Listen for record creation (Application Submit / First Form)
$(document).on('knack-record-create.view_393', function(event, view, record) {
const recordId = record.id;
console.log('CREATE')
saveRecordId(recordId);
});
// This is the Save Record ID / Second Form.
// This removes the view from HTML upon rendering to prevent data manipulation.
$(document).on('knack-view-render.view_740', function (event, view, record) {
$('#' + view.key).remove();
});
None needed 😎
Create a short text field for the parent object to capture the Knack Record ID
Create a text formula field on the child object that is set to the Knack Record ID field on the parent object
Create a new page to Save the Record ID
Make sure the page settings do NOT have the page included in the page menu
Add a parent object form view to the page that will only show the Knack Record ID field. No additional form rules are needed.
Establish a form submit page or multi-page application where the parent object is created if there is not one already. For example, in the TDS TIA Module there is a parent object multi-page application. The TIA Applicant Information is the first page in this application where the parent object is created. This is the form view that will save the Knack Record ID.
Update the JS for data section where the Record ID is stored with the correct Field ID
data: {
// Store Record ID in Knack Record ID field on TIA Request object
field_824 : recordId,
},
Update the JS handlers for both the first and second forms with the correct View IDs
// Listen for record creation (Application Submit / First Form)
$(document).on('knack-record-create.view_393', function(event, view, record) {
const recordId = record.id;
console.log('CREATE')
saveRecordId(recordId);
});
// This is the Save Record ID / Second Form
// This removes the view from HTML upon rendering to prevent data manipulation.
$(document).on('knack-view-render.view_740', function (event, view, record) {
$('#' + view.key).remove();
});
Lastly, the url in the function will need to be updated with the Scene and View IDs of the Save Record ID page
url: "https://api.knack.com/v1/pages/scene_269/views/view_740/records/" + recordId,