Name

global.RestTriggerPath

Description

Handles path generation for API Builder.

Script

var RestTriggerPath = (function() {

  function processName(name) {
      return name.toLowerCase().trim().replace(/\W+/ig, '_');
  }

  function resolveGlobalScopeName() {
      if (gs.getProperty('glide.appcreator.company.code'))
          return gs.getProperty('glide.appcreator.company.code');

      return gs.hasRole('maint') ? 'now' : 'global';
  }

  function getScopeName(scopeRecord) {
      var scopeName = scopeRecord.scope;
      return scopeName == 'global' ? resolveGlobalScopeName() : scopeName;
  }

  function generatePathWithNewFlowName(currentPath, formattedFlowName) {
  	var parts = currentPath.split('/');
  	var prefix = parts.slice(0, parts.length - 1).join('/');
  	return prefix + '/' + formattedFlowName;
  }
  
  function generateBasePath(flowInfo) {
      var scopeId = flowInfo.scopeId;
      var flowName = flowInfo.flowName;
      var currentBasePath = flowInfo.basePath.value;
  	
  	gs.info('CurrentPath ' + currentBasePath);

      var formattedName = processName(flowName);
      if (gs.nil(formattedName))
          throw new Error(gs.getMessage('Valid base path could not be generated: {0} cannot be converted to valid path token', flowName));

      if (currentBasePath !== '')
          return generatePathWithNewFlowName(currentBasePath, formattedName);

      var scope = new GlideRecord('sys_scope');
      if (!scope.get(scopeId))
          throw new Error(scope.getLastErrorMessage());

      var scopeName = getScopeName(scope);
      return '/api/' + scopeName + '/' + formattedName;
  }

  return {
      generateBasePath: generateBasePath
  };	
})();

Sys ID

68f53162c7131010bd2f868cb8c26024

Offical Documentation

Official Docs: