What is the recommendation in relation to declaration of variables in scripts?
I have a test/demo field script which is executed every time a check mark is changed.
The problem I have is when this is run for the 2nd and subsequent times I get an error since the variable is already declared (see error on screenshot attached).
var upc_lookup_id = 'fld-8b5f99cd185044f8a0b35204db4c86ac';
record.getFieldValue(upc_lookup_id);
var callbackFunction = function() {
console.log(value_1);
};
var value_1;
let prompter = Prompter.new();
prompter.addParameter('Label 1')
.show('Message prompt', callbackFunction);
record.setFieldValue(upc_lookup_id, false);
document.saveAllChanges();
It’s entirely possible I’m doing something daft since I’m new to javascript!
Attachments:
You must be
logged in to view attached files.
It’s best to declare variables within a function declaration.
function runPrompter() {
var upc_lookup_id = 'fld-8b5f99cd185044f8a0b35204db4c86ac';
record.getFieldValue(upc_lookup_id);
var callbackFunction = function() {
console.log(value_1);
};
var value_1;
let prompter = Prompter.new();
prompter.addParameter('Label 1').show('Message prompt', callbackFunction);
record.setFieldValue(upc_lookup_id, false);
document.saveAllChanges();
}
runPrompter();
That should work.
But it may be that you’ve found a bug that’s not clearing out the JSContext
when you run the script the second time.
Also probably that I haven’t tested the prompter within a Script Field. I’ve only ever used it within a Form Script.