Fetch Associated images in item record using suitescript

imageSearch: function(idArray) {
    var resultObj = {}
    try {
        var websiteInternalID = '2'

        var wesite = record.load({
            type: 'website',
            id: websiteInternalID
        })

        var dataArray = ['imageitemfielddelimiter', 'imagestructurefielddelimiter', 'imageuniquefield']

        var delimitor = wesite.getValue({
            fieldId: dataArray[0]

        }) || '_'
        var delimitor_1 = wesite.getValue({
            fieldId: dataArray[1]

        }) || '-'
        var uniqField = wesite.getValue({
            fieldId: dataArray[2]

        }) || 'itemid'

        log.debug('debug@uniqField', uniqField)

        var itemSearchObj = search.create({
            type: "item",
            filters: [
                ["internalid", "anyof", idArray],
                "AND",
                ["isonline", "is", "T"],
                "AND",
                ["parent", "noneof", "@NONE@"]
            ],
            columns: [
                search.createColumn({
                    name: "internalid",
                    label: "Internal ID"
                }),
                search.createColumn({
                    name: "formulatext",
                    formula: "CONCAT(CONCAT({parent." + uniqField + "}, '" + delimitor + "'),{custitem_jj_colour})",
                    label: "Formula (Text)"
                })

            ]
        });

        var searchResultCount = itemSearchObj.runPaged().count;
        log.debug("itemSearchObj result count", searchResultCount);
        itemSearchObj.run().each(function(result) {
            // .run().each has a limit of 4,000 results
            var item = result.getValue({
                name: "internalid"
            });
            var imageUrl = result.getValue({
                name: "formulatext"
            });

            resultObj[item] = imageUrl ? (main.fileSearch(imageUrl)) : '';
            return true;
        });

    } catch (e) {
        log.debug('error@imageSearch', e)
        log.error('error@imageSearch', e)
    }
    return resultObj;
},
fileSearch: function(imageName) {

    var name = ''
    try {

        var fileSearchObj = search.create({
            type: "file",
            filters: [
                ["name", "contains", imageName],
                "AND",
                ['availablewithoutlogin', 'is', 'T']

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

                })

            ]
        });

        var searchResultCount = fileSearchObj.runPaged().count;
        log.debug("itemSearchObj result count", searchResultCount);


        var resultSet = fileSearchObj.run();

        // now take the first portion of data.
        var currentRange = resultSet.getRange({
            start: 0,
            end: 5
        });

        if (currentRange.length > 0) {
            var fileSearchRec = currentRange[0]
            var name = fileSearchRec.getValue({
                name: "name"
            });
        }

    } catch (err) {
        log.debug('error@fileSearch', err)
        log.error('error@fileSearch', err)
    }
    return name;
}

Leave a comment

Your email address will not be published. Required fields are marked *