Forum Discussion
_anomDiebolt_
7 years agoQrew Elite
I wanted to add to my answer as I did not address the issue of using the code for a "Batch Update" as your title mentioned. The code I posted included a function changeFieldType() that changed the type of a single field. You might think that you could just call this function multiple times to make a batch update to multiple fields. However, if you tried this on a large number of fields you might well encounter a problem as your browser may choke when making so many simultaneous network requests. Luckily, there is a simple solution which can be extended and applied to much larger automation pipelines.
Note that the original code always returned a promise that resolved to the string "it certainly worked!". This was done because the native action GenChangeFieldType changes the field type on the backend and returns an unstructured HTML page as opposed to a well structured XML response. We could rip through the returned HTML page and scrape out some information indicating the action completed successfully or not but it probably isn't worth the extra effort because short of an error in the parameters the operation will almost always be successful. If you did want to process the HTML response the function would look something like this:
Well it turns out that there is a new feature in JavaScript that allows you to do precisely this. Here is some additional code that uses something called an "asynchronous generator" function named changeFieldTypes (nb this function name is plural and it takes an array of fids):
Asynchronous Generators and Pipelines in JavaScript
https://dev.to/nestedsoftware/asynchronous-generators-and-pipelines-in-javascript--1h62
The bottom line is that using these techniques (jQuery and ES6 Promises along with Asynchronous Generators etc) you can create complex pipelines of processing to automate just about anything and the resulting code will be extremely short.
Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=682
Note that the original code always returned a promise that resolved to the string "it certainly worked!". This was done because the native action GenChangeFieldType changes the field type on the backend and returns an unstructured HTML page as opposed to a well structured XML response. We could rip through the returned HTML page and scrape out some information indicating the action completed successfully or not but it probably isn't worth the extra effort because short of an error in the parameters the operation will almost always be successful. If you did want to process the HTML response the function would look something like this:
function changeFieldType(dbidTable, fid, type) {If we wanted to do a batch update on multiple fields we might have a array of fids:
return Promise.resolve(
$.get(dbidTable, {
a: "GenChangeFieldType",
BUT: "CONVERT+DATA",
D0: type,
fid: fid
})
).then(function(response) {
return <extract some data from the HTML response>;
});
}
var fids = ["6", "7", "8", "9"];Then we might call changeFieldType() multiple times in succession like this:
changeFieldType(dbidTable, fid[0], type)But clearly this approach will not work if you have say 100 fields to convert as the code would be repetitious and lengthy. If only we could use some type of a for loop to structure this chain of promises.
.then(function(response) {
console.log(response);
return changeFieldType(dbidTable, fid[1], type)
})
.then(function(response) {
console.log(response);
return changeFieldType(dbidTable, fid[2], type)
})
.then(function(response) {
console.log(response);
return changeFieldType(dbidTable, fid[3], type)
})
.then(function(response) {
console.log(response);
})
Well it turns out that there is a new feature in JavaScript that allows you to do precisely this. Here is some additional code that uses something called an "asynchronous generator" function named changeFieldTypes (nb this function name is plural and it takes an array of fids):
async function* changeFieldTypes(dbidTable, fids, type) {I will skip the technical details in this post but you can read more about Asynchronous Generators here:
for (var i = 0; i < fids.length; i++) {
var fid = fids;
yield await changeFieldType(dbidTable, fid, type);
}
}
for await (const response of changeFieldTypes(dbidTable, fids, type)) {
console.log(response);
}
Asynchronous Generators and Pipelines in JavaScript
https://dev.to/nestedsoftware/asynchronous-generators-and-pipelines-in-javascript--1h62
The bottom line is that using these techniques (jQuery and ES6 Promises along with Asynchronous Generators etc) you can create complex pipelines of processing to automate just about anything and the resulting code will be extremely short.
Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=682