Config Files

I have a use case that I need to either manually create a project number, or have the system to it automatically. I tried a couple of methods and settled on having a config file that stored the last project number used, then I would do a get on the config, add 1, save the project records and update the config record.

There might be a better way, put this is working nicely. I thought I would share the code. (pasting into here loses all formatting, it is easier to follow when indented).

// *****************************************************************************
// Project Code
// *****************************************************************************

$(document).on('knack-record-create.view_41', function(event, view, record) {
// Declare variables used
var KnackAppID = "xxxx";
var KnackAPIKey = "yyyy";

var ProjectNumber = record.field_254_raw;
var ProjectName = record.field_39_raw;
var ProjectOrigin = record.field_203_raw;

// Get the config file that contains the last used Project Number.
// The GET uses the specific record ID for a specific config record.
// There should only be (1) config file.
$.ajax({
url: "https://api.knack.com/v1/objects/object_81/records/5d5c7c11d30ec50010f87f4e/",
type: "GET",
headers: {
"X-Knack-Application-Id": KnackAppID,
"X-Knack-REST-API-Key": KnackAPIKey
},
success: function(data) {
console.log("GET Config - Done!")

switch(ProjectOrigin) {
case "DetaiLED Solutions":
LastProjectNumber = data.field_790_raw,
CurrentProjectNumber = LastProjectNumber + 1,
ProjectDisplay = CurrentProjectNumber + " - " + ProjectName
break;
case "VER":
CurrentProjectNumber = ProjectNumber ,
ProjectDisplay = CurrentProjectNumber + " - " + ProjectName
break;
case "Original DetaiLED Solutions":
CurrentProjectNumber = ProjectNumber ,
ProjectDisplay = CurrentProjectNumber + " - " + ProjectName
break;
default:
CurrentProjectNumber = ProjectNumber ,
ProjectDisplay = CurrentProjectNumber + " - " + ProjectName
}


// If GET worked correctly, then...
// Save the Data to the Project Object

$.ajax({
url: "https://api.knackhq.com/v1/objects/object_10/records/" + record.id,
type: "PUT",
headers: {
"X-Knack-Application-Id": KnackAppID,
"X-Knack-REST-API-Key": KnackAPIKey
},
data: {
field_233: ProjectDisplay,
field_254: CurrentProjectNumber,

},
success: function(data) {
console.log("PUT Project - Done!");

// If PUT worked correctly, then update config file with new Last Project Number
$.ajax({
url: "https://api.knackhq.com/v1/objects/object_81/records/5d5c7c11d30ec50010f87f4e/",
type: "PUT",
headers: {
"X-Knack-Application-Id": KnackAppID,
"X-Knack-REST-API-Key": KnackAPIKey
},
data: {
field_790: CurrentProjectNumber,
},
success: function(data) {
console.log("PUT Config - Done!");
}
});
}
});
}
});
});