Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var EnsureUniqueName = Class.create();
- EnsureUniqueName.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 - ";
- // Deconstruct the incoming name string back to to nPrefix and nConvention3
- var nPrefix = formData.StackName.substring(0, 1);
- var nConvention3 = formData.StackName.substring(2, 9);
- // 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()); }
- }
- 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 : 'EnsureUniqueName'
- });
- var EnsureUniqueNameObj = new EnsureUniqueName(inputAttributes);
- var outputParams = EnsureUniqueNameObj.execute();
- gs.info( "Output of script is = " + outputParams);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement