---
sourceDocument: Yokohama IT Operations Management
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/yokohama/it-operations-management

 Release :

    - yokohama

ft:locale :

    - en-US

ft:publication_title :

    - Yokohama IT Operations Management

ft:clusterId :

    - itom

bundleId :

    - itom

workflow :

    - Technology


---

# Create a policy action script

# Create a policy action script {#ariaid-title1}

* Release version: Yokohama
* 
* Updated January 30, 2025
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 2 minutes to read

Use policy action scripts to get, update, or set values in cloud requests.{#create-policy-script__shortdesc-policy-action-scripts}

## Before you begin

Role required: sn_cmp.cloud_governor or admin.

## About this task

For more information on using scripts, see [Cloud scripts and cloud script templates](https://servicenow-prod.fluidtopics.net/IZjjZnudGUfk4RkwG6Kxcg "In the Cloud Provisioning and Governance application, script execution is divided into cloud scripts and cloud script templates. Use scripts in blueprints, resource blocks, OS profiles, and use policy scripts to set request form attributes. Policy scripts cannot override user data.").

## Procedure

1. On the Cloud Admin Portal, navigate to GovernPolicies.
2. On the Policy Action Scripts tab, click New, enter a unique and descriptive Name and Description, fill in the form, and then click Submit.  
   After you enter a name, a script appears with a variable that uses the name that you entered.{#create-policy-script__table_sqt_4gp_qz__entry__2}

   | Field | Description |
   |-|-|
   | Action Script Category | Choose another category or keep the one you created in an earlier step. |
   | Script | Modify the `customScripts` function in the script. This is the only part of the script that you need to modify. |
   [ ]

   {#create-policy-script__table_sqt_4gp_qz}

## Example

The following example script determines whether the user belongs to a user group and then overrides the user-specified application server node name value with the value MyNodeName.  


    customScript : function(formData){
      var groups = this.parameters.userData.groups; 
        if( groups.length > 0){ 
          formData.App_Server_NodeName = "MyNodeName"; 
        }
      return formData;
    },

This example returns a random number:  


    customScript : function(formData){
      var min = 10000;
      var max = 99999;
      var num = Math.floor(Math.random() * (max - min + 1)) + min;
      formData.randomNumber = num + "";  
      return formData;
    },

This example script adds three hours to the lease date using the base-system
[GlideDateTime](https://developer.servicenow.com/app.do#!/training/article/app_store_learnv2_scripting_jakarta_server_side_scripting/app_store_learnv2_scripting_jakarta_glidedatetime?v=jakarta) class.  


    var SetLeaseShort = Class.create();
    SetLeaseShort.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);
    		
    		// var leaseValue = now();
    		// leaseValue = leaseValue.setHours(leaseValue.getHours()+4);
    		// formData.LeaseEndDate = leaseValue;
    		
    		if (formData.hasOwnProperty("LeaseEndDate")) {
    			gdt = new GlideDateTime();
    			// 10800 = 3 hours
    			gdt.addSeconds(10800);
    			formData.LeaseEndDate = gdt.getDisplayValue();
    		}	
    		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 : 'SetLeaseShort'
    });	

    var SetLeaseShortObj = new SetLeaseShort(inputAttributes);
    var outputParams = SetLeaseShortObj.execute();
    gs.info( "Output of script is = " + outputParams);

This example script sets the user-specified Azure Wordpress DNS name field from the form to the same name as the stack name:  

    var WordpressFill = Class.create();
    WordpressFill.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);
    		
    		formData.Azure_Wordpress_v6_vmDnsName = 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 : 'WordpressFill'
    });	

    var WordpressFillObj = new WordpressFill(inputAttributes);
    var outputParams = WordpressFillObj.execute();
    gs.info( "Output of script is = " + outputParams);

**Related tasks**   

* [Example policy action script that tags resources](https://servicenow-prod.fluidtopics.net/cpwpOqvsWTtrt2TihEgmSw "Your instance tracks tagged resources for billing and reporting. Policy action scripts can add and modify resource tags. This example tags a storage volume resource.")

*[\>]: and then


