Show category based on domain

If category needs to show based on domain, then we can achieve it by using extension in SCA.

Create a field in the commerce category to select the domains. And by using saved search get the categories belong to the given domain. Then extend the Categories’ in ‘suitescript’ to show the result categories in the domain.Adding sample code below.

Suitescript 
// for getting the categories for the given domain using saved search.

define('JJ.categorysite.category'
,	[
		'category.ServiceController','SC.Model',
		'SC.Models.Init',
		'Backbone.Validation',
		'underscore',
        'Categories'
	]
,	function (
		categoryServiceController,
		SCModel,
		ModelsInit,
		BackboneValidation,
		_,
        CategoriesModel
	)
{
	'use strict';
	return SCModel.extend({
        get: function get(baseurl) {
            try {
                var productFeatures = {};
                var field = ModelsInit.customer.getFieldValues();
                
                // console.log('field', field)
                console.log("baseurl", baseurl)             
				var commercecategorySearch = nlapiSearchRecord("commercecategory",null,
					[
					["catalog","anyof","1"],
                    "AND", 
                    ["custrecord_jj_domain","anyof",baseurl]
					], 
					[
					new nlobjSearchColumn("name"), 					
					new nlobjSearchColumn("primaryparent"), 
					new nlobjSearchColumn("pagetitle"), 
					new nlobjSearchColumn("urlfragment"),
					new nlobjSearchColumn("custrecord_jj_domain"),
                    new nlobjSearchColumn("internalid").setSort(false)
					]
					);
				// console.log('commercecategorySearch', commercecategorySearch)
                var result = JSON.stringify(commercecategorySearch);
				//  console.log('result', result)       
                result = JSON.parse(result);
				// console.log('result', result)  
                if (result.length > 0) {
                    
                    _.each(result, function(eachResult) {
                    //    console.log("eachResult",eachResult.columns)
                        var featuredata = {};
                        featuredata.internalid = (eachResult.id) || '';
                        console.log("featuredata.internalid",featuredata.internalid)
                        featuredata.name = (eachResult.columns && eachResult.columns.name) || '';
                        featuredata.primaryparent = (eachResult.columns && eachResult.columns.primaryparent) || '';
                        productFeatures[featuredata.internalid]=featuredata;
                      
                    });
                }
                console.log('result SEARCH', JSON.stringify(productFeatures))  
                return productFeatures;


            } catch (e) {
                console.log("Test", e)
                return []
            }
        }
    });
	
});
    
Suitescript

// Extending the categories and returning the value

define('JJ.categorysite.categoryentry'
    , [
        'SC.Model', 'SC.Models.Init', 'underscore', 'Categories', 'JJ.categorysite.category'
    ]
    , function (
        SCModel,
        ModelsInit,
        _,
        CategoriesModel, Categorysite
    ) {
        'use strict';

        console.error('inside suitescript entry22');
        _.extend(CategoriesModel, {

            getCategoryTree: _.wrap(CategoriesModel.getCategoryTree, function (fn) {
                try {
                    console.log("inside getCategoryTree");
                    var intvales = []; var temp;
                    var self=this;
                    // var originalupdate2 = _.toArray(arguments);
                    var originalupdate = fn.apply(this, _.toArray(arguments).slice(1));
                   // originalupdate=JSON.stringify(originalupdate);
                    var searchresult = Categorysite.get('145'); // saved search answer.125 is the required domain id
                    var resultfin=_.filter(originalupdate,function(data){
                    // console.log("data",data)                   
                        if(searchresult[data.internalid]){                           
                        data.categories=_.filter(data.categories,function (subcat){
                            // console.log("subcat",subcat.internalid) 
                             if(searchresult[subcat.internalid]){                              
                                subcat.categories=_.filter(subcat.categories,function (subsubcat){
                                    // console.log("subsubcat",subsubcat.internalid) 
                                    if(searchresult[subsubcat.internalid]){
                                        subsubcat.categories=_.filter(subsubcat.categories,function (subsubcatcat){
                                            // console.log("subsubcatcat",subsubcatcat.internalid) 
                                            if(searchresult[subsubcatcat.internalid]){                              
                                                 return true                                              
                                             }
                                        })
                                        return true
                                    }                 
                                 })
                               return true 
                             }              
                            });
                         return true
                         }                   
                     });
                console.log("result at the end",resultfin)                    
                } catch (err) {
                    console.log("error in getCategoryTree", err);
                }
                originalupdate = resultfin;                
                return originalupdate

            })
        });
    });

Leave a comment

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