Custom Module as Extension

Jira Code: IBT-38

Using the product internal id as a filter, we create a search to files from NetSuite and display it in the website.

//JS
define(
    'ProductDetailsSupportFile.Collection'
,   [
        'ProductDetailsSupportFile.Model'
    ,   'Backbone.CachedCollection'

    ,   'Backbone'
    ,   'underscore'
    ,   'Utils'
    ]
,   function BrowseByGameSupportFileCollection (

        ProductDetailsSupportFileModel
    ,   BackboneCachedCollection

    ,   Backbone
    ,   _
    ,   Utils
    )
{
    'use strict';

    return BackboneCachedCollection.extend({

        model: ProductDetailsSupportFileModel,

        url: function(){
            return Utils.getAbsoluteUrl(getExtensionAssetsPath('services/ProductDetailsSupportFile.Service.ss'));
        }
    });
});
//JS
/*
	© 2017 NetSuite Inc.
	User may not copy, modify, distribute, or re-bundle or otherwise make available this code;
	provided, however, if you are an authorized user with a NetSuite account or log-in, you
	may use this code subject to the terms that govern your access and use.
*/

//@module Account
define('ProductDetailsSupportFile.Model'
,	[	'Backbone'
	,	'underscore'
	,	'Utils'
	]
,	function (
		Backbone
	,	_
	,	Utils
	)
{
	'use strict';

	//@class Account.Login.Model
	// Sends user input data to the login service
	// validating email and password before they are sent
	// [Backbone.validation](https://github.com/thedersen/backbone.validation)
	// @extend Backbone.Model
	return Backbone.Model.extend({

		//@property {String} urlRoot
		urlRoot: Utils.getAbsoluteUrl(getExtensionAssetsPath('services/ProductDetailsSupportFile.Service.ss'))

	});

});
//JS
define(
    'ProductDetailsSupportFile.View'
,   [
    	'SC.Configuration'

    ,   'product_details_support_file.tpl'

    ,   'ProductDetailsSupportFile.Collection'
    ,   'Backbone'
    ,   'underscore'
    ,   'jQuery'
    ,   'Utils'
    ]
,   function BrowseByGameView (

        Configuration

    ,   product_details_support_file_tpl

    ,   ProductDetailsSupportFileCollection
    ,   Backbone
    ,   _
    ,   jQuery
    ,   Utils
    )
{
    'use strict';

    return Backbone.View.extend({

        template: product_details_support_file_tpl

    ,   initialize: function initialize (options) {

            options = options || {};
        
            this.collection = options.collection || new ProductDetailsSupportFileCollection();

            this.collection.fetch({
                data: {
                    iteminternalid: this.model.get('item').internalid
                }
            }).then(_.bind(this.render, this))
        }

    ,   getContext:function getContext(){

            var documents = this.collection.models;
            var hasResults = documents && documents.length;

            return {

                hasResults: hasResults

            ,   documents : documents

            };

        }

    });
});
//SUITESCRIPT: SERVICE CONTROLLER
define(
    'JJ.CustomPDP.ProductDetailsSupportFile.ServiceController'
,	[
        'ServiceController'
    ,	'Application'
    ,	'ProductDetailsSupportFile.Models'
    ]
,	function ProductDetailsSupportFileServiceController (
        ServiceController
    ,	Application
    ,	ProductDetailsSupportFileModel
    )
{
    'use strict';

    return ServiceController.extend({
        name:'JJ.CustomPDP.ProductDetailsSupportFile.ServiceController'

    ,	get: function get () {
            var iteminternalid
            ,   results;

            try {

                iteminternalid = this.request.getParameter('iteminternalid');

                if (iteminternalid) {
                    results = ProductDetailsSupportFileModel.get(iteminternalid);
                } else {
                    results = []
                }

                this.sendContent(results, { cache: response.CACHE_DURATION_LONG });

            } catch (e) {
                console.warn('ProductDetailsSupportFiles.Service.ss::' + e.name, e);
                this.sendError(e);
            }
        }
    });
});
SUITESCRIPT: MODEL
define(
    'ProductDetailsSupportFile.Models'
,	[
        'SC.Model'
    ,   'JJ.CustomPDP.ProductDetailsSupportFile.ServiceController'
    ,   'Application'
    ,   'underscore'
    ]
,	function BrowseByGameModel (
        SCModel
    ,   ProductDetailsSupportFileServiceController
    ,   Application
    ,   _
)
{
    'use strict';

    return SCModel.extend({
        name: 'ProductDetailsSupportFile'
    ,   record: {
            type: 'customrecord_gd_support_files'
        ,   fieldsets: {
                list: {}
            ,   detailed: {
                    internalid: 'internalid'
                ,   document: 'custrecord_gd_support_document'
                }
            }
        }

    ,	get: function (id) {
            var self = this
            ,   fieldset = self.record.fieldsets.detailed
            ,   result = []
            ,   searchFilters
            ,   gameDataSearchResult;

            searchFilters = [
                new nlobjSearchFilter('isinactive', null, 'is', 'F')
            ,   new nlobjSearchFilter('custrecord_item', null, 'is', id)
            ];

            gameDataSearchResult = Application.getAllSearchResults(
                this.record.type,
                searchFilters,
                this.getColumnsArray(fieldset)
              );

            if (!gameDataSearchResult || !gameDataSearchResult.length) {
                return result;
            }

            _.each(gameDataSearchResult, function(doc){

                result.push({
                    id: doc.getValue(fieldset.internalid)
                ,   name:doc.getText(fieldset.document)
                ,   url : self.getDocUrl(doc, fieldset)
                });

            });

            return result;
        }

    ,   getColumnsArray: function getColumnsArray (fieldset) {
            var columns;

            columns = [];

            _.each(_.keys(fieldset), function each (key) {
                columns.push(new nlobjSearchColumn(fieldset[key]));
            }, this);

            return columns;
        }

    ,   getDocUrl: function getDocUrl (doc, fieldset) {

            var url = nlapiResolveURL('suitelet', 'customscript_get_file_url','customdeploy_get_file_url', true);
            var params = '&filename=' + doc.getText(fieldset.document) + '&fileid=' + doc.getValue(fieldset.document);
            var response = nlapiRequestURL(url + params);

            var body = response.getBody();

            try {

                var URLParsed = JSON.parse(body);

                return URLParsed.url;

            } catch (e) {
                return null
            }
        }
    });
});
//TEMPLATE
<div class="product-details-specsinformation-game-compatibility">
    <div class="product-details-support-files-list">
        {{#if hasResults }}
        <div class="product-details-specsinformation-game-compatibility-header">
                    <h2 style="margin-bottom: 15px;">SUPPORT FILES</h2>
                </div>
            <ul class="list-group list-group-flush">
                {{#each documents }}
                    <li class="list-group-item">
                        <a href="{{this.url}}" data-touchpoint="home" target="_blank">
                            <i class="product-details-support-files-icon-pdf"></i>
                            {{ this.name }}
                        </a>
                    </li>
                {{/each}}
            </ul>
        {{ else }}
            <!--<p style="text-align: center;">-->
                <!--There are not support's documents.-->
            <!--</p>-->
        {{/if}}
    </div>
.product-details-support-files{

    font-size: 15px;

    .list-group {
        text-align: center;
    }

    .product-details-support-files-icon-pdf {
        @extend .fa;
        @extend .icon-pdf;
    }
}

Leave a comment

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