Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Naming1 = Class.create();
- Naming1.prototype = Object.extendsObject(sn_cmp.PolicyExecutionBase, {
- customScript : function(formData){
- // Manipulation of form parameters are supported. Changes in any other attributes
- // will be ignored.
- // The data available for manipulation is as follows:
- // Form Data - for example: StackName can be accessed through formData.StackName
- // formData.StackName = "MyStack";
- // User Data - for example: User Id can be accessed through this.parameters.userData
- // if(this.parameter.userData.userId == 'servicenowuserId')
- //this.info("id = " + formData.StackName);
- // Set debugging information to be sent to gs.info with searchable prefix
- var thisDebug = true;
- var thisDebugPrefix = "ZZZZZ - ";
- // Get the current logged-in Username and split it into an array
- var nConvention1 = this.parameters.userData.userName;
- var nConvention2 = nConvention1.split(/[^A-Za-z]/);
- var nPrefix = "";
- // Get current time and date and put Month, Day, Hour, and Minute into a string
- var currentTime = new Date();
- var nConvention3 = ('0' + (currentTime.getMonth() + 1)).slice(-2) +
- ('0' + currentTime.getDate()).slice(-2) +
- ('0' + currentTime.getHours()).slice(-2) +
- ('0' + currentTime.getMinutes()).slice(-2);
- var t = ""; // Declared here for timestamp calculations later on ...
- // Make sure the prefix has two characters
- if (nConvention2.length < 2) {
- // If current user does not have two initials, come up with a different 2 letters
- if (nConvention1 == "admin") {
- nPrefix = "sa";
- } else {
- nPrefix = nConvention1.charAt(0) + nConvention1.charAt(1);
- }
- } else {
- nPrefix = nConvention2[0].charAt(0) + nConvention2[1].charAt(0);
- }
- // Assemble the first candidate ...
- formData.StackName = "" + nPrefix + nConvention3;
- // Log the current name if thisDebug = true
- if (thisDebug) { gs.info("" + thisDebugPrefix + formData.StackName); }
- // Check the name against existing stack names
- var nameCount = new GlideRecord('sn_cmp_stack');
- nameCount.addQuery('name',formData.StackName);
- nameCount.query();
- if (thisDebug) { gs.info("" + thisDebugPrefix + "existing stacks with this name: " + nameCount.getRowCount()); }
- while (nameCount.getRowCount() > 0) {
- // If execution ends up here, increment the timestamp by 1 until we find something unique
- t = "00000000" + (++nConvention3);
- nConvention3 = t.substr(t.length - 8);
- formData.StackName = "" + nPrefix + nConvention3;
- if (thisDebug) { gs.info("" + thisDebugPrefix + formData.StackName); }
- nameCount.addQuery('name',formData.StackName);
- nameCount.query();
- if (thisDebug) { gs.info("" + thisDebugPrefix + "existing stacks with this name: " + nameCount.getRowCount()); }
- }
- // Additional places we want to use the name can go here
- if (formData.hasOwnProperty("ConfigurationName")) {
- formData.ConfigurationName = formData.StackName;
- }
- return formData;
- },
- execute: function() {
- if(this.parameters != null && this.parameters.formData != null){
- var inputData = JSON.parse(JSON.stringify(this.parameters.formData));
- var outputFormData = this.customScript(inputData);
- if( outputFormData != null){
- this.parameters.formData = outputFormData;
- }
- }
- var output = {};
- output.answer = this.parameters;
- return JSON.stringify(output);
- },
- type : 'Naming1'
- });
- var Naming1Obj = new Naming1(inputAttributes);
- var outputParams = Naming1Obj.execute();
- gs.info( "Output of script is = " + outputParams);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement