Script stopped working

Viewing 17 reply threads
  • Author
    Posts
  • October 2, 2020 at 3:02 AM #42098

    David Gold
    Participant

    I have a Script that used to work perfectly but has stopped working in recent versions of Tap Forms and can’t workout why.

    The script is supposed to take a search term from clipboard, do a search, if one result is found bring up a screen displaying the “Comments” field and also copy it to the clipboard. If multiple results are found it presents them, you pick one and then it also displays the “Comments” field of the selected one and copies it to the clipboard. What has stopped this working? I’m not getting any errors in the Console and the variable references are correct.

    var myForm = document.getFormNamed('Travel Database');
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var selected;
    
    function copy_comments( comments ) {
        Utils.copyTextToClipboard( comments );
    }
    
    function multiple_results() {
    
        var joined = '--multiple_matches--';
        var res;
        for (res of results) {
            joined = joined +
            res.location + '::' + res.comment;
        }
        copy_comments( joined );
    }
    
    function search_records( haystack , needle ) {
    
        var location_id = 'fld-c55265c3f56b43feb423f5a198dffe6c';
        var comment_id = 'fld-141d923e785148e3aec84576c746a4a4';
        var rec;
    
        for (const rec of haystack) {
            if ( rec.getFieldValue( location_id ).toLowerCase().includes( needle.toLowerCase() ) ) {
                results.push( { location: rec.getFieldValue( location_id ) , comment: rec.getFieldValue( comment_id ) } );
                result_count++;
            }
    
        }
    
        if( result_count == 0 ){
            console.log( 'No results found!' );
        }else if( result_count > 1 ){
            multiple_results();
        }else{
            copy_comments( results[0].comment );
        }
    
    }
    
    search_records( records , search_term );
    October 2, 2020 at 3:27 AM #42099

    David Gold
    Participant

    I forgot to say I have separate Scripts for the iOS version and the Mac version of Tap Forms given some iOS limitations. The one above is the iOS version and below is the Mac version.

    var myForm = document.getFormNamed('Travel Database');
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var results_comments = [];
    var selected;
    
    function copy_comments( comments ) {
        Utils.copyTextToClipboard( comments );
    }
    
    function copy_result_multiple( comments ) {
    
        if ( comments == true ) {
            console.log( 'Index:' + results.indexOf( selected ) );
            console.log( results_comments[ results.indexOf( selected ) ] );
            copy_comments( results_comments[ results.indexOf( selected ) ] );
        } else {
            console.log( 'Cancelled' );
        }
    
    }
    
    function multiple_results( all_results ) {
    
        let prompter = Prompter.new();
        prompter.cancelButtonTitle = 'cancel';
        prompter.continueButtonTitle = 'Copy Comment';
        prompter.addParameter('Select Result ', 'selected', 'popup', all_results)
        .show('Multiple Results Found', copy_result_multiple );
    
    }
    
    function search_records( haystack , needle ) {
    
        var location_id = 'fld-cf720b6ab4314f0bb5f47bc9bd61f0a9';
        var comment_id = 'fld-0096b55f012d4d4fb34a95f784951b55';
        var rec;
    
        for (rec of haystack) {
            if ( rec.getFieldValue( location_id ).includes( needle ) ) {
                results.push( rec.getFieldValue( location_id ) );
                results_comments.push( rec.getFieldValue( comment_id ) );
                result_count++;
            }
    
        }
    
        if( result_count == 0 ){
            console.log( 'No results found!' );
        }else if( result_count > 1 ){
            //multiple results
            multiple_results( results );
        }else{
            //single result
            copy_comments( results[0] );
        }
    
    }
    
    search_records( records , search_term );
    October 3, 2020 at 11:17 PM #42112

    Brendan
    Keymaster

    Can you upload your form (.tff file) with these scripts? I just fixed the attachment upload function.

    October 3, 2020 at 11:39 PM #42113

    Sam Moffatt
    Participant

    Do you mind being a little more specific about which part isn’t working?

    My first inclination would be to pepper the thing with console.log statements everywhere to see where it stops.

    I just copied the Mac version (the last post you did) and tried it out with a test document and it worked fine. I had to change the field names but when I ran it in the script editor it worked.

    One minor change is where you have if (result_count == 0 ){ instead of just logging the error message, use an alert box:

    
        if( result_count == 0 ){
            Utils.alertWithMessage("No Results", <code>No results were found for the search term: ${needle}</code>);
    

    Makes it a little more obvious there is no results available.

    I didn’t look at this on iOS though but generally my scripts work identically on iOS and MacOS. I think there was a time when the prompter wasn’t on iOS but it should be there now but perhaps there is still something funky there.

    October 3, 2020 at 11:53 PM #42114

    David Gold
    Participant

    Brendan – I’ve uploaded the file.

    Sam – I have the iOS version working (without the prompter – I didn’t realise the prompter was now supported). The issue I’m having is with the second script (the Mac one). The console is showing: “TypeError: undefined is not an object (evaluating ‘rec.getFieldValue( location_id ).includes’), line:(null)”
    Let me know if you know what is causing it.
    By the way love your videos!

    Attachments:
    You must be logged in to view attached files.
    October 4, 2020 at 2:44 PM #42120

    Sam Moffatt
    Participant

    Ok, that’s actually a data problem, one of your records doesn’t have location_id set on it and you’re getting undefined back from the getFieldValue call. When you then chain that to add the .includes, you’re getting the undefined is not an object error because getFieldValue returned undefined for the empty record. Chaining sometimes makes it have to figure out where something goes wrong, especially for contexts that can legitimately return a mix of return types.

    A simple fix would be to do something like:

    
    var location = rec.getFieldValue(location_id);
    if (location && location.includes ...

    An extra variable but Javascript will test if it’s “truthy” and continue. For your purposes that shouldn’t hurt anything because you want to see if it has a search value anyway so presumably any field that is empty can be ignored (undefined and an empty string are both falsy).

    Good to hear you enjoy the videos! Stuff on the forum is often the inspiration for videos so in a sense I’m putting my money where my mouth is because it’s sometimes easier to see something rather than read it.

    October 4, 2020 at 3:17 PM #42121

    David Gold
    Participant

    OK thanks. That’s strange as if I filter manually by location and look for location is empty I get no records. With your recommendation earlier regarding the result count being zero and giving a message rather than passing the message to the console – is the code you’re mentioned there correct as I’m getting a messaging saying “SyntaxError: Unexpected token ‘<‘”

    October 4, 2020 at 4:03 PM #42122

    Sam Moffatt
    Participant

    Oh that’s an annoyance of the forum, it turns the backtick character into <code> and </code> respectively even though it’s inside a script context already. In Javascript the backticks are used to create template literals which allow you to embed variables inside the string. Not particularly necessary in this case but something I’m in the habit of doing.

    If you’re looking for the record that is problematic, you can use record.getUrl() for the record that fails the test and console.log it to find it.

    Here’s my copy of the script, I took out the template string so hopefully the forum doesn’t munge it and it’s been updated to point to different fields and field names (title and subtitle from the Books form in the document I use for the YouTube videos):

    var myForm = document.getFormNamed('Book');
    var records = myForm.getRecords();
    var search_term = Utils.copyTextFromClipboard();
    var result_count = 0;
    var results = [];
    var results_comments = [];
    var selected;
    
    function copy_comments( comments ) {
        Utils.copyTextToClipboard( comments );
    }
    
    function copy_result_multiple( comments ) {
    
        if ( comments == true ) {
            console.log( 'Index:' + results.indexOf( selected ) );
            console.log( results_comments[ results.indexOf( selected ) ] );
            copy_comments( results_comments[ results.indexOf( selected ) ] );
        } else {
            console.log( 'Cancelled' );
        }
    
    }
    
    function multiple_results( all_results ) {
    
        let prompter = Prompter.new();
        prompter.cancelButtonTitle = 'cancel';
        prompter.continueButtonTitle = 'Copy Comment';
        prompter.addParameter('Select Result ', 'selected', 'popup', all_results)
        .show('Multiple Results Found', copy_result_multiple );
    
    }
    
    function search_records( haystack , needle ) {
    	var title_id = 'fld-59b7b2dfa5c843afb969e74df4ad111c';
    	var subtitle_id = 'fld-b7d482d0db5d4554ad0948f19394f441';
        var rec;
    
        for (rec of haystack) {
        	var title = rec.getFieldValue( title_id );
        	
        	if (title && title.includes( needle ) ) {
                results.push( rec.getFieldValue( title_id ) );
                results_comments.push( rec.getFieldValue( subtitle_id ) );
                result_count++;
            } else {
            	if (!title) {
            		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
            copy_comments( results[0] );
        }
    
    }
    
    search_records( records , search_term );
    October 4, 2020 at 4:05 PM #42123

    Sam Moffatt
    Participant

    One extra feature is that the console should make the link clickable so if you run it outside of the script editor (e.g. in the normal view with the console open) then it should take you to the record that it doesn’t think is setup properly.

    October 5, 2020 at 12:10 AM #42125

    David Gold
    Participant

    It’s working with your changes. Really appreciate it. Not coming up with any errors now. I’m going to try and expand what I want it so do and will come back if I have any queries (am just starting 201 of your Javascript/Tap Forms guide so getting there slowly…

    October 5, 2020 at 1:25 AM #42128

    David Gold
    Participant

    I had one other question. I’ve changed the script to show the Comments field using alertWithMessage() (instead of copying to clipboard) and it’s working fine. If I wanted to then wanted to do something else after I’ve viewed the comments and pressed the “OK” button how do I do that? I can get it to work before I’ve pressed the OK but that doesn’t allow me to read the alert.

    Also one other question – if one wants to open an external URL is there anyway to stop the message coming up that you are leaving Tap Forms (on iOS) or is that built in as a security measure to the Tap Forms app?

    October 5, 2020 at 1:35 AM #42130

    Sam Moffatt
    Participant

    Good to hear it’s working!

    I can’t claim credit for the Javascript guide though, that’s T. L. Ford’s (or @cattailnu) hard work not mine. It’s a great contribution though because whilst Javascript in Tap Forms is similar enough to Javascript at large, a lot of existing resources focus on the web (which has it’s own features added by browsers) or projects like node.js (also with it’s own quirks). Hopefully some Tap Forms specific resources will help the community at large.

    October 5, 2020 at 1:41 AM #42132

    David Gold
    Participant

    You’re right. -apologies. Was confusing the two. I have watched your videos also!

    October 5, 2020 at 1:46 AM #42133

    Sam Moffatt
    Participant

    I don’t think Utils.alertWithMessage returns anything, it should block the script and continue executing on the next line, I don’t think it’s an async call but I could be misremembering. I have some prompter functions of my own (really borrowed/inspired by @daniel_leu) but I need to do some documentation work on them.

    I don’t think you can suppress the message, I believe it’s intended to be a security measure which is relatively common best practices these days.

    October 5, 2020 at 1:52 AM #42134

    David Gold
    Participant

    OK thanks. At the moment the alert from Utils.alertWithMessage comes up but before I have a chance to read it or even press OK it executes the subsequent task. I ideally wanted it only to do that after I hit the OK button on the alert. Not sure if that’s possible.

    October 5, 2020 at 5:57 PM #42140

    Sam Moffatt
    Participant

    I think that’s part of the async handling, the prompter confirm might be more up your alley. Let me grab some time later tonight to do some docs for it because it should help you out.

    October 19, 2020 at 1:09 AM #42302

    Sam Moffatt
    Participant

    Ok, I finally got around writing this up, you can grab the script from my GitHub and there is also sample execution code as well. If you use my updated Script Manager you can pull down everything. The GitHub page has an example of a confirmation dialog done async and also a text input. You can use a similar approach of wrapping your method in an async block and it should execute neatly.

    Here’s a little broader of an example:

    async function comboExample() {
    	let confirmed = await promptConfirm('Are you sure?');
    	console.log(confirmed);
    
    	let details = await promptText('Enter Value', 'Location:');
    	console.log(details);
    
    	Utils.alertWithMessage("Sample messsage", "Message text");
    	console.log('Sample message');
    }
    
    comboExample();
    
    October 19, 2020 at 5:18 AM #42304

    David Gold
    Participant

    Thanks so much. Will have a play around with it over the next day or so.

Viewing 17 reply threads

You must be logged in to reply to this topic.