Advertisement
zaq_hack

Unique Stack Name - Prefill

May 9th, 2022
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Naming1 = Class.create();
  2. Naming1.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.         // Get the current logged-in Username and split it into an array
  18.         var nConvention1 = this.parameters.userData.userName;
  19.         var nConvention2 = nConvention1.split(/[^A-Za-z]/);
  20.         var nPrefix = "";
  21.  
  22.         // Get current time and date and put Month, Day, Hour, and Minute into a string
  23.         var currentTime = new Date();
  24.         var nConvention3 = ('0' + (currentTime.getMonth() + 1)).slice(-2) +
  25.                          ('0' + currentTime.getDate()).slice(-2) +  
  26.                          ('0' + currentTime.getHours()).slice(-2) +
  27.                          ('0' + currentTime.getMinutes()).slice(-2);
  28.         var t = ""; // Declared here for timestamp calculations later on ...
  29.  
  30.         // Make sure the prefix has two characters
  31.         if (nConvention2.length < 2) {
  32.             // If current user does not have two initials, come up with a different 2 letters
  33.             if (nConvention1 == "admin") {
  34.                 nPrefix = "sa";
  35.             } else {
  36.                 nPrefix = nConvention1.charAt(0) + nConvention1.charAt(1);             
  37.             }
  38.         } else {
  39.             nPrefix = nConvention2[0].charAt(0) + nConvention2[1].charAt(0);
  40.         }
  41.  
  42.         // Assemble the first candidate ...
  43.         formData.StackName = "" + nPrefix + nConvention3;
  44.         // Log the current name if thisDebug = true
  45.         if (thisDebug) { gs.info("" + thisDebugPrefix + formData.StackName); }
  46.  
  47.         // Check the name against existing stack names
  48.         var nameCount = new GlideRecord('sn_cmp_stack');
  49.         nameCount.addQuery('name',formData.StackName);
  50.         nameCount.query();
  51.         if (thisDebug) { gs.info("" + thisDebugPrefix + "existing stacks with this name: " + nameCount.getRowCount()); }
  52.  
  53.         while (nameCount.getRowCount() > 0) {
  54.             // If execution ends up here, increment the timestamp by 1 until we find something unique
  55.             t = "00000000" + (++nConvention3);
  56.             nConvention3 = t.substr(t.length - 8);
  57.             formData.StackName = "" + nPrefix + nConvention3;
  58.             if (thisDebug) { gs.info("" + thisDebugPrefix + formData.StackName); }
  59.  
  60.             nameCount.addQuery('name',formData.StackName);
  61.             nameCount.query();
  62.             if (thisDebug) { gs.info("" + thisDebugPrefix + "existing stacks with this name: " + nameCount.getRowCount()); }
  63.         }
  64.                
  65.         // Additional places we want to use the name can go here
  66.         if (formData.hasOwnProperty("ConfigurationName")) {
  67.             formData.ConfigurationName = formData.StackName;
  68.         }
  69.        
  70.         return formData;
  71.     },
  72.     execute: function() {
  73.         if(this.parameters !=  null && this.parameters.formData != null){
  74.             var inputData = JSON.parse(JSON.stringify(this.parameters.formData));
  75.             var outputFormData = this.customScript(inputData);
  76.             if( outputFormData != null){
  77.                 this.parameters.formData = outputFormData;
  78.             }
  79.         }
  80.         var output = {};
  81.         output.answer = this.parameters;
  82.         return JSON.stringify(output);
  83.     },
  84.     type : 'Naming1'
  85. });
  86.  
  87. var Naming1Obj = new Naming1(inputAttributes);
  88. var outputParams = Naming1Obj.execute();
  89. gs.info( "Output of script is = " + outputParams);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement