Name
sn_install_base.InstallBaseUtilSNC
Description
No description available
Script
var InstallBaseUtilSNC = Class.create();
InstallBaseUtilSNC.prototype = {
initialize: function() {
this.SPIBFilter = new sn_install_base.SoldProductAndInstallBaseFilter();
},
getMySPsFromAdditionalAccount: function(accessType) {
var RC = global.CSMRelationshipConstants;
if(!gs.hasRole(RC.ROLE_SOLD_PRODUCT_AUTH_CONTACT)) return [];
var params = {};
params[RC.SKIP_BEFORE_QUERY_FILTER] = true;
params[RC.NO_PERSONA_QUERY] = true;
params[RC.IS_ACCOUNT_QUERY] = true;
params[RC.IS_CONTACT_SOLD_PRODUCT] = true;
return (new global.CSMRelationshipUtils().getMyEntities(null, global.CSMBaseConstants.SOLD_PRODUCT_RELATED_PARTY, RC.FIELD_SOLD_PRODUCT, (accessType || RC.ACCESS.READ), params) || []);
},
getMySPsForAuthorizedAccount: function(account, contact, accessType) {
var RC = global.CSMRelationshipConstants;
var params = {};
params[RC.SKIP_BEFORE_QUERY_FILTER] = true;
params[RC.NO_PERSONA_QUERY] = true;
params[RC.NO_PERSONA_ACCOUNT_QUERY] = true;
params[RC.IS_ACCOUNT_QUERY] = true;
params[RC.IS_CONTACT_SOLD_PRODUCT] = true;
params[RC.ADDITIONAL_ENCODED_QUERY] = "account=" + account;
params[RC.SKIP_ROLE_CHECK] = true;
return (new global.CSMRelationshipUtils().getMyEntities(null, global.CSMBaseConstants.SOLD_PRODUCT_RELATED_PARTY, RC.FIELD_SOLD_PRODUCT, (accessType || RC.ACCESS.READ), params) || []);
},
getMySPsFromAdditionalContacts: function(accessType) {
var RC = global.CSMRelationshipConstants;
if(!gs.hasRole(RC.ROLE_SOLD_PRODUCT_AUTH_CONTACT)) return [];
var params = {};
params[RC.SKIP_BEFORE_QUERY_FILTER] = true;
params[RC.IS_CONTACT_SOLD_PRODUCT] = true;
return (new global.CSMRelationshipUtils().getMyEntities(null, global.CSMBaseConstants.SOLD_PRODUCT_RELATED_PARTY, RC.FIELD_SOLD_PRODUCT, (accessType || RC.ACCESS.READ), params) || []);
},
getMyPrimaryAndAdditionalSPs: function() {
var sp = [];
//add SPs to array, where sp contact is me
sp.push(this.getMySPsFromPrimaryContacts());
//add SPs to array, where sp additional contact is me
sp.push(this.getMySPsFromAdditionalContacts());
return sp;
},
getMySPsFromPrimaryContacts: function() {
var sp = [];
//add SPs to array, where sp contact is me
var spGr = new GlideRecord(global.CSMBaseConstants.SOLD_PRODUCT);
spGr.addQuery('contact', gs.getUserID());
spGr._skip_before_query_filter = true;
spGr.query();
while (spGr.next()) {
sp.push(spGr.getUniqueValue());
}
return sp;
},
getConsumerRelationshipInstallBases: function(){
var installBases = [];
var gr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
if(this.isHouseHoldPluginActive()){
gr.addNullQuery("household");
}
// consumer in my consumer relationships
gr.addEncodedQuery("consumerDYNAMICcd5fcbf60fe210103ff81b41ff767e47");
gr._skip_before_query_filter = true;
gr.query();
while(gr.next()){
installBases.push(gr.sys_id+"");
}
return installBases;
},
getConsumerRelationshipSoldProducts: function(includeHouseholdProducts){
var key = 'consumers_relationship_sold_product_' + gs.getUserID();
var consumerRelSP = GlideAppCache.get(key);
if (!gs.nil(consumerRelSP))
return consumerRelSP;
var soldProducts = [];
var gr = new GlideRecord(global.CSMBaseConstants.SOLD_PRODUCT);
if(!includeHouseholdProducts && this.isHouseHoldPluginActive()){
gr.addNullQuery("household");
}
// consumer in my consumer relationships
gr.addEncodedQuery("consumerDYNAMICcd5fcbf60fe210103ff81b41ff767e47");
gr._skip_before_query_filter = true;
gr.query();
while(gr.next()){
soldProducts.push(gr.sys_id+"");
}
GlideAppCache.put(key, soldProducts);
return soldProducts;
},
getMyInstallBaseItems: function(pointerEncodedQuery, cacheKey) {
var util = new sn_install_base.SoldProductAndInstallBaseUtil();
return (util.getInstallBaseItems(pointerEncodedQuery, cacheKey) || []);
},
getMyInstallBaseItemsForSPContact: function() {
return this.getMyInstallBaseItems("sold_product.contactDYNAMIC3a3138280f3e10103ff81b41ff767e9f");
},
getIBsFromMyNonManagedAccount: function() {
var ib = [];
var ibGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
ibGr.addQuery("accountDYNAMICee12375877267010d3ef07dc7d5a995d");
ibGr._skip_before_query_filter = true;
ibGr.query();
while (ibGr.next()) {
ib.push(ibGr.getUniqueValue());
}
return ib;
},
//Delete related Additional Contacts from sold product, when a contact relationship record is deleted
deleteContactsAddedByContactRelationship: function(contactRelationGr) {
if (gs.nil(contactRelationGr)) return;
var curAccount = contactRelationGr.company;
var curContact = contactRelationGr.contact;
var contactPrimaryAccount = curContact ? curContact.account + "" : "";
//Invalid configuration
if (gs.nil(curAccount) || gs.nil(curContact) || gs.nil(contactPrimaryAccount)) return;
//Do not delete Additional Contacts from sold products associated to this account, if the contact is part of the same account or a partner account
if (curAccount == contactPrimaryAccount) return;
var partnerAccounts = new global.CSManagementUtils().getPartnerAccounts(curAccount) || [];
if (partnerAccounts.indexOf(contactPrimaryAccount) >= 0) return;
var spcontacts = new GlideRecord(global.CSMRelationshipConstants.RELATIONSHIP_SOLD_PRODUCT_RELATED_PARTY);
spcontacts.addQuery('sold_product.account', curAccount);
spcontacts.addQuery('contact', curContact);
spcontacts.deleteMultiple();
},
//Delete related Additional Contacts from sold product, when an Account relationship record is deleted
deleteContactsAddedByAccountRelationship: function(accountRelationGr) {
if (gs.nil(accountRelationGr)) return;
var toCompany = accountRelationGr.to_company;
var fromCompany = accountRelationGr.from_company;
//Invalid configuration
if (gs.nil(toCompany) || gs.nil(fromCompany)) return;
var spcontacts = new GlideRecord(global.CSMRelationshipConstants.RELATIONSHIP_SOLD_PRODUCT_RELATED_PARTY);
spcontacts.addQuery('sold_product.account', toCompany);
var qc = spcontacts.addQuery('contact.account', fromCompany);
qc.addOrCondition('account', fromCompany);
spcontacts.deleteMultiple();
},
getConsumerSoldProducts: function(accessLevel, additionalParams) {
var util = new sn_install_base.SoldProductAndInstallBaseUtil();
return (util.getConsumerSoldProducts(accessLevel, additionalParams) || []);
},
/* a sold product S is valid for given consumer C if
1. S is created for C
2. S is created for household where household.head = C
*/
getAdditionalValidSoldProducts: function(consumerId, validSoldProducts) {
if (!consumerId) return;
return this.SPIBFilter.getAdditionalValidSoldProducts(consumerId, validSoldProducts);
},
/*
Use this method only for sold products belonging to accounts with contact restriction on.
Returns string containing comma seperated values of contacts with full access to Sold Product for which access restriction is on.
Granular role check is not done in this method.
*/
getSPContactsHavingFullAccessWithAccessRestriction: function(sold_product) {
if (gs.nil(sold_product)) return '';
var contacts = "";
var gr = new GlideRecord(global.CSMBaseConstants.SOLD_PRODUCT);
if (gr.get(sold_product)) {
if (!gs.nil(gr.contact))
contacts += gr.contact;
if (!gs.nil(gr.additional_users)) {
/*
Currently, we only have valid contacts populated in sn_install_base_sold_product.additional_users.
If this changes in future and internal users are also getting added,
we need to add additional checks to filter out only contacts from there
*/
contacts += (contacts.length > 0 ? (','+gr.additional_users) : gr.additional_users);
}
}
return contacts;
},
getSPAuthorizedAccounts: function(soldProduct){
var authorizedAccounts = [];
if(gs.nil(soldProduct)) return authorizedAccounts;
var RC = global.CSMRelationshipConstants;
var params = {};
params[RC.IS_CONTACT_SOLD_PRODUCT] = true;
params[RC.ADDITIONAL_ENCODED_QUERY] = 'sold_product='+soldProduct + '^responsibility.type=customer_account';
params[RC.NO_PERSONA_QUERY] = true;
params[RC.SKIP_ROLE_CHECK] = true;
authorizedAccounts = new global.CSMRelationshipUtils().getMyEntities(null, RC.RELATIONSHIP_SOLD_PRODUCT_RELATED_PARTY, RC.FIELD_ACCOUNT, RC.ACCESS.READ, params);
return authorizedAccounts;
},
/*
Returns string containing comma seperated list of consumers with full access to Sold Product.
Granular role check is not done in this method.
*/
getSPConsumersHavingFullAccess: function(sold_product) {
if (gs.nil(sold_product)) return '';
var consumers = "";
var gr = new GlideRecord(global.CSMBaseConstants.SOLD_PRODUCT);
if (gr.get(sold_product)) {
if (!gs.nil(gr.consumer))
consumers += gr.consumer;
if (!gs.nil(gr.additional_consumers)) {
consumers += (consumers.length > 0 ? (','+gr.additional_consumers) : gr.additional_consumers);
}
if(GlidePluginManager.isActive(global.CSMBaseConstants.HOUSEHOLD_PLUGIN)){
if(!gs.nil(gr.household) && !gs.nil(gr.household.head)){
consumers += (consumers.length > 0 ? (','+gr.household.head) : gr.household.head);
}
}
}
return consumers;
},
//Check is valid household member or not
addHouseHoldRelatedQuery: function(conSoldProductGr, consumerId) {
if (!consumerId || !conSoldProductGr || !conSoldProductGr.isValid()) return;
return this.SPIBFilter.addHouseHoldRelatedQuery(conSoldProductGr, consumerId);
},
isValidAdditionalConsumerOfSP: function(consumerId, soldProduct, accessLevel, household) {
if (!consumerId || !soldProduct) return false;
return this.SPIBFilter.isValidAdditionalConsumerOfSP(consumerId, soldProduct, accessLevel, household);
},
//Check if there is atleast one common valid sold product b/w consumers.
isCoConsumer: function(consumerId, loggedInConsumerId) {
if (!consumerId || !loggedInConsumerId)
return false;
if (consumerId == loggedInConsumerId)
return true;
var conValidSP = this.SPIBFilter.getValidSoldProducts(consumerId);
if (!conValidSP)
return false;
var mapOfconValidSP = {};
for (var i = 0, len = conValidSP.length; i < len; i++)
mapOfconValidSP[conValidSP[i]] = true;
var loggedInConValidSP = this.SPIBFilter.getValidSoldProducts(loggedInConsumerId);
if (!loggedInConValidSP)
return false;
for (i = 0, len = loggedInConValidSP.length; i < len; i++)
if (mapOfconValidSP[loggedInConValidSP[i]])
return true;
},
getMyPrimaryConsumerSPs: function() {
var sp = [];
//add SPs to array, where sp consumer is me
var spGr = new GlideRecord(global.CSMBaseConstants.SOLD_PRODUCT);
spGr.addQuery('consumer', new global.CSManagementUtils().getConsumerId());
spGr._skip_before_query_filter = true;
spGr.query();
while (spGr.next())
sp.push(spGr.getUniqueValue());
return sp;
},
generateCacheKeyForSPConsumer: function(accessLevel, additionalParams) {
var spCacheKey;
if (gs.nil(additionalParams))
additionalParams = {};
//key for sold product consumer based on additional consumer id, access level, additional responsibilities and is sold product viewer or not.
spCacheKey = "SPC" + additionalParams.consumerId + accessLevel;
if (additionalParams.isSPViewer)
spCacheKey += "V";
if (additionalParams.responsibilities)
spCacheKey += "R";
return spCacheKey;
},
/*
* For the given install base item, fetch all the descendants in the hierarchy.
*
* @param ibItemSysId - Install Base Item SysId
*
* @return Array - Install Base Item sysIds
*/
getAllInstallBaseItemDescendants: function(ibItemSysId, skipRootCheck) {
var ibItemArray = [];
if (gs.nil(ibItemSysId))
return ibItemArray;
if (skipRootCheck) {
this._getAllDescendants(ibItemSysId, ibItemArray);
} else {
var installBaseGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
installBaseGr.addQuery("sys_id", ibItemSysId);
installBaseGr.query();
if (installBaseGr.next()) {
if (gs.nil(installBaseGr.getValue("root")))
return this.getAllRootDescendants(ibItemSysId);
else
this._getAllDescendants(ibItemSysId, ibItemArray);
}
}
return ibItemArray;
},
/*
* recursive function to get the install base item's descendants.
*/
_getAllDescendants: function(ibItemSysId, ibItemArray) {
var installBaseGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
installBaseGr.addQuery("parent", ibItemSysId);
installBaseGr.query();
while (installBaseGr.next()) {
var currentSysId = installBaseGr.getUniqueValue() + '';
ibItemArray.push(currentSysId);
this._getAllDescendants(currentSysId, ibItemArray);
}
},
/*
* Get All the Root install base item's descendants.
*/
getAllRootDescendants: function(rootSysId) {
var ibItemArray = [];
var installBaseGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
installBaseGr.addQuery("root", rootSysId);
installBaseGr.query();
while (installBaseGr.next()) {
ibItemArray.push(installBaseGr.getUniqueValue() + '');
}
return ibItemArray;
},
/*
* returns true if household plugin is active.
*/
isHouseHoldPluginActive: function() {
return GlidePluginManager.isActive(global.CSMBaseConstants.HOUSEHOLD_PLUGIN);
},
/*
* returns true if service organization plugin is active.
*/
isServiceOrganizationPluginActive: function() {
return GlidePluginManager.isActive(global.CSMBaseConstants.SERVICE_ORGANIZATION_PLUGIN);
},
/*
* returns true if B2B2C plugin is active.
*/
isB2B2CPluginActive: function() {
return GlidePluginManager.isActive(global.CSMBaseConstants.B2B2C_PLUGIN);
},
/*
* returns the owner fields of Install base items.
*
* Expected owner fields: Account, Contact, Consumer, household, and service organization.
*/
getInstallBaseItemOwnerFields: function() {
var fields = ['account', 'contact', 'consumer'];
if (this.isHouseHoldPluginActive())
fields.push('household');
if (this.isServiceOrganizationPluginActive())
fields.push('service_organization');
return fields;
},
/*
* returns the owner(s) of Install base items.
*
* Expected owner fields: Account, Contact, Consumer, household, and service organization.
*/
getInstallBaseItemOwner: function(current) {
var response = [];
var fieldNames = this.getInstallBaseItemOwnerFields();
for (var i = 0, len = fieldNames.length; i < len; i++) {
var val = current[fieldNames[i]];
if (!gs.nil(val)) {
response.push(fieldNames[i]);
}
}
return response;
},
/*
* returns the array of my persona based accessible additional Install base items.
*
*/
getMyAdditionalInstallBaseItems: function(accessType) {
var RC = global.CSMRelationshipConstants;
var key = "additional_install_base_items_" + gs.getUserID() + (gs.nil(accessType) ? "" : accessType);
var additionalIBs = GlideAppCache.get(key);
if (!gs.nil(additionalIBs))
return additionalIBs;
var params = {};
params[RC.SKIP_BEFORE_QUERY_FILTER] = true;
additionalIBs = (new global.CSMRelationshipUtils().getMyEntities(null, RC.RELATIONSHIP_INSTALL_BASE_RELATED_PARTY, RC.FIELD_INSTALL_BASE_ITEM, (accessType || RC.ACCESS.READ), params) || []);
GlideAppCache.put(key, additionalIBs);
return additionalIBs;
},
/*
* returns the array of accessible additional Install base items based on my account as related party.
*
*/
getMyIBsFromAdditionalAccount: function(accessType) {
var RC = global.CSMRelationshipConstants;
var key = "additional_account_install_bases_" + gs.getUserID() + (gs.nil(accessType) ? "" : accessType);
var additionalAccountIBs = GlideAppCache.get(key);
if (!gs.nil(additionalAccountIBs))
return additionalAccountIBs;
if (!gs.hasRole(RC.ROLE_CONTACT)) return [];
var params = {};
params[RC.SKIP_BEFORE_QUERY_FILTER] = true;
params[RC.NO_PERSONA_QUERY] = true;
params[RC.IS_ACCOUNT_QUERY] = true;
additionalAccountIBs = (new global.CSMRelationshipUtils().getMyEntities(null, RC.RELATIONSHIP_INSTALL_BASE_RELATED_PARTY, RC.FIELD_INSTALL_BASE_ITEM, (accessType || RC.ACCESS.READ), params) || []);
GlideAppCache.put(key, additionalAccountIBs);
return additionalAccountIBs;
},
/*
* returns the array of authorized account sys_ids of the given installBaseItem
*/
getIBAuthorizedAccounts: function(installBaseItem){
var authorizedAccounts = [];
if(gs.nil(installBaseItem)) return authorizedAccounts;
var RC = global.CSMRelationshipConstants;
var params = {};
params[RC.ADDITIONAL_ENCODED_QUERY] = 'install_base_item='+installBaseItem + '^responsibility.type=customer_account';
params[RC.NO_PERSONA_QUERY] = true;
params[RC.SKIP_ROLE_CHECK] = true;
authorizedAccounts = new global.CSMRelationshipUtils().getMyEntities(null, RC.RELATIONSHIP_INSTALL_BASE_RELATED_PARTY, RC.FIELD_ACCOUNT, RC.ACCESS.READ, params);
return authorizedAccounts;
},
/*
* returns the string of authorized contacts seperated by comma for the given installBaseItem, when Account restriction access in enabled.
*/
getIBContactsHavingFullAccessWithAccessRestriction: function(installBaseItem) {
if (gs.nil(installBaseItem)) return '';
var contacts = "";
var gr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
if (gr.get(installBaseItem)) {
if (!gs.nil(gr.contact))
contacts += gr.contact;
var RC = global.CSMRelationshipConstants;
var authorizedContacts = [];
var index = 0;
var params = {};
params[RC.ADDITIONAL_ENCODED_QUERY] = 'install_base_item='+installBaseItem + '^type.entity_type=customer_contact';
params[RC.NO_PERSONA_QUERY] = true;
params[RC.SKIP_ROLE_CHECK] = true;
authorizedContacts = new global.CSMRelationshipUtils().getMyEntities(null, RC.RELATIONSHIP_INSTALL_BASE_RELATED_PARTY, RC.FIELD_CONTACT, RC.ACCESS.READ, params);
while (authorizedContacts.length > index) {
contacts += (contacts.length > 0 ? (','+authorizedContacts[index]) : authorizedContacts[index]);
index++;
}
}
return contacts;
},
getAffectedIBsForCase: function(caseId) {
var affectedIBs = [];
var gr = new GlideRecord(global.CSMBaseConstants.AFFECTED_INSTALL_BASE_TABLE);
gr.addQuery('csm_case', caseId);
gr.query();
while(gr.next()) {
affectedIBs.push(gr.install_base_item + '');
}
return affectedIBs;
},
/*
* Gets Sold products that are related to install base items for a given persona (additionalAccount, additionlaServiceOrganization, additionalParty).
* @return array of sold product ids
*/
getMyAdditionalSoldProducts: function(persona, accessType) {
var soldProducts = [];
var myInstallBaseItems = this.getAllIBsFromMyAdditionalIBHierarchy(persona, accessType);
if(myInstallBaseItems.length <= 0){
return soldProducts;
}
this.getSoldProductsForGivenIBs(myInstallBaseItems, soldProducts);
return soldProducts;
},
/*
* Gets Sold products that are related to install base items whose primary contact is logged-in user.
* @return array of sold product ids
*/
getMySoldProductsOfIBPrimaryContact: function() {
var soldProducts = [];
var myInstallBaseItems = this.getMyIBsFromPrimaryContacts();
if(myInstallBaseItems.length <= 0){
return soldProducts;
}
this.getSoldProductsForGivenIBs(myInstallBaseItems, soldProducts);
return soldProducts;
},
/*
* Gets Sold products that are related to given install base list.
* @return list of sold product ids.
*/
getSoldProductsForGivenIBs: function(installBaseList, soldProductList) {
var installedProductGr= new GlideRecord(global.CSMBaseConstants.M2M_INSTALLED_PRODUCT);
installedProductGr.addQuery('install_base_item', 'IN', installBaseList.join(','));
installedProductGr._skip_before_query_filter = true;
installedProductGr.query();
while (installedProductGr.next()) {
soldProductList.push(installedProductGr.getValue('sold_product'));
}
return soldProductList;
},
/*
*
* Gets my IBs for the head of household
* @return array of IBs ids
*/
getMyIBsForHeadOfHousehold: function() {
var IBs = [];
var consumer;
var consumerGr = new GlideRecord(global.CSMBaseConstants.CONSUMER_TABLE);
consumerGr.addQuery('user', gs.getUserID());
consumerGr.query();
while (consumerGr.next()) {
consumer = consumerGr.getUniqueValue();
}
var gr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
gr.addEncodedQuery("householdISNOTEMPTY");
gr._skip_before_query_filter = true;
gr.query();
while (gr.next()) {
var hgr = new GlideRecord(global.CSMBaseConstants.HOUSEHOLD);
hgr.addQuery('sys_id', gr.household);
if (!gs.nil(consumer)) {
hgr.addQuery('head', consumer);
}
hgr.query();
while (hgr.next()) {
IBs.push(gr.getUniqueValue());
}
}
return IBs;
},
/*
*
* Gets my Cases that are related to given persona (Primary Contact of IB, Primary Consumer of IB, Related Party of IB, etc...).
* @return array of case ids
*/
getCasesForGivenPersona: function(pointerEncodedQuery) {
var cases = [];
var affectedInstallBaseGr= new GlideRecord(global.CSMBaseConstants.AFFECTED_INSTALL_BASE_TABLE);
affectedInstallBaseGr.addEncodedQuery(pointerEncodedQuery);
affectedInstallBaseGr._skip_before_query_filter = true;
affectedInstallBaseGr.query();
while (affectedInstallBaseGr.next()) {
cases.push(affectedInstallBaseGr.getValue('csm_case'));
}
return cases;
},
/*
* Gets the IBs of my account relationships
* @return array of IBs
*/
getMyIBsFromAccountRelationships: function() {
var installBases = [];
//add IBs to array, where IB account is in relationship with my account.
var ibGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
ibGr.addQuery('account', 'DYNAMIC', '6ca0933a0fe210103ff81b41ff767ed9');
ibGr._skip_before_query_filter = true;
ibGr.query();
while (ibGr.next()) {
installBases.push(ibGr.getUniqueValue());
}
return installBases;
},
getMyIBsFromPrimaryContacts: function() {
var installBases = [];
//add IBs to array, where IB contact is me
var ibGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
ibGr.addQuery('contact', gs.getUserID());
ibGr._skip_before_query_filter = true;
ibGr.query();
while (ibGr.next()) {
installBases.push(ibGr.getUniqueValue());
}
return installBases;
},
getMyIBsFromPrimaryConsumers: function() {
var installBases = [];
//add IBs to array, where IB consumer is me
var ibGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
ibGr.addQuery('consumer.user', gs.getUserID());
ibGr._skip_before_query_filter = true;
ibGr.query();
while (ibGr.next()) {
installBases.push(ibGr.getUniqueValue());
}
return installBases;
},
getMyIBsFromSPRelatedParty: function() {
var ibUtil = new sn_install_base.InstallBaseUtil();
var installBaseItems = [];
var gr = new GlideRecord(global.CSMBaseConstants.M2M_INSTALLED_PRODUCT);
gr._skip_before_query_filter = true;
var spRelPartyQueryCondition = gr.addQuery('sold_product', 'IN', ibUtil.getMySPsFromAdditionalContacts());
spRelPartyQueryCondition.addOrCondition('sold_product', 'IN', ibUtil.getMySPsFromAdditionalAccount());
spRelPartyQueryCondition.addOrCondition('sold_product', 'IN', ibUtil.getConsumerSoldProducts());
gr.addNotNullQuery('install_base_item');
gr.query();
while(gr.next()) {
installBaseItems.push(gr.install_base_item + '');
}
return installBaseItems;
},
/*
*
* Gets my IBs that are related to given persona(additional_account, additional_service_organization, additional_party).
* @return array of IBs
*/
getMyPersonaBasedRelatedIBs: function(persona, accessType) {
var RC = global.CSMRelationshipConstants;
var myInstallBaseItems = [];
switch(persona) {
case RC.RELATED_PARTY_AS_ADDITIONAL_ACCOUNT :
myInstallBaseItems = this.getMyIBsFromAdditionalAccount(accessType);
break;
case RC.RELATED_PARTY_AS_ADDITIONAL_SERVICE_ORGANIZATION :
myInstallBaseItems = new sn_cs_queryrules.SOQueryRuleUtils().getMyIBsFromAdditionalSO(accessType);
break;
default :
myInstallBaseItems = this.getMyAdditionalInstallBaseItems(accessType);
break;
}
return myInstallBaseItems;
},
/*
*
* Gets all IBs of my root IBs hierarchy for a given persona(additional_account, additional_service_organization, additional_party).
* @return array of IBs
*/
getAllIBsFromMyAdditionalIBHierarchy: function(persona, accessType) {
var installBases = [];
var myInstallBaseItems = this.getMyPersonaBasedRelatedIBs(persona, accessType);
if(myInstallBaseItems.length <= 0){
return installBases;
}
installBases = installBases.concat(myInstallBaseItems);
var ibGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
ibGr.addQuery('root','IN', myInstallBaseItems.join(','));
ibGr._skip_before_query_filter = true;
ibGr.query();
while (ibGr.next()) {
installBases.push(ibGr.getUniqueValue());
}
return installBases;
},
/*
* fetch the contracts for the given install base item
*
* @return contract array.
*/
getContracts: function(ibItemSysId) {
var contracts = [];
var installBaseGr = new GlideRecord(global.CSMBaseConstants.M2M_CONTRACT_SOLD_PRODUCT);
installBaseGr.addQuery('install_base', ibItemSysId);
installBaseGr.addEncodedQuery('removedISEMPTY^ORremoved>=javascript:gs.beginningOfToday()');
installBaseGr.query();
while (installBaseGr.next())
contracts.push(installBaseGr.contract.number + '');
return contracts;
},
/*
* fetch the contracts for the given install base item in the entitlements.
*
* @return contract array.
*/
getEntitlements: function(ibItemSysId) {
var contracts = [];
var serviceEntitlementGr = new GlideRecord(global.CSMBaseConstants.ENTITLEMENT_TABLE);
serviceEntitlementGr.addQuery('install_base_item', ibItemSysId);
serviceEntitlementGr.addEncodedQuery('end_date>=javascript:gs.beginningOfToday()^ORend_dateISEMPTY');
serviceEntitlementGr.query();
while (serviceEntitlementGr.next()) {
if (serviceEntitlementGr.getValue('contract'))
contracts.push(serviceEntitlementGr.contract.number + '');
}
return contracts;
},
/* Get Maintenance Plans for the given install base item
*
* @param installBaseSysId - install base sys_id
* @param tableName - table name of the IB or its extended table
*
* @return array of Maintenance Plans.
*/
getMaintenancePlansForIB: function(installBaseSysId, tableName) {
return this._getMaintenancePlans([installBaseSysId], tableName);
},
/* Get Maintenance Plans for the given install base item and child IBs
*
* @param installBaseSysId - install base sys_id
* @param tableName - table name of the IB or its extended table
*
* @return array of Maintenance Plans.
*/
getMaintPlansForIBAndDescendants: function(installBaseSysId, tableName) {
var childIBs = [];
childIBs = this.getAllInstallBaseItemDescendants(installBaseSysId, false);
childIBs.push(installBaseSysId);
return this._getMaintenancePlans(childIBs);
},
/* Get Maintenance Plans for the given install base items
*
* @param installBaseSysIds - array of install base sys_id's
* @param tableName - table name of the IB or its extended table
*
* @return array of Maintenance Plans.
*/
_getMaintenancePlans: function(installBaseSysIds) {
var plans = [];
if (gs.nil(installBaseSysIds) || installBaseSysIds.length == 0) {
return plans;
}
var workPlanRecordgr = new GlideRecord(global.CSMBaseConstants.M2M_WORK_PLAN_TO_RECORD);
workPlanRecordgr.addQuery('install_base', 'IN', installBaseSysIds);
workPlanRecordgr.query();
while (workPlanRecordgr.next()) {
var planSysId = workPlanRecordgr.schedule.plan+'';
if (plans.indexOf(planSysId) == -1)
plans.push(planSysId);
}
return plans;
},
/**
* filters and returns [sys_ids] of IBs from both IB and SP Related parties(mapped as installed product) based on given params
*
* @param {*} related_party_id sys_id of either account/service_org/contact/consumer/user
* @param {*} params.responsibility sys_id of single responsibility on Related Party
* @param {*} params.access type of access as mentioned on CSMRelationshipConstantsSNC
*
* Note: either one of params.responsibility/params.access is accepted in a given request
*
* @returns array of IB sys_ids
*/
getAllEntities: function(related_party_id, params) {
var ibEntities = this.getEntities(global.CSMBaseConstants.INSTALL_BASE_RELATED_PARTY,
InstallBaseItemAPIConstants.INSTALL_BASE_FIELD, related_party_id, params);
var spEntities = this.getEntities(global.CSMBaseConstants.SOLD_PRODUCT_RELATED_PARTY,
InstallBaseItemAPIConstants.SOLD_PRODUCT_FIELD, related_party_id, params);
var installProductGR = new GlideRecord(global.CSMBaseConstants.M2M_INSTALLED_PRODUCT);
installProductGR.addQuery(InstallBaseItemAPIConstants.SOLD_PRODUCT_FIELD, 'IN', spEntities);
installProductGR.query();
if(gs.nil(ibEntities)) ibEntities = new Array();
while(installProductGR.next())ibEntities.push(installProductGR.getValue(InstallBaseItemAPIConstants.INSTALL_BASE_FIELD));
return ibEntities;
},
/**
* filters and returns [sys_ids] of IB/SP based on given params
*
* @param {*} relationship table name of relatedParty {IBRP or SPRP}
* @param {*} field IB/SP field name on the relationship table
* @param {*} related_party_id sys_id of either account/service_org/contact/consumer/user
* @param {*} params.responsibility sys_id of single responsibility on Related Party
* @param {*} params.access type of access as mentioned on CSMRelationshipConstantsSNC
*
* Note: either one of params.responsibility/params.access is accepted in a given request
*
* @returns array of @param field values on the @param relationship table.
*/
getEntities: function(relationship, field, related_party_id, params) {
var responsibilities,
result;
if(!gs.nil(params.responsibility)) {
responsibilities = [params.responsibility];
} else if(!gs.nil(params.access)){
responsibilities = global.CSMResponsibilityModelUtil.getResponsibilities(relationship, params.access, null, true) || [];
}
result = this._queryCompanyQuery(relationship, field, related_party_id, responsibilities);
if(gs.nil(result) || result.length == 0) {
result = this._queryPersona(relationship, field, related_party_id, responsibilities);
}
return result;
},
/**
* query based on given user/consumer @param related_party_id in @param relationship table
*
* @param {*} relationship table name of relatedParty {IBRP or SPRP}
* @param {*} field IB/SP field name on the relationship table
* @param {*} related_party_id sys_id of either consumer/user
* @param {*} responsibilities sys_ids of responsibility on Related Party
*
*
* @returns array of @param field values on the @param relationship table.
*/
_queryPersona: function(relationship, field, related_party_id, responsibilities) {
var userGR = new GlideRecord(global.CSMBaseConstants.USER_TABLE),
consumerGR = new GlideRecord(global.CSMBaseConstants.CONSUMER_TABLE),
userId,
consumerId;
var result = new Array();
if(userGR.get(related_party_id)) {
userId = related_party_id;
if(consumerGR.get('user', userId)){
consumerId = consumerGR.getValue('sys_id');
}
} else if(consumerGR.get(related_party_id)) {
consumerId = related_party_id;
if(!gs.nil(consumerGR.user)) {
userId = consumerGR.getValue('user');
}
} else {
return result;
}
var relationshipGR = new GlideRecord(relationship);
var personaQuery;
if(!gs.nil(userId)) {
if (relationshipGR.isValidField('user')) {
personaQuery = relationshipGR.addQuery(global.CSMRelationshipConstants.DEFAULT_PERSONA_FIELD, userId);
}
if(this._hasUserRole(userId, global.CSMRelationshipConstants.ROLE_CONTACT)) {
if(!gs.nil(personaQuery)) personaQuery.addOrCondition(global.CSMRelationshipConstants.FIELD_CONTACT, userId);
else personaQuery = relationshipGR.addQuery(global.CSMRelationshipConstants.FIELD_CONTACT, userId);
var account = this._getUserAccount(userId);
if(!gs.nil(account)){
var accountResponsibilities = global.CSMResponsibilityModelUtil.getResponsibilities(relationship, global.CSMRelationshipConstants.ACCESS.READ, null, true)||[];
var accountRecords = this._queryAccount(relationship, field, account, accountResponsibilities);
this._concatArray(result, accountRecords);
}
}
if(this.isServiceOrganizationPluginActive() && relationshipGR.isValidField('service_organization')){
var SOs = this._getSOHelper().getUserServiceOrgIds(userId).join(',');
if(!gs.nil(SOs)) {
var SOResponsibilities = global.CSMResponsibilityModelUtil.getResponsibilities(relationship, global.CSMRelationshipConstants.ACCESS.READ, null, true)||[];
var SORecords = this._queryServiceOrg(relationship, field, SOs, SOResponsibilities);
this._concatArray(result, SORecords);
}
}
}
if (!gs.nil(consumerId)) {
if(!gs.nil(personaQuery)) personaQuery.addOrCondition(global.CSMRelationshipConstants.FIELD_CONSUMER, consumerId);
else personaQuery = relationshipGR.addQuery(global.CSMRelationshipConstants.FIELD_CONSUMER, consumerId);
}
if(!gs.nil(personaQuery) && !gs.nil(responsibilities) && responsibilities.length>0) {
relationshipGR.addQuery(global.CSMRelationshipConstants.RESPONSIBILITY, 'IN', responsibilities);
}
relationshipGR.query();
while(relationshipGR.next())
result.push(relationshipGR.getValue(field));
return result;
},
/**
* query based on given account/SO @param related_party_id in @param relationship table
*
* @param {*} relationship table name of relatedParty {IBRP or SPRP}
* @param {*} field IB/SP field name on the relationship table
* @param {*} related_party_id sys_id of either account/SO
* @param {*} responsibilities sys_ids of responsibility on Related Party
*
*
* @returns array of @param field values on the @param relationship table.
*/
_queryCompanyQuery: function(relationship, field, related_party_id, responsibilities) {
var comapnyGR = new GlideRecord(global.CSMBaseConstants.CORE_COMPANY),
result = [];
if(comapnyGR.get(related_party_id)) {
switch(String(comapnyGR.sys_class_name)){
case global.CSMBaseConstants.ACCOUNT_TABLE:
result = this._queryAccount(relationship, field, related_party_id, responsibilities);
break;
case global.CSMBaseConstants.SERVICE_ORGANIZATION_TABLE:
case global.CSMBaseConstants.EBL_TABLE:
case global.CSMBaseConstants.IBL_TABLE:
result = this._queryServiceOrg(relationship, field, [related_party_id], responsibilities);
break;
}
}
return result;
},
/**
* query based on given @param serviceorg in @param relationship table
*
* @param {*} relationship table name of relatedParty {IBRP or SPRP}
* @param {*} field IB/SP field name on the relationship table
* @param {*} service_org array of sys_ids of service_org
* @param {*} responsibilities sys_ids of responsibility on Related Party
* @param {*} result array to which sys_ids are added
*
* @returns returns array of @param field values
*/
_queryServiceOrg: function(relationship, field, service_org, responsibilities) {
var result = [];
var relatedPartyGR = new GlideRecord(relationship);
if (this.isServiceOrganizationPluginActive() && relatedPartyGR.isValidField('service_organization')) {
relatedPartyGR.addNullQuery(global.CSMRelationshipConstants.FIELD_CONTACT);
relatedPartyGR.addNullQuery(global.CSMRelationshipConstants.FIELD_CONSUMER);
relatedPartyGR.addNullQuery(global.CSMRelationshipConstants.DEFAULT_PERSONA_FIELD);
relatedPartyGR.addNotNullQuery(global.CSMRelationshipConstants.FIELD_SERVICE_ORGANIZATION);
relatedPartyGR.addQuery(global.CSMRelationshipConstants.FIELD_SERVICE_ORGANIZATION, 'IN', service_org);
if(!gs.nil(responsibilities))
relatedPartyGR.addQuery(global.CSMRelationshipConstants.RESPONSIBILITY, 'IN', responsibilities);
relatedPartyGR.query();
while(relatedPartyGR.next()) result.push(relatedPartyGR.getValue(field));
}
return result;
},
/**
* query based on given @param account in @param relationship table
*
* @param {*} relationship table name of relatedParty {IBRP or SPRP}
* @param {*} field IB/SP field name on the relationship table
* @param {*} account sys_id of account
* @param {*} responsibilities sys_ids of responsibility on Related Party
* @param {*} result array to which sys_ids are added
*
* @returns returns array of @param field values
*/
_queryAccount: function(relationship, field, account, responsibilities) {
var result = [];
var relatedPartyGR = new GlideRecord(relationship);
relatedPartyGR.addNullQuery(global.CSMRelationshipConstants.FIELD_CONTACT);
relatedPartyGR.addNullQuery(global.CSMRelationshipConstants.FIELD_CONSUMER);
relatedPartyGR.addNullQuery(global.CSMRelationshipConstants.DEFAULT_PERSONA_FIELD);
relatedPartyGR.addNotNullQuery(global.CSMRelationshipConstants.FIELD_ACCOUNT);
relatedPartyGR.addQuery(global.CSMRelationshipConstants.FIELD_ACCOUNT, account);
if(!gs.nil(responsibilities))
relatedPartyGR.addQuery(global.CSMRelationshipConstants.RESPONSIBILITY, 'IN', responsibilities);
relatedPartyGR.query();
while(relatedPartyGR.next()) result.push(relatedPartyGR.getValue(field));
return result;
},
_getUserAccount: function(userId) {
var contactGR = new GlideRecord(global.CSMBaseConstants.CONTACT_TABLE);
if(contactGR.get(userId)) {
return contactGR.account;
}
return null;
},
_getSOHelper: function() {
return new global.ServiceOrganizationUtil();
},
_hasUserRole: function(userId, role) {
var roleGR = new GlideRecord(global.CSMBaseConstants.USER_ROLE_TABLE);
roleGR.get('name', role);
var roleSysId = roleGR.sys_id;
var userRoleGR = new GlideRecord(global.CSMBaseConstants.USER_HAS_ROLE_TABLE);
userRoleGR.addQuery('user', userId);
userRoleGR.addQuery('role', roleSysId);
userRoleGR.query();
var ans= userRoleGR.next();
return ans;
},
_concatArray: function(result, sub) {
for(var index =0; index<sub.length; index++) {
result.push(sub[index]);
}
},
/* Get Maintenance Schedules for the given install base item and child IBs
*
* @param installBaseSysId - install base sys_id
* @param tableName - table name of the IB or its extended table
*
* @return array of Maintenance Schedules.
*/
getMaintSchedulesForIBAndDescendants: function(installBaseSysId) {
var childIBs = this.getAllInstallBaseItemDescendants(installBaseSysId, false);
childIBs.push(installBaseSysId);
var schedules = [];
var workPlanRecordgr = new GlideRecord(global.CSMBaseConstants.M2M_WORK_PLAN_TO_RECORD);
workPlanRecordgr.addQuery('install_base', 'IN', childIBs);
workPlanRecordgr.query();
while (workPlanRecordgr.next()) {
var scheduleSysId = workPlanRecordgr.getValue('schedule')+'';
if (schedules.indexOf(scheduleSysId) == -1)
schedules.push(scheduleSysId);
}
return schedules;
},
validateInstallBaseCaseRecord: function(inputVars) {
var entity = [];
var ibGr = new GlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM);
ibGr.get(inputVars['install_base']);
if (ibGr.root)
inputVars['install_base'] = ibGr.root;
if (!gs.nil(inputVars['account'])) {
if (inputVars['account'] != ibGr.account) {
entity.push("account");
}
} else if (!gs.nil(inputVars['consumer'])) {
if (inputVars['consumer'] != ibGr.consumer) {
showError = true;
if (this.isHouseHoldPluginActive() && !gs.nil(ibGr.household)) {
if (inputVars['consumer'] == ibGr.household.head) {
showError = false;
}
}
if (showError) {
var params = {
'responsibility': null,
'access': 'FULL'
};
var ibSysIdsForAuthorisedConsumer = this.getAllEntities(inputVars['consumer'], params);
if(ibSysIdsForAuthorisedConsumer.indexOf(inputVars['install_base'].toString()) > -1)
showError=false;
else if(ibSysIdsForAuthorisedConsumer.indexOf(ibGr.getUniqueValue()) > -1)
showError=false;
}
if (showError) {
entity.push("consumer");
}
}
}
if (gs.nil(inputVars['consumer']) && this.isHouseHoldPluginActive() && !gs.nil(inputVars['household'])) {
if (!gs.nil(ibGr.household) && inputVars['household'] != ibGr.household) {
entity.push("household");
}
}
if (gs.nil(inputVars['account']) && gs.nil(inputVars['consumer']) && gs.nil(inputVars['household'])) {
if (this.isServiceOrganizationPluginActive() && !gs.nil(inputVars['requesting_service_organization'])) {
if (inputVars['requesting_service_organization'] != ibGr.service_organization) {
entity.push("requesting service organisation");
}
}
}
return entity;
},
/**
* For the given table name, validate whether it is install base item or one of the table extensions of install base item.
*
* @param tableName - name of the table.
*/
isValidInstallBaseTable: function (tableName) {
if (!global.CSMBaseConstants.INSTALL_BASE_ITEM.equals(tableName))
return true;
return new global.CSMRelationshipUtils().isChildOf(global.CSMBaseConstants.INSTALL_BASE_ITEM, tableName);
},
/**
* Returns true if the New UI actions should be shown on the sn_install_base_m2m_contract_sold_product related lists
* for ast_contract and sn_pss_core_service_contract
*/
showRLNewActionForContract: function (parent) {
if (parent.sys_class_name == "ast_contract") {
return true;
}
if (parent.sys_class_name == "sn_pss_core_service_contract") {
return true;
}
return false;
},
/*
* Method to return supported IB classes with which model category can be created
* @return List of table extensions of Install base item table (excluding base IB table)
*/
getIBClassesForModelCategory: function() {
var tableHierarchy= new GlideTableHierarchy(global.CSMBaseConstants.INSTALL_BASE_ITEM);
var tableArray = tableHierarchy.getTableExtensions();
return tableArray;
},
type: 'InstallBaseUtilSNC'
};
InstallBaseUtil.CACHE_KEY_MY_IB = "my_install_base_items";
InstallBaseUtil.CACHE_KEY_MY_IB_SP_CONTACT_IS_ME = "my_install_base_items_sp_contact_is_me";
InstallBaseUtil.CACHE_KEY_MY_IP_FROM_SP = "my_installed_products_from_sp";
Sys ID
ef0e12dd77e63010d3ef07dc7d5a99f2