Run script on record change / save

Viewing 6 reply threads
  • Author
    Posts
  • February 1, 2021 at 3:53 PM #43349

    Keith Nolen
    Participant

    Hi! Is there a way to run a script on a record change or save event? And can I modify just one record with a script, or does a script have to modify all records in a form?

    What other events are there?

    February 2, 2021 at 1:10 PM #43351

    Sam Moffatt
    Participant

    Calculation and Script fields are automatically run each time a field they reference is updated within a record. A script field can then change other fields in the record as well which can optionally also trigger scripts that watch those fields (a behaviour that can be disabled if needed to avoid cycles or other behaviour that might not be intended). Calc and script fields are also evaluated when a record is created.

    Form scripts are executed on demand and you can execute them against the currently selected record (in which case record is populated) or you can apply it to all records in the form (form is populated which has a getRecords() method). There is also another special search variable that is populated when you’re working with a search (you can do typeof(search) !== "undefined" to see if it is set).

    February 3, 2021 at 1:02 AM #43355

    Brendan
    Keymaster

    Great explanation Sam.

    At some point I would like to add triggers to run Form Scripts based upon a specific set of events. I know you asked for this eons ago Sam.

    July 9, 2021 at 1:00 PM #44770

    Kenneth James
    Participant

    I am misunderstanding something about how and when scripts are triggered and when they fire. My use case: Trying to set a toggle switch (CheckMark field) in a record that will populate a default value in the Location field, otherwise, just leave the Location field alone.

    The script in the Script Field.

    function Default_Location() {
    var useDefault = record.getFieldValue('fld-332bc0f45e1d4a9bbcf6b90d0087234e');
    
    console.log(useDefault) 
    	
    	if (useDefault = false) {
    		// the check mark is NOT checked meaning add the default location
    		record.setFieldValue('fld-e83fedb15a0d4a09be818513c2a0f4e5',
    			JSON.parse(

    {
    “altitude”: “849.74”,
    “heading”: “-0”,
    “lat”: “46.9999999”,
    “lon”: “-118.99999999”,
    “pitch”: 0,
    “title”: “123 Main Street, Anywhere, United States”}`))
    // my actual home address goes here!
    }
    }
    Default_Location();`

    What happens:

    Add a new record:
    CheckMark field = unchecked (i.e. false)
    Location field = blank (what it should be)

    While in the record, I check the CheckMark field indicating that the Location field should be auto-populated with the default value.
    CheckMark field = checked (i.e. true)
    Location field = still blank, nothing happens.

    Close the record and move to another record

    Go back to the previous record and re-open it.
    CheckMark field = checked (i.e. true)
    Location field = still blank, nothing happened.

    IF I REVERSE THE LOGIC IN THE SCRIPT, change the if (useDefault = true), THEN a new record is populated automatically with the default location while the CheckMark field is unchecked.

    Something is not behaving like I anticipate that it should be behaving.

    Obviously, I am missing something.

    Thanks for any advice.

    Kenneth James

    July 9, 2021 at 2:09 PM #44772

    Daniel Leu
    Participant

    The ‘equal to’ operator is ‘==’ and not ‘=’. So you should use

    if (useDefault == false) {

    (and I don’t know how many times I happen to make the same mistake….)

    July 9, 2021 at 7:03 PM #44775

    Kenneth James
    Participant

    Thank you. I knew of the distinction, and I even found my Java Script book to verify and all I was seeing were examples for the assignment designation – the single “=”. It just didn’t occur to me that that was the problem.

    Problem solved!

    Thank you
    KDJ

    July 10, 2021 at 8:23 AM #44777

    Daniel Leu
    Participant

    Great that it was just this litte thing!

Viewing 6 reply threads

You must be logged in to reply to this topic.