Script variable declaration

Viewing 1 reply thread
  • Author
    Posts
  • January 3, 2019 at 3:50 PM #33125

    Ian Heath
    Participant

    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.
    January 3, 2019 at 5:41 PM #33130

    Brendan
    Keymaster

    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.

Viewing 1 reply thread

You must be logged in to reply to this topic.