Pre-processing Field entry

Viewing 9 reply threads
  • Author
    Posts
  • June 7, 2022 at 6:06 PM #47451

    David Schwane
    Participant

    How do I implement field data scripting wihout looping? I understand field scripts and fields are watched via adding the field id. But how do I watch a field, change its value, and not cause a loop? I’m used to event driven functions where I could choose pre or post save, etc.

    Example:
    URL Field; If someone enters a URL with a query string, I want to strip out everything after the question mark. Where do i put this script?
    var web_site = record.getFieldValue('fld-xyz');
    var new_web_site = web_site.split('?')[0];
    record.setFieldValue('fld-xyz', new_web_site);

    June 7, 2022 at 9:39 PM #47452

    Daniel Leu
    Participant

    Create a field with the type script and put it there. It will only be executed when the field web_site changes. At the end of the script, add a form.saveAllChanges().

    June 8, 2022 at 10:24 AM #47459

    Brendan
    Keymaster

    As Daniel said, and you can also hide the Script Field from displaying on the form. It will still activate when the field is changed in the UI.

    June 8, 2022 at 4:38 PM #47468

    David Schwane
    Participant

    Thank you and I understood that is the current mechanic, but for me, the script fires twice because I suppose I’m changing the field that I’m watching. While this looping doesn’t cause an issue with this example, I have some other situations where the script firing twice is problematic.

    Example attached.
    import
    open console
    copy into website field a URL with query string, i.e https://example.com/movies?si=zyH0Aqaaa

    console shows
    Script Excecuted 6/8/2022, 7:32:50 PM for https://example.com/movies
    Script Excecuted 6/8/2022, 7:32:50 PM for https://example.com/movies

    Attachments:
    You must be logged in to view attached files.
    June 8, 2022 at 9:01 PM #47470

    Daniel Leu
    Participant

    Yeah, the script is triggered again if you update it with your script. In situations like this, I use two fields, one with the raw data and one with the processed data.

    June 9, 2022 at 9:42 AM #47472

    David Schwane
    Participant

    do you have an example of a two field system?

    I have a scenario where I have Last Used (Date field) and Times Used (Number field). Each time Date Used is changed, Times Used increments 1. However, I need the Times Used to be editable for one off edits. Right now it increments twice (and 4 times if I use the date picker).

    
    var last_used = record.getFieldValue('fld-1b7192ecbe934a678e07a7db55b1cd3e');
    var times_used = record.getFieldValue('fld-83fc43975985438780e4344d310933b1');
    console.log("Process Excecuted "+ new Date().toLocaleString() + " times used = " + times_used);
    record.setFieldValue('fld-83fc43975985438780e4344d310933b1', times_used + 1);
    form.saveAllChanges();
    

    example attached.

    Attachments:
    You must be logged in to view attached files.
    June 9, 2022 at 8:43 PM #47474

    Daniel Leu
    Participant

    Something like this:

    const fieldWebSite = 'fld-...'
    const fieldWebSiteClean = 'fld-...'
    
    var web_site = record.getFieldValue(fieldWebSite);
    var new_web_site = web_site.split('?')[0];
    record.setFieldValue(fieldWebSiteClean, new_web_site);

    Re Times Used:
    I’m not certain that this works, but I think to remember that the field trigger code parses the script for fld-... patterns. So when using const fieldTimesUsed = form.getFieldNamed('Times Used').getId(), you might avoid the trigger. Certainly @Brendan will chime in on this.

    June 10, 2022 at 8:17 PM #47477

    David Schwane
    Participant

    thank you, getId() worked to bypass the trigger

    June 12, 2022 at 11:15 AM #47480

    Daniel Leu
    Participant

    Perfect!

    June 12, 2022 at 5:33 PM #47481

    David Schwane
    Participant

    one last related question. I notice if I refresh all records, the script is getting fired and incrementing the field by 1 each time. Is there a way to bypass the script trigger from record refresh?

Viewing 9 reply threads

You must be logged in to reply to this topic.