Advertisement
zaq_hack

Unique Stack Name - Preprovision

May 9th, 2022
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var EnsureUniqueName = Class.create();
  2. EnsureUniqueName.prototype = Object.extendsObject(sn_cmp.PolicyExecutionBase, {
  3.     customScript : function(formData){
  4.         // Manipulation of form parameters are supported. Changes in any other attributes
  5.         // will be ignored.
  6.         // The data available for manipulation is as follows:
  7.         // Form Data -  for example: StackName can be accessed through formData.StackName
  8.         // formData.StackName = "MyStack";
  9.         // User Data  - for example: User Id can be accessed through this.parameters.userData
  10.         // if(this.parameter.userData.userId == 'servicenowuserId')
  11.         //this.info("id = " + formData.StackName);
  12.        
  13.         // Set debugging information to be sent to gs.info with searchable prefix
  14.         var thisDebug = true;
  15.         var thisDebugPrefix = "ZZZZZ - ";
  16.        
  17.         // Deconstruct the incoming name string back to to nPrefix and nConvention3
  18.         var nPrefix = formData.StackName.substring(0, 1);
  19.         var nConvention3 = formData.StackName.substring(2, 9);
  20.        
  21.         // Log the current name if thisDebug = true
  22.         if (thisDebug) { gs.info("" + thisDebugPrefix + formData.StackName); }
  23.  
  24.         // Check the name against existing stack names
  25.         var nameCount = new GlideRecord('sn_cmp_stack');
  26.         nameCount.addQuery('name',formData.StackName);
  27.         nameCount.query();
  28.         if (thisDebug) { gs.info("" + thisDebugPrefix + "existing stacks with this name: " + nameCount.getRowCount()); }
  29.  
  30.         while (nameCount.getRowCount() > 0) {
  31.             // If execution ends up here, increment the timestamp by 1 until we find something unique
  32.             t = "00000000" + (++nConvention3);
  33.             nConvention3 = t.substr(t.length - 8);
  34.             formData.StackName = "" + nPrefix + nConvention3;
  35.             if (thisDebug) { gs.info("" + thisDebugPrefix + formData.StackName); }
  36.  
  37.             nameCount.addQuery('name',formData.StackName);
  38.             nameCount.query();
  39.             if (thisDebug) { gs.info("" + thisDebugPrefix + "existing stacks with this name: " + nameCount.getRowCount()); }
  40.         }
  41.        
  42.         return formData;
  43.     },
  44.     execute: function() {
  45.         if(this.parameters !=  null && this.parameters.formData != null){
  46.             var inputData = JSON.parse(JSON.stringify(this.parameters.formData));
  47.             var outputFormData = this.customScript(inputData);
  48.             if( outputFormData != null){
  49.                 this.parameters.formData = outputFormData;
  50.             }
  51.         }
  52.         var output = {};
  53.         output.answer = this.parameters;
  54.         return JSON.stringify(output);
  55.     },
  56.     type : 'EnsureUniqueName'
  57. });
  58.  
  59. var EnsureUniqueNameObj = new EnsureUniqueName(inputAttributes);
  60. var outputParams = EnsureUniqueNameObj.execute();
  61. gs.info( "Output of script is = " + outputParams);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement