Searching an unlinked form

Viewing 6 reply threads
  • Author
    Posts
  • October 21, 2019 at 8:28 PM #37292

    Stephen Abshire
    Participant

    Is it possible to search for a given value in a field that is located on an unlinked form? A code snipped would be very useful.

    October 21, 2019 at 10:49 PM #37294

    Daniel Leu
    Participant

    Maybe something like this:

    // get form of based on form name
    var myForm = document.getFormNamed('my form');
    // get all records
    var records = myForm.getRecords();
    // loop over all records
    var rec;
    for (rec of records) {
       // get value and compare
       if (rec.getFieldValue(field_id) == 'expected value') {
          // if match, do something
          console.log("success");
          break;
       }
    }

    Please note that I didn’t run this code…. It might make sense to put this all in a function os it can be easily reused.

    October 23, 2019 at 7:19 PM #37326

    Stephen Abshire
    Participant

    Got this working thanks.

    October 24, 2019 at 7:58 AM #37333

    Daniel Leu
    Participant

    Perfect!

    October 25, 2019 at 5:23 AM #37358

    David Gold
    Participant

    I’ve edited the above but am running into issues with the final copy to clipboard. Have I made an error I’m missing somewhere:

    // get form of based on form name
    var search = Utils.copyTextFromClipboard();
    var myForm = document.getFormNamed('Travel Database');
    // get all records
    var records = myForm.getRecords();
    // loop over all records
    var rec;
    for (rec of records) {
       // get value and compare
       if (rec.getFieldValue('fld-cf720b6ab4314f0bb5f47bc9bd61f0a9') == search) {
          // if match, do something
    	Utils.copyTextToClipboard(rec.getFieldValue('fld-cf720b6ab4314f0bb5f47bc9bd61f0a9'));
          break;
       }
    }
    October 25, 2019 at 7:59 AM #37363

    Daniel Leu
    Participant

    I’m not certain that your comparison works. What is the content of fld-cf720b6ab4314f0bb5f47bc9bd61f0a9 supposed to be? The string ‘search’? Then it should be in quotes.

    Sidenode: I recommend to define all the fields used in a script at the beginning of a script or at the beginning of a function. And then use the identifiers later on. This increases readability, specially when you revisit a script that you wrote a while ago.

    var field_a_id = 'fld-....';
    var field_b_id = 'ld-....';
    ...
       if (rec.getFieldValue(field_a_id) == search) {
          // if match, do something
    	Utils.copyTextToClipboard(rec.getFieldValue(field_b_id));
          break;
       }
    ...
    
    October 25, 2019 at 8:38 AM #37364

    Sam Moffatt
    Participant

    As an aside, this will do an exact match on a value:

       if (rec.getFieldValue(field_a_id) == search) {
    

    This will do a regular expression match:

       if (rec.getFieldValue(field_a_id).match(/search/i) {
    

    If your search term wasn’t an exact match, this will do a partial match for you.

    October 25, 2019 at 2:08 PM #37384

    David Gold
    Participant

    I’m taking a search term from the clipboard and then comparing it to the contents of the field fld-cf720b6ab4314f0bb5f47bc9bd61f0a9 and if there is a match I am then taking the contents of a different field in that record and putting that into the clipboard. Am I using the field references incorrectly? (I take the separate point on the regex matching).

Viewing 6 reply threads

You must be logged in to reply to this topic.