Name

x_snc_codesanity.IsCheckApplicableToRecordByRules

Description

Returns true if the given check should be executed on the given record based on the given rules, false if not.

Script

function IsCheckApplicableToRecordByRules(grCheck,grRecord,rules)
{
  var IsValidRecord = x_snc_devtools.IsValidRecord;
  var GetValue = x_snc_devtools.GetValue;
  var GetRecord = x_snc_devtools.GetRecord;
  var IsObject = x_snc_devtools.IsObject;
  var GetArrayValue = x_snc_devtools.GetArrayValue;
  var GetStringValue = x_snc_devtools.GetStringValue;
  var GetBoolValue = x_snc_devtools.GetBoolValue;

  
  if (IsValidRecord(grRecord) == false)
  {
  	return false;
  }
  
  if (IsValidRecord(grCheck) == false)
  {
  	return false;
  }
  
  if (IsObject(rules) == false)
  {
  	return true;
  }
  
  var strRecordScopeName = '';
  if (grRecord.getRecordClassName() == 'sys_app')
  {
  	strRecordScopeName = GetStringValue(grRecord.scope);
  }
  else
  {
  	strRecordScopeName = GetStringValue(grRecord.sys_scope.scope);
  }
  
  
  var bResult = true;
  
  // BLACKLISTING - prevent specific checks from running
  
  var aBlacklistRules = GetArrayValue(GetValue(rules,'blacklist'));
  
  for (var nRule = 0; nRule < aBlacklistRules.length; nRule++)
  {
  	var rule = aBlacklistRules[nRule];
  	var aChecks = GetArrayValue(GetValue(rule,'checks'));
  	
  	var bAppliesToCheck = false;
  	
  	if (aChecks.length == 0) // no checks are specified in the rule: the rule applies to all checks
  	{
  		bAppliesToCheck = true;
  	}
  	else
  	{
  		for (var nCheck = 0; nCheck < aChecks.length; nCheck++)
  		{
  			var strCheck = aChecks[nCheck];
  			if (strCheck == grCheck.getValue('name') || strCheck == GetStringValue(grCheck.sys_id))
  			{
  				bAppliesToCheck = true;
  			}
  		}
  	}
  	
  	if (bAppliesToCheck == true)
  	{
  		var bAppliesToRecord = false;

  		var bAll = GetBoolValue(GetValue(rule,'all'));
  		if (bAll == true)
  		{
  			bAppliesToRecord = true;
  		}
  		else
  		{
  			var aScopes = GetArrayValue(GetValue(rule,'scopes'));
  			for (var nScope = 0; nScope < aScopes.length; nScope++)
  			{
  				var strScope = aScopes[nScope];
  				if (strScope == strRecordScopeName || strScope == GetStringValue(grRecord.sys_scope) )
  				{
  					bAppliesToRecord = true;
  				}
  			}
  		}
  			
  		if (bAppliesToRecord == true)
  		{
  			bResult = false;
  		}
  	}
  }
  
  
  
  // WHITELISTING - only run specific checks if conditions match
  
  var aWhitelistRules = GetArrayValue(GetValue(rules,'whitelist'));
  
  for (var nRule = 0; nRule < aWhitelistRules.length; nRule++)
  {
  	var rule = aWhitelistRules[nRule];
  	var aChecks = GetArrayValue(GetValue(rule,'checks'));
  	
  	var bAppliesToCheck = false;
  	
  	for (var nCheck = 0; nCheck < aChecks.length; nCheck++)
  	{
  		var strCheck = aChecks[nCheck];
  		if (strCheck == grCheck.getValue('name') || strCheck == GetStringValue(grCheck.sys_id))
  		{
  			bAppliesToCheck = true;
  		}
  	}
  	
  	if (bAppliesToCheck == true)
  	{
  		bResult = false;
  		
  		var aScopes = GetArrayValue(GetValue(rule,'scopes'));
  		
  		for (var nScope = 0; nScope < aScopes.length; nScope++)
  		{
  			var strScope = aScopes[nScope];
  			if (strScope == strRecordScopeName || strScope == GetStringValue(grRecord.sys_scope) )
  			{
  				bResult = true;
  			}
  		}
  	}
  }
  
  return bResult;

}

Sys ID

6181dbbadbc59d10dcbdd03cd396190e

Offical Documentation

Official Docs: