Name

sn_customerservice.CSPortalUtil

Description

Helper functions for Customer Service portal

Script

var CSPortalUtil = Class.create();
CSPortalUtil.prototype = {
  initialize : function() {},
  
  isDuplicateUserID : function(user_name) {
  	var user = new GlideRecord("sys_user");
  	user.addQuery("user_name",user_name);
  	user.query();
  	if(user.next())
  		return true;
  	return false;
  },
  
  createPassword : function() {
  	return GlideSecureRandomUtil.getSecureRandomString(8);
  },
  
  checkCompatibility : function(browser) {
      var message = '';
      if(typeof browser !== "undefined" && browser.isMSIE && browser.versionString <= 8)
  		message = 'unsupported_browser';
      else {
          var device = new global.CSManagementUtils().scopedGlideMobileExtensions();
          if (device == "m" || device == "mobile" || device == "tablet")
              message = 'unsupported_device';
      }
      return message;     
  },
  
  showCreateLoginUIAction : function(current) {
  	var uri = gs.action.getGlideURI();
  	var dup = this.isDuplicateUserID(current.first_name + "." + current.last_name);	
  	if(current.isValidRecord() && current.canWrite() && !current.user_name && gs.hasRole("sn_customerservice.customer_admin")&& !current.isNewRecord() && uri.get('sysparm_view') == "ess" && !dup)
  		return true;
  	else
  		return false;
  },
  
  /**
  	Get Accounts to determine the cases to which I have read/write access in the portal.
  		account: account sys_id
  		readWriteAccess:
  			true : create access
  			false : read access
  		adminAccess:
  			true: return all account where admin/read/write flag set to true on relationship
  		checkAcctCustomer: check account is a customer account for creating cases
  */
  getAccessibleAccounts : function(account, readWriteAccess, checkAcctCustomer, adminAccess) {
  	var relationshipTypes = new GlideRecord('sn_customerservice_account_relationship_type');
  	var accessibleAccounts = [];
  	var accountGr = new GlideRecord("customer_account");
  	account = account || gs.getUser().getCompanyID();
  	checkAcctCustomer = checkAcctCustomer || false;
  	adminAccess = adminAccess || false;
  	if (accountGr.get(account)) {
  		if (checkAcctCustomer && accountGr.customer)
  			accessibleAccounts.push(account);
  		else
  			accessibleAccounts.push(account);	
  		relationshipTypes.addQuery('active', true);
  		relationshipTypes.query();
  		while(relationshipTypes.next()) {
  			var currentAccessibleAccounts = [];
  			var accountRelationships = new GlideRecord('account_relationship');
  			accountRelationships.addQuery('relationship_type', relationshipTypes.sys_id);
  			accountRelationships.addQuery('reverse_relationship', false);
  			accountRelationships.addQuery('from_company', account);
  			accountRelationships.query();
  			while(accountRelationships.next())
  				currentAccessibleAccounts.push(accountRelationships.getValue("to_company"));
  			var relationshipAccess = new GlideRecord('sn_customerservice_account_relationship_access');
  			relationshipAccess.addQuery('relationship_type', relationshipTypes.sys_id);
  			relationshipAccess.query();
  			while(relationshipAccess.next() && !gs.nil(readWriteAccess)) {
  				if(gs.hasRole(relationshipAccess.role.name) && (readWriteAccess && relationshipAccess.write) || (!readWriteAccess && relationshipAccess.read) || (adminAccess && ( relationshipAccess.read || relationshipAccess.write))) {
  					accessibleAccounts = accessibleAccounts.concat(currentAccessibleAccounts);
  					//Extra care so that if relationshipAccess has multiple records with same relationship_type and for the same role
  					break;
  				}
  			}
  		}
  		accessibleAccounts = accessibleAccounts.concat(this.getAccountsFromContactRelationship());
  		if(gs.hasRole('sn_customerservice.customer_admin'))
  			accessibleAccounts = accessibleAccounts.concat(this.getAccountsFromHierarchy(account));
  	}
  	return accessibleAccounts;
  },
  
  getAccountsFromHierarchy : function(account, includeWholeTree) {
  	var hierarcicalAccounts = [];
  	var gr = new GlideRecord('customer_account');
  	if(gr.get(account)) {
  		var accountPath = gr.getValue('account_path');
  		if(accountPath) {
  			var gra = new GlideRecord('customer_account');
  			if(includeWholeTree){
  				var topParent = accountPath.split('/')[0];
  				gra.addQuery('account_path', topParent).addOrCondition('account_path', 'STARTSWITH', topParent + '/');
  			}
  			else
  				gra.addQuery('account_path', 'STARTSWITH', accountPath + "/");
  			gra.query();
  			while(gra.next())
  				hierarcicalAccounts.push(gra.getValue('sys_id'));
  		}
  	}
  	return hierarcicalAccounts;
  },
  
  getAccountsFromContactRelationship : function(contact) {
  	var contactAccounts = [];
  	var accountContacts;
  	contact = contact || gs.getUserID();
  	accountContacts =  new GlideRecord('sn_customerservice_contact_relationship');
  	accountContacts.addQuery('contact', contact);
  	accountContacts.query();
  	while(accountContacts.next())
  		contactAccounts.push(accountContacts.getValue('company'));
  	return contactAccounts;
  },
  
  getAccountsFromAccountRelationship : function(account) {
  	var relationshipTypes = new GlideRecord('sn_customerservice_account_relationship_type');
  	var accessibleAccounts = [];
  	var accountGr = new GlideRecord("customer_account");
  	account = account || gs.getUser().getCompanyID();
  	if (accountGr.get(account)) {
  		relationshipTypes.addQuery('active', true);
  		relationshipTypes.query();
  		while(relationshipTypes.next()) {
  			var currentAccessibleAccounts = [];
  			var accountRelationships = new GlideRecord('account_relationship');
  			accountRelationships.addQuery('relationship_type', relationshipTypes.sys_id);
  			accountRelationships.addQuery('reverse_relationship', false);
  			accountRelationships.addQuery('from_company', account);
  			accountRelationships.query();
  			while(accountRelationships.next())
  				currentAccessibleAccounts.push(accountRelationships.getValue("to_company"));
  			var relationshipAccess = new GlideRecord('sn_customerservice_account_relationship_access');
  			relationshipAccess.addQuery('relationship_type', relationshipTypes.sys_id);
  			relationshipAccess.query();
  			while(relationshipAccess.next()) {
  				if(gs.hasRole(relationshipAccess.role.name) && relationshipAccess.read) {
  					accessibleAccounts = accessibleAccounts.concat(currentAccessibleAccounts);
  					//Extra care so that if relationshipAccess has multiple records with same relationship_type and for the same role
  					break;
  				}
  			}
  		}
  	}
  	return accessibleAccounts;
  },
  
  getContactsFromAccountRelationship : function(account) {
  	var contactAccounts = [];
  	var accounts = [];
  	if(typeof account == 'string')
  		accounts.push(account);
  	else
  		accounts = account;
  	var accountContacts = new GlideRecord('sn_customerservice_contact_relationship');
  	accountContacts.addQuery('company', 'IN', accounts.join(','));
  	accountContacts.query();
  	while(accountContacts.next())
  		contactAccounts.push(accountContacts.getValue('contact'));
  	return contactAccounts;
  },
  
  getRelationshipRolesWithAdminAccess : function() {
  	var roles = [];
  	var relationshipAccess = new GlideRecord('sn_customerservice_account_relationship_access');
  	relationshipAccess.addQuery('admin', true);
  	relationshipAccess.query();
  	while(relationshipAccess.next())
  		roles.push(relationshipAccess.role.name.toString());
  	return roles;
  },
  
  getRelationshipRolesWithReadAccess : function() {
  	var relationshipRoles = {};
  	var relationshipAccess = new GlideRecord('sn_customerservice_account_relationship_access');
  	relationshipAccess.addQuery('read', true);
  	relationshipAccess.query();
  	while(relationshipAccess.next()) {
  		var relationType = relationshipAccess.getValue('relationship_type');
  		if(!relationshipRoles[relationType])
  			relationshipRoles[relationType] = [];
  		relationshipRoles[relationType].push(relationshipAccess.role.name.toString());
  	}
  	return relationshipRoles;
  },
  
  removeCase : function(caseId) {
  	var caseRecGR = new GlideRecordSecure('sn_customerservice_case');
  	if(caseRecGR.get(caseId)){
  		caseRecGR.deleteRecord();
  	}
  	
  },
  
  type: 'CSPortalUtil'
};

Sys ID

93a7c600c3020200b0449f2974d3aec0

Offical Documentation

Official Docs: