Script to conduct a search

Viewing 3 reply threads
  • Author
    Posts
  • October 18, 2023 at 1:10 AM #49995

    David Gold
    Participant

    I don’t know JavaScript so I was wondering if someone could help me with a simple script for the iOS app that does a search of a database for a term called “input” of a database called Boral with a form called Boral Data and searched a field called Name (I can add the field ID where it is needed)?

    Any assistance would be much appreciated.

    October 18, 2023 at 6:41 PM #49998

    David Gold
    Participant

    I’ve had a go at it cutting and pasting from some other scripts but can’t work out why it’s not working. Am getting an error in the console saying:

    var myForm = document.getFormNamed(‘Boral’);
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var selected;

    function copy_name( name ) {
    Utils.copyTextToClipboard( name );
    }

    function multiple_results() {

    var joined = ‘–multiple_matches–‘;
    var res;
    for (res of results) {
    joined = joined +
    res.name;
    }
    copy_comments( joined );
    }

    function search_records( haystack , needle ) {

    var name_id = ‘fld-1acb38c0b51b44698849501507b51722’;
    var rec;

    for (const rec of haystack) {
    if ( rec.getFieldValue( name ).toLowerCase().includes( needle.toLowerCase() ) ) {
    results.push( { location: rec.getFieldValue( name_id ) } );
    result_count++;
    }

    }

    if( result_count == 0 ){
    console.log( ‘No results found!’ );
    }else if( result_count > 1 ){
    multiple_results();
    }else{
    copy_name( results[0].name );
    }

    }

    search_records( records , search_term );

     

    • This reply was modified 1 year, 2 months ago by David Gold.
    October 20, 2023 at 6:16 PM #50015

    Brendan
    Keymaster

    What is the error message?

    October 20, 2023 at 8:10 PM #50024

    Daniel Leu
    Participant
Viewing 3 reply threads

You must be logged in to reply to this topic.