Open record as result

Viewing 15 reply threads
  • Author
    Posts
  • October 19, 2023 at 2:39 AM #50000

    Paul Hirst
    Participant

    If I’ve written a script to conduct a search and then at the end want to open the record that is the result of the search is there a way to do this? I have found the ability to print the result but that brings it up as a page to print rather than opens up the record?

    October 19, 2023 at 9:29 AM #50001

    Daniel Leu
    Participant

    Hi Paul, did you try form.selectRecord(someRecord);?

    • This reply was modified 1 year ago by Daniel Leu.
    October 19, 2023 at 1:32 PM #50003

    Paul Hirst
    Participant

    No I haven’t. I’ll give that a shot. Is someRecord the record ID? I can’t see it defined in the API.

    October 19, 2023 at 4:38 PM #50004

    Daniel Leu
    Participant

    According to the API doc, the parameter is a record, so it’s a record object and not a record id. If you have one search result I would try

    let records = search.getRecords();
    form.selectRecord(records[0]);
    
    
    • This reply was modified 1 year ago by Daniel Leu.
    October 20, 2023 at 12:39 AM #50008

    David Gold
    Participant

    Hi Daniel (or OP)

    I would like to be able to do this also. I have some code below (not sure exactly how to insert a code block). I’m trying to do what you’ve said in line 11 with function show_name. Basically I want to be able to search the name field and when I get a result (whether multiple or single by selection in prompter) it then opens the record in Tap Forms. Can you tell me how to make it work?

    Really appreciate any help as my javascript knowledge is limited.

    var myForm = document.getFormNamed(‘Test’);
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var results_name = [];
    var selected;
    function show_name( name ) {
        form.selectRecord(name[0]);
    }
    function copy_result_multiple( name ) {
        if ( name == true ) {
            console.log( ‘Index:’ + results.indexOf( selected ) );
            console.log( results_name[ results.indexOf( selected ) ] );
            show_name( results_name[ results.indexOf( selected ) ] );
        } else {
            console.log( ‘Cancelled’ );
        }
    }
    function multiple_results( all_results ) {
        let prompter = Prompter.new();
        prompter.cancelButtonTitle = ‘cancel’;
        prompter.continueButtonTitle = ‘Show Name’;
        prompter.addParameter(‘Select Result ‘, ‘selected’, ‘popup’, all_results)
        .show(‘Multiple Results Found’, copy_result_multiple );
    }
    function search_records( haystack , needle ) {
    var name_id = ‘fld-1acb38c0b51b44698849501407b51722’;
        var rec;
        for (rec of haystack) {
        var name = rec.getFieldValue( name_id );
        if (name && name.includes( needle ) ) {
                results.push( rec.getFieldValue( name_id ) );
                results_name.push( rec.getFieldValue( name_id ) );
                result_count++;
            } else {
            if (! name) {
            console.log(“Empty field: ” + rec.getUrl());
            }
            }
        }
        if( result_count == 0 ){
            Utils.alertWithMessage(“No Results”, “No results were found for the search term: ” + needle);
        }else if( result_count > 1 ){
            //multiple results
            multiple_results( results );
        }else{
            //single result
            show_name( results_name[0] );
        }
    }
    search_records( records , search_term );
    October 20, 2023 at 2:05 PM #50011

    Daniel Leu
    Participant

    You can use backticks around the code to have it displayed as such.

    When I write code, I try to avoid global variables. It makes it easier to understand what’s going on.

    With show_name( results_name[0] ); you provide a record object as argument. So there’s no need to do this again with form.selectRecord(name[0]);. Just form.selectRecord(name); in the show_name function would be sufficient.

    October 20, 2023 at 4:24 PM #50012

    David Gold
    Participant

    I hear you re global variables. When I remove the [0] I get the following error in the console:

    Search Test Records Again: TypeError: Argument does not match Objective-C Class, line:(null)

    October 20, 2023 at 6:17 PM #50016

    Daniel Leu
    Participant

    I got it working after some edits. It shows as well that form.selectRecord(someRecord); works :). The issue was results vs results_name. The former contains the found records while the latter contains the matching names.

    A remaining issue is when you get more than one match. Since you match on the name and then use results_name.indexOf(name) to find the index, you will always stop at the first found entry. This needs some refinement.

    //var myForm = document.getFormNamed(‘Test’);
    //var records = myForm.getRecords();
    var records = form.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var results_name = [];
    var selected;
    function show_name( name ) {
        form.selectRecord(name);
    }
    function copy_result_multiple( name ) {
        if ( name == true ) {
        let index = results_name.indexOf(selected);
            console.log( 'Index:' + index );
            console.log( results_name[index]);
            show_name( results[index]);
        } else {
            console.log( 'Cancelled' );
        }
    }
    function multiple_results( all_results ) {
        let prompter = Prompter.new();
        prompter.cancelButtonTitle = 'cancel';
        prompter.continueButtonTitle = 'Show Name';
        prompter.addParameter('Select Result ', 'selected', 'popup', results_name)
        .show('Multiple Results Found', copy_result_multiple );
    }
    function search_records( haystack , needle ) {
    var name_id = 'fld-5ecd60d90a8246f4853175f6fc6aaabe';
    //var name_id = 'fld-1acb38c0b51b44698849501407b51722';
        var rec;
        for (rec of haystack) {
        var name = rec.getFieldValue( name_id );
        if (name && name.includes( needle ) ) {
                results.push( rec );
                results_name.push( rec.getFieldValue( name_id ) );
                result_count++;
            } else {
            if (! name) {
           console.log("Empty field: " + rec.getUrl());
            }
            }
        }
        if( result_count == 0 ){
            Utils.alertWithMessage("No Results", "No results were found for the search term: " + needle);
        } else if ( result_count > 1 ){
            //multiple results
            console.log("Found " + result_count + " matches");
            multiple_results( results );
        } else {
            //single result
            console.log("Found one match");
            show_name( results[0] );
        }
    }
    search_records( records , search_term );
    • This reply was modified 1 year ago by Daniel Leu.
    • This reply was modified 1 year ago by Daniel Leu.
    • This reply was modified 1 year ago by Daniel Leu.
    • This reply was modified 1 year ago by Daniel Leu.
    • This reply was modified 1 year ago by Daniel Leu.
    October 20, 2023 at 8:07 PM #50023

    David Gold
    Participant

    Thanks so much. Really appreciate it. It is definitely working on my Mac. For some reason it doesn’t actually open the record on iOS but I’ll live with it working on Mac. Thanks again!

    October 20, 2023 at 8:14 PM #50025

    Daniel Leu
    Participant

    Cool! The iOS issue is something for @brendan to look at.

    October 20, 2023 at 8:16 PM #50026

    David Gold
    Participant

    OK thanks.

    October 20, 2023 at 9:41 PM #50027

    Brendan
    Keymaster

    The form.selectRecord() function only works on the Mac version right now.

    You could ask the record for its URL with record.getUrl() and then call Utils.openUrl(url); to have the record displayed on iOS.

    October 21, 2023 at 12:11 AM #50028

    David Gold
    Participant

    Thanks. Got that working.

    November 2, 2023 at 12:46 PM #50080

    Glen Forister
    Participant

    I’ve always wanted this in a database, and I did have it in HanDBase in it’s early years, but it got lost for some reason in its later years and I severely miss it and asked about it soon after I joined.

    Is this a function that can be attempted without Script knowledge to make all the connections.  I can do that for simple scripts, but I wonder about the maze above.

    Hopefully.

    November 3, 2023 at 8:25 PM #50085

    Brendan
    Keymaster

    Hi Glen,

    Which part of the above maze do you mean when you say this? Searching records? You don’t need scripting to search records.

    January 24, 2024 at 11:11 PM #50374

    Scott Specker
    Participant

    Hi Brendan, thanks, this explains why form.selectRecord() wasn’t working on iOS.

    The form.selectRecord() function only works on the Mac version right now.

    You could ask the record for its URL with record.getUrl() and then call Utils.openUrl(url); to have the record displayed on iOS.

    I have a script that creates a new record following your mileage entry example. At the end, I want to go to the newly created record. Since select record doesn’t work on iOS, I tried the URL suggestion.

    Unfortunately, the URL solution doesn’t appear to work on iOS, either. It works on the Mac, but on iOS, it doesn’t jump to the new record. I have to exit the current record and then manually select the new one.

    var newRecord = form.addNewRecord();
    form.saveAllChanges();

    // form.selectRecord(newRecord);
    var url = newRecord.getUrl();
    Utils.openUrl(url);

    Any idea what I am doing wrong?

Viewing 15 reply threads

You must be logged in to reply to this topic.