penone
penone

Reputation: 792

Scripted search and looping - how do I show only 1 alert if multiple results?

I have a script that is triggering a search when a field is changed. The goal is that if the search finds a result to alter the user that this may be a possible duplicate. The issue I am having is that if there are more than 1 result it will show the alert as many times as there are results. How can I have it show the result only once?

/**
 *@NApiVersion 2.0
 *@NScriptType ClientScript
 */
define(['N/record', 'N/search', 'N/ui/dialog'], function(r, search, u) {
    function fieldChanged(context) {
        var recCurrent = context.currentRecord;
        var show = recCurrent.getValue({
            fieldId: 'custrecord_eym_er_show'
        });
        var client = recCurrent.getValue({
            fieldId: 'custrecord_eym_er_customer'
        });

        if ((context.fieldId == 'custrecord_eym_er_customer')) {
            var client = recCurrent.getValue({
                fieldId: 'custrecord_eym_er_customer'
            });
            console.log(client);
            var sv = search.create({
                type: "customrecord_eym_exhibit_reg",
                columns: [
                    search.createColumn({
                        name: "internalid",
                        label: "Internal ID"
                    })
                ],

                filters: [
                    ["custrecord_eym_er_show", "anyof", show],
                    "AND",
                    ["custrecord_eym_er_customer", "anyof", client]
                ]
            });

            var pagedData = sv.runPaged({
                pageSize: 1000
            });

            // iterate the pages
            for (var i = 0; i < pagedData.pageRanges.length; i++) {

                // fetch the current page data
                var currentPage = pagedData.fetch(i);
                // and forEach() thru all results
                currentPage.data.forEach(function(result) {

                    // you have the result row. use it like this....
                    var duplicate = result.getValue('internalid');
                    console.log(duplicate);
                    if (duplicate) {
                        alert('There is more than 1 entry for this client')
                    }
                });

            }

        }
    }
    return {
        fieldChanged: fieldChanged
    };
});

Upvotes: 0

Views: 407

Answers (3)

B. Assem
B. Assem

Reputation: 1078

There is a way to get the number of results returned by a search, in your case it seems you just want to show an alert of the search return at least one result so, you can use this code:

/**
 *@NApiVersion 2.0
 *@NScriptType ClientScript
 */
define(['N/record', 'N/search', 'N/ui/dialog'], function(r, search, u) {
    function fieldChanged(context) {
        var recCurrent = context.currentRecord;
        var show = recCurrent.getValue({
            fieldId: 'custrecord_eym_er_show'
        });
        var client = recCurrent.getValue({
            fieldId: 'custrecord_eym_er_customer'
        });

        if ((context.fieldId == 'custrecord_eym_er_customer')) {
            var client = recCurrent.getValue({
                fieldId: 'custrecord_eym_er_customer'
            });
            console.log(client);
            var sv = search.create({
                type: "customrecord_eym_exhibit_reg",
                columns: [
                    search.createColumn({
                        name: "internalid",
                        label: "Internal ID"
                    })
                ],

                filters: [
                    ["custrecord_eym_er_show", "anyof", show],
                    "AND",
                    ["custrecord_eym_er_customer", "anyof", client]
                ]
            });

            if(sv.runPaged().count >= 1) {
               alert('There is more than 1 entry for this client');
            }    
        }
    }
    return {
        fieldChanged: fieldChanged
    };
});

Upvotes: 0

Martha
Martha

Reputation: 764

Try using a summary level grouping on the saved search results. Ex: If you're getting multiple internal ids, where 0 mean no duplicates, and 1+ means duplicates try changing

        columns: [
            search.createColumn({
                name: "internalid",
                label: "Internal ID"
            })
        ],

to

        columns: [
            search.createColumn({
                name: "internalid",
                label: "Internal ID",
                summary: 'GROUP'
            })
        ],

And change

            var duplicate = result.getValue('internalid');

to

            var duplicate = result.getValue(name: 'internalid', summary: 'GROUP');

If I assumed incorrectly about the use of internal ids, add "Customer" or the desired grouped value as a column and use the summary grouping on that field.

Upvotes: 0

Mohammad Mattour
Mohammad Mattour

Reputation: 196

You can change:

 var pagedData = sv.runPaged({
                pageSize: 1000
            });

To

var pagedData = sv.run().getRanged({
                    start: 0,
                    end:1
                });

The variable pagedData is an array contains the result, if you have many duplication you will have only the First result.

Also another way you can simply introduce a variable to print it once like this:

 var printOnce = 0;
        for (var i = 0; i < pagedData.pageRanges.length; i++) {

            // fetch the current page data
            var currentPage = pagedData.fetch(i);
            // and forEach() thru all results
            currentPage.data.forEach(function(result) {

                // you have the result row. use it like this....
                var duplicate = result.getValue('internalid');
                console.log(duplicate);
                if (duplicate && printOnce==0) {
                    printOnce++;
                    alert('There is more than 1 entry for this client')
                }
            });

        }

I think the first approach is better since you don't need to retrieve all the data.

Upvotes: 1

Related Questions