Commerce categories of the Cricket SCA web site are updated only when “cricket” value is selected as one of the carriers.
Rest remain as same.
Script “bwv_cricket_phone_model_manager.js” scripted record: phone model.
/**
* @method managePhoneModel [ENTRY POINT - BEFORE SUBMIT]
* @desc manage the edit, copy and delete events of a phone model
* @param type
*/
function managePhoneModel(type){
try{
switch (type.toString()) {
case 'copy':
var phoneModel = nlapiGetNewRecord();
manageCreatePhoneModel(phoneModel);
break;
case 'edit':
var oldRecord = nlapiGetOldRecord();
var phoneModel = nlapiGetNewRecord();
manageEditPhoneModel(phoneModel,oldRecord);
break;
case 'delete':
manageDeletePhoneModel();
break;
}
}catch(e){
nlapiLogExecution('ERROR', "General Error on managePhoneModel(). Code : " ,e);
}
}
/**
* @method managePhoneModelCreation
* @description manage the create event of a phone model
* @param type
*/
function managePhoneModelCreation(type){
try{
switch (type.toString()) {
case 'create':
var phoneModel = nlapiGetNewRecord();
manageCreatePhoneModel(phoneModel);
break;
}
}catch(e){
nlapiLogExecution('ERROR', "General Error on managePhoneModel(). Code : " ,e);
}
}
/**
* @method manageCreatePhoneModel
* @desc manage the creation of a New Phone model
* Steps :
* 1 - Create a new commerce category with the phone model information
* 2 - Find a commerce category with the same name of the brand
* 3 - Assign the new commerce category created as sub-category of the step 2
* 4 - Find all commerce category with the same name of the carriers (multi-select field)
* 5 - Assign the new commerce category created as sub-category of the 4
*
* @param phoneModelRecord
*/
function manageCreatePhoneModel(phoneModelRecord){
// Get the phone model values
var phoneModelValues = getPhoneModelValues(phoneModelRecord);
var Carrier= phoneModelValues.carrier;
nlapiLogExecution('DEBUG', "Carrier", Carrier);
if(Carrier==true) {
var newCommerceCategory = nlapiCreateRecord('commercecategory');
setPhoneModelValues(newCommerceCategory,phoneModelValues);
nlapiLogExecution('ERROR', "Create", "Create");
// Create a new commerce category using the values of the new phone model
// Set the phone model values on the new commerce category
// Get the database id of the new commerce category
var newCommerceCategoryId = nlapiSubmitRecord(newCommerceCategory);
/*var carrier = phoneModelValues.carrier;
var cricket_carrier =chk_cricket(carrier);
if(cricket_carrier)
{
nlapiLogExecution('ERROR', "carrier status", cricket_carrier);
var newCommerceCategoryId = nlapiSubmitRecord(newCommerceCategory);
}*/
nlapiLogExecution('ERROR', "NEW COMMERCE CATEGORY", newCommerceCategoryId);
// Link the parent commerce category(using the brand and carrier) with the new one
linkWithParentCategories(newCommerceCategoryId,phoneModelRecord);
}
else{
nlapiLogExecution('DEBUG', "status","no cricket carries is selected" );
}
}
/**
* @method manageEditPhoneModel
* @desc manage the edition of a Phone model
* Steps :
* 1 - Find a commerce category with the same name of the phone model edited, and update that category using the new values of the phone model
* 2 - Check if Brand or Carriers change, if change :
* 2.1 : Find a commerce category with the same name of the brand
* 2.2 : Remove the commerce category created as sub-category of the step 2.1
* 2.3 : Find a commerce category with the same name of the Carriers
* 2.4 : Remove the commerce category created as sub-category of the step 2.3
* 2.5 : Make the link with the new brand and carriers
*
* @param phoneModelRecord
*/
function manageEditPhoneModel(phoneModelRecord,oldRecord){
// Find a commerce category with the name of the phone model
var commerceCategory = findCommerceCategory(phoneModelRecord.getFieldValue("name"));
// If exists
if (commerceCategory){
// Get the values of the phone model
var phoneModelValues = getPhoneModelValues(phoneModelRecord);
// Set the values of the phone model
setPhoneModelValues(commerceCategory,phoneModelValues);
// Get the database id of the commerce category
var commerceCategoryId = nlapiSubmitRecord(commerceCategory);
nlapiLogExecution('ERROR', "Edited Commerce Category" , commerceCategoryId);
// Process old record (unlink with previows parent commerce categories)
var mustProcessOldRecord = processOldRecord(oldRecord,phoneModelRecord);
// nlapiLogExecution('ERROR', "old brand" ,JSON.stringify(oldRecord.getFieldText("custrecord_bw_brand")));
// nlapiLogExecution('ERROR', "new brand" ,JSON.stringify(phoneModelRecord.getFieldText("custrecord_bw_brand")));
//
//
// nlapiLogExecution('ERROR', "oldParentCarriersNames" , JSON.stringify(oldRecord.getFieldTexts("custrecord_bw_carrier")));
// nlapiLogExecution('ERROR', "newParentCarriersNames" , JSON.stringify(phoneModelRecord.getFieldTexts("custrecord_bw_carrier")));
// If brand or carrier change we need to update the link between the categories
if (mustProcessOldRecord){
var usageAfterProcessOldRecord = nlapiGetContext().getRemainingUsage();
nlapiLogExecution('ERROR', "USAGE AFTER OLD RECORD" ,usageAfterProcessOldRecord);
// Link the parent commerce category(using the brand and carrier) with the new one
linkWithParentCategories(commerceCategoryId,phoneModelRecord);
var usage2 = nlapiGetContext().getRemainingUsage();
var usageAfterNewLinks = nlapiGetContext().getRemainingUsage();
nlapiLogExecution('ERROR', "USAGE AFTER NEW LINKS" ,usageAfterNewLinks);
}
}
}
/**
* @method manageDeletePhoneModel
* @desc when a phone model is deleted, we find the commerce category related to the phone model, the we remove them
*/
function manageDeletePhoneModel(){
var commerceCategory = findCommerceCategory(nlapiGetFieldValue("name"));
if (commerceCategory){
var commerceCategoryId = commerceCategory.getId();
nlapiLogExecution('ERROR', "commerceCategory",commerceCategoryId);
deleteComerceCategory(commerceCategoryId);
nlapiLogExecution('ERROR', "DELETED COMMERCE CATEGORY", commerceCategoryId);
}
}
/**
* @method linkWithParentCategories
* @param newCommerceCategoryId
* @param phoneModelRecord
* @desc Make the link between the parent commerce categories(Brand and Carriers) with the commerce cateogry (Phone Model)
*/
function linkWithParentCategories(newCommerceCategoryId,phoneModelRecord){
var carriersCategories = [];
// Brand name of the phone Model
var parentBrandName = phoneModelRecord.getFieldText("custrecord_bw_brand");
// Carrier names of the phone Model
// var parentCarriersNames = phoneModelRecord.getFieldTexts("custrecord_bw_carrier");
/*
parentCarriersNames.forEach(function(carrierName) {
var parentCarrierCategory = findCommerceCategory(carrierName);
parentCarrierCategory && carriersCategories.push(parentCarrierCategory);
});
*/
/*
// for each carrier, we make the link to the new commerce category created
carriersCategories.forEach(function(parentCarrierCategory) {
try{
linkWithParentCategory(newCommerceCategoryId,parentCarrierCategory);
}catch(e){
nlapiLogExecution('ERROR', "Error trying to link carriers commerce category", e);
}
});
*/
// Using the brand name, find a commerce category with the same name
var parentBrandCategory = findCommerceCategory(parentBrandName);
try{
// If we have found a commerce category, we make the link
parentBrandCategory && linkWithParentCategory(newCommerceCategoryId,parentBrandCategory);
}catch(e){
nlapiLogExecution('ERROR', "Error trying to link Brand commerce category", e);
}
}
/**
* @method linkWithParentCategory
* @param subCategoryId
* @param parentCategory
* @desc link a caregory with a subcategory
*/
function linkWithParentCategory(subCategoryId,parentCategory){
parentCategory.selectNewLineItem('subcategories');
parentCategory.setCurrentLineItemValue('subcategories', 'subcategory', subCategoryId);
parentCategory.commitLineItem('subcategories');
var parentId = nlapiSubmitRecord(parentCategory);
nlapiLogExecution('ERROR', "NEW LINK", "PARENT = " + parentId + " , SUBCATEGORY = " + subCategoryId);
}
/**
* @method processOldRecord
* @param oldPhoneModel
* @param newPhoneModel
* @desc
*/
function processOldRecord(oldPhoneModel,newPhoneModel){
var oldCarriersCategories = [];
// Old Brand Name
var oldParentBrandName = oldPhoneModel.getFieldText("custrecord_bw_brand");
// Old Carriers Names
var oldParentCarriersNames = oldPhoneModel.getFieldTexts("custrecord_bw_carrier");
// New Brand Name
var newParentBrandName = newPhoneModel.getFieldText("custrecord_bw_brand");
// New Carriers Names
// var newParentCarriersNames = newPhoneModel.getFieldTexts("custrecord_bw_carrier");
/*
oldParentCarriersNames.forEach(function(carrierName) {
var parentCarrierCategory = findCommerceCategory(carrierName);
parentCarrierCategory && oldCarriersCategories.push(parentCarrierCategory);
});
*/
// If the brand change, we have to unlink from previews parent Categories
var brandChange = (oldParentBrandName.toString() != newParentBrandName.toString());
// var carriersChange = !arraysEqual(oldParentCarriersNames,newParentCarriersNames);
// if (brandChange || carriersChange){
if (true){
var oldParentBrandCategory = findCommerceCategory(oldParentBrandName);
var phoneModelCategory = findCommerceCategory(newPhoneModel.getFieldValue("name"));
if (oldParentBrandCategory && phoneModelCategory){
removeFromOldParentCategories(phoneModelCategory.getId(),oldParentBrandCategory);
}
/*
// for each carrier, we make the link to the new commerce category created
oldCarriersCategories.forEach(function(oldParentCarrierCategory) {
try{
removeFromOldParentCategories(phoneModelCategory.getId(),oldParentCarrierCategory);
}catch(e){
nlapiLogExecution('ERROR', "Error trying to UNLINK categories" , e);
}
});
*/
// If the brand change, we return true to set the new brand, in other case return false
return true
}
return false
}
/**
* @method removeFromOldParentCategories
* @param subcategoryId
* @param parentCategory
* @desc Unlink parent commerce categories (Brand and Carriers) with a commerce category (Phone Model)
*/
function removeFromOldParentCategories(subcategoryId,parentCategory){
var categoriesCounter = parentCategory.getLineItemCount("subcategories");
var i = 1,founded = false;
while (i <= categoriesCounter && founded == false){
var item = parseInt(parentCategory.getLineItemValue('subcategories', 'subcategory', i));
if(parseInt(subcategoryId) == item){
parentCategory.removeLineItem('subcategories',i);
categoriesCounter = parentCategory.getLineItemCount("subcategories");
nlapiLogExecution('ERROR', "remove sucategoryid = " + subcategoryId, "parentCategory record = " + parentCategory.getId());
founded = true;
}else
i++
}
nlapiSubmitRecord(parentCategory);
}
// Start Utilities
/**
* @method findCommerceCategory
* @param name
* @desc return (if exists) a commerce category with the name on the parameter
* @returns {*}
*/
function findCommerceCategory(name){
var filters = [],
columns = [],
commerceCategory;
filters.push(new nlobjSearchFilter('name', null, 'is', name));
filters.push(new nlobjSearchFilter('catalog', null, 'is', 3));
var commerceCategories = nlapiSearchRecord('commercecategory', null, filters, columns);
if (commerceCategories && commerceCategories.length > 0 ){
commerceCategory = nlapiLoadRecord("commercecategory",commerceCategories[0].id);
}
return commerceCategory
}
/**
* @method deleteComerceCategory
* @desc delete a commerce cateogry with the id on the parameter
* @param commerceCategoryId
*/
function deleteComerceCategory(commerceCategoryId){
nlapiDeleteRecord("commercecategory",commerceCategoryId);
}
/**
* @methid getPhoneModelValues
* @param phoneModel
* @returns {{name: string, description: string, pageTitle: string, pageHeading: string, url: string, pageBanner: string, thumbnail: string, metaKeyWords: string, metaDescription: string, isInActive: string, carrier: string, brand: string, defaultModel: string, cricketFlag: string, model: string}}
*/
function getPhoneModelValues(phoneModel){
var isInActive = phoneModel.getFieldValue("isinactive");
var model = phoneModel.getFieldText("custrecord_bw_model");
var name = phoneModel.getFieldValue("name");
var carrier = phoneModel.getFieldText("custrecord_bw_carrier");
if(carrier) {
var carrier = carrier.split("\u0005");
nlapiLogExecution('ERROR', "carrier",carrier);
for(var i = 0 ; i <carrier.length ; i++){
if(carrier[i]=='Cricket'){
carrier= true;
}
else{
carrier= false;
}
}
}
var brand = phoneModel.getFieldText("custrecord_bw_brand");
var defaultModel = phoneModel.getFieldValue("custrecord_rb_phonemodel_default");
var cricketFlag = phoneModel.getFieldValue("custrecord_rb_cricketflag");
var pageTitle = phoneModel.getFieldValue("custrecord_rb_phonemodelpagetitle");
var pageHeading = phoneModel.getFieldValue("custrecord_rb_phonemodelpageheading");
var url = phoneModel.getFieldValue("custrecord_rb_phonemodelurlfragment");
var pageBanner = phoneModel.getFieldValue("custrecord_rb_phonemodelpagebanner");
var thumbnail = phoneModel.getFieldValue("custrecord_rb_phonemodelthumbnail");
var metaKeyWords = phoneModel.getFieldValue("custrecord_rb_phonemodelmetakeywords");
var metaDescription = phoneModel.getFieldValue("custrecord_rb_phonemodelmetadescription");
var description = phoneModel.getFieldValue("custrecord_rb_phonemodeldescription");
var seqno = phoneModel.getFieldValue("custrecord_rb_sequencenumphone");
return {
name : name,
description : description,
pageTitle : pageTitle,
pageHeading : pageHeading,
url : url,
pageBanner : pageBanner,
thumbnail : thumbnail,
metaKeyWords : metaKeyWords,
metaDescription : metaDescription,
isInActive : isInActive,
carrier : carrier,
brand : brand,
defaultModel : defaultModel,
cricketFlag : cricketFlag,
seqno : seqno,
model : model
}
}
function chk_cricket(data) {
//var carrier = data;
var carrier_arr = data;
var carrier_len = carrier_arr.length;
var result = false;
for (var i = 0; i < carrier_len; i++) {
if (carrier_arr[i] == 6) {
result = true;
}
}
return result;
}
/**
* @method setPhoneModelValues
* @param commerceCategory
* @param values
*/
function setPhoneModelValues(commerceCategory,values){
try {
values.name && commerceCategory.setFieldValue("name",values.name);
values.description && commerceCategory.setFieldValue("description",values.description);
values.pageTitle && commerceCategory.setFieldValue("pagetitle",values.pageTitle);
values.pageHeading && commerceCategory.setFieldValue("pageheading",values.pageHeading);
values.url && commerceCategory.setFieldValue("urlfragment",values.url);
values.pageBanner && commerceCategory.setFieldValue("pagebanner",values.pageBanner);
values.thumbnail && commerceCategory.setFieldValue("thumbnail",values.thumbnail);
values.metaKeyWords && commerceCategory.setFieldValue("metakeywords",values.metaKeyWords);
values.metaDescription && commerceCategory.setFieldValue("metadescription",values.metaDescription);
values.seqno && commerceCategory.setFieldValue("sequencenumber",values.seqno);
// (values.isInActive == "T") && commerceCategory.setFieldValue("isinactive",values.isInActive);
(values.isInActive == null) && commerceCategory.setFieldValue("displayinsite","T"); // DISPLAY IN WEBSITE IF THE PHONE MODEL IS NOT INACTIVE
(values.isInActive == "T") && commerceCategory.setFieldValue("displayinsite","F"); // DISPLAY IN WEBSITE IF THE PHONE MODEL IS NOT INACTIVE
commerceCategory.setFieldValue("catalog",3); // TODO
values.brand && commerceCategory.setFieldText("primaryparent",values.brand); // TODO
// commerceCategory.setFieldValue("addtohead",values); // TODO
// commerceCategory.setFieldValue("sequencenumber",values); // TODO
return commerceCategory;
} catch (e) {
logme('E@setPhoneModelValues', e.message);
}
}
/**
* CHeck If two arrays are equals
* @param arr1
* @param arr2
* @returns {boolean}
*/
function arraysEqual(arr1,arr2){
if(arr1.length !== arr2.length)
return false;
for(var i = arr1.length; i--;) {
if(arr1[i].toLowerCase() !== arr2[i].toLowerCase())
return false;
}
return true;
}
/*******************************************************************************
* Log function
*
* @param TITLE
* @param DETAILS
* @returns Created By rosemol on 09-Feb-2018 12:39:35 PM
*/
function logme(TITLE, DETAILS) {
nlapiLogExecution('DEBUG', TITLE, DETAILS);
}