How to search a checkbox pick list

Viewing 5 reply threads
  • Author
    Posts
  • March 10, 2021 at 6:35 AM #43781

    Ray Robillard
    Participant

    Hi !

    I’ve a movie database in which there’s a Tag pick list, made of checkboxes. I want to find how many different records have a certain item in this pick list checked.

    So thinking the pick list returned something I wasn’t sure of, I tried to get the type of the object, but all I am getting is undefined.

    var value = movie_form_records[index].getFieldValue(movie_form_tags_id);
    console.log(typeof value);
    (console output is “undefined”)

    So when I try this :
    var pos = value.search(“Feel good”);

    It never returns any item, despite the fact that some of the records have this tag. I know this because using the search filter, I can find several movies for which this tag is checked.

    My loop goes through all the records and make the following test :
    if (pos >-1) {
    nombre++;
    }

    But when I am running the code, I am getting the following output in the console :

    2021-03-10, 8:17:57 AM / Movies / Feel good
    Feel good: TypeError: undefined is not an object (evaluating ‘value.search’), line:(null)
    Feel good: TypeError: undefined is not an object (evaluating ‘value.search’), line:(null)

    The whole code is :

    function Feel_Good() {

    var nombre = 0;
    var movie_form = document.getFormNamed(‘Movies’);

    var movie_form_tags_field = movie_form.getFieldNamed(‘Tags’);
    var movie_form_tags_id = movie_form_tags_field.getId();

    var movie_form_records = movie_form.getRecords();

    for (var index = 0, count = movie_form_records.length; index < count; index++) {

    var value = movie_form_records[index].getFieldValue(movie_form_tags_id);

    var pos = value.search(“Feel good”);

    if (pos >-1) {
    nombre++;
    }

    }

    return nombre;
    }

    So.. what am I doing wrong ? I’m a total newbie at JavaScript. So maybe I missed something somewhere.

    Thanks,

    Ray

    March 10, 2021 at 6:57 AM #43782

    Ray Robillard
    Participant

    Here’s a picture of the code and console

    March 11, 2021 at 12:44 AM #43794

    Sam Moffatt
    Participant

    The undefined type means that its not an object which is why when you do value.search you get an error because value is undefined and has no value. Since you’re iterating through all of your records, you need to check if the value you get back is undefined and skip because if the field isn’t set on a record, then you will get an undefined value back from the scripting API.

    Try something like this that ignores empty fields and also logs any records missing tags for further review:

    function Feel_Good() {
    
        var nombre = 0;
        var movie_form = document.getFormNamed(‘Movies’);
    
        var movie_form_tags_field = movie_form.getFieldNamed(‘Tags’);
        var movie_form_tags_id = movie_form_tags_field.getId();
    
        var movie_form_records = movie_form.getRecords();
    
        for (var index = 0, count = movie_form_records.length; index < count; index++) {
            var value = movie_form_records[index].getFieldValue(movie_form_tags_id);
    
            if (value) {
                var pos = value.search(“Feel good”);
    
                if (pos > -1) {
                    nombre++;
                }
            } else {
                console.log("Record missing tags: " + record.getUrl());
            }
    
        }
    
        return nombre;
    }
    
    March 12, 2021 at 7:32 AM #43809

    Ray Robillard
    Participant

    Thanks a lot, Sam ! I will try this tomorrow.

    Is there an online course on JavaScript you recommend ?

    March 12, 2021 at 8:33 PM #43811

    Sam Moffatt
    Participant

    I think the best option I’d go for is T L Ford’s JavaScript 101 which is focused from a Tap Forms users approach and I have some videos on my YouTube channel as well.

    There are plenty of web focused Javascript resources (Mozilla Developer Network, W3Schools) and a few resources leveraging node.js (also W3Schools as an example), there aren’t that many resources that focus on pure Javascript that I’ve found. A lot of the resources include platform specific stuff that really only relates to the use in web browsers or to node.js rather than the ECMAScript standard.</p>

    March 13, 2021 at 12:52 PM #43821

    Ray Robillard
    Participant

    Awesome !!! Thanks a lot Sam !

Viewing 5 reply threads

You must be logged in to reply to this topic.