Closed VanWeapon closed 1 year ago
here is some code i wrote to do this on-demand in node for table names, tho the concept applies broadly:
var fs = require('fs');
const axios = require('axios');
async function getTableRest() {
let url = 'https://instance.service-now.com/api/now/table/sys_db_object?sysparm_fields=name';
let auth = 'Basic ' + btoa('username' + ':' + 'password');
axios
.get(url, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: auth,
},
})
.then((res) => {
// console.log(res.data.result);
// console.log(res);
let results = res.data.result;
// console.log(results);
fs.writeFileSync('./ts/tablenames.d.ts', `declare type InstanceTableNames = `);
for (var x of results) {
var table_name = x.name;
if (!table_name) continue;
fs.appendFile(
'./ts/tablenames.d.ts',
`
| "${table_name}"`,
{ encoding: 'utf-8' },
() => {}
);
}
});
}
getTableRest();
Resulting file:
Then I just change the type signature of GlideRecord (and GlideAggregate)
and now i have autocomplete for table names in GlideRecord:
Interesting didn't know this! Does this result in an error when the used value is not in the type, or preloaded? (that may be confusing for the average user)
No errors, since these aren't TypeScript files they don't get checked unless the user runs a TSLint explicitly
Implemented :)
Typescript types allow for declaration of a set of constant strings like so:
Then you can update the definition of
gs.getProperty
in the d.ts file like belowThen, when you try to use gs.getProperty in a script, you actually get autocomplete showing you only valid property names.
Since this list is large, this should probably only be pulled/refreshed on demand. Maybe once optionally during initial instance setup, then again as a separate command
For now, I can load this list manually by exporting to csv or something then loading into my own typedef file, but it would be cool if the extension could do this too. Maybe a similar thing could be done for table names so that GlideRecord can only be used on valid tables?