logo

NJP

Setting Variable value through script includes

Import · Sep 17, 2019 · article

SetValueByScript.

UseCase : Need to set value for stackName as Stack_ attribute.

Examples :If NodeName is “acpNode01”

StackName should be “Stack_acpNode01”

When the user will change the nodeName, stackName should get updated automatically.

Solution : We can achieve this using SetValueByScriptAction

SetValueByScriptAction : Action provides you a feature to call any script includes the function and you can return the value which you want to set it in Form Field.

(Make sure script include must be created in sn_cmp scope)

1. You need to create one script include like.PopulateStackName.2. Create one method in script include with one paremeter

See getStackName method defined below

var PopulateStackName = Class.create(); PopulateStackName.prototype = { initialize: function() { }, getStackName: function(stack_name) { return "Stack_"+stack_name;

},

type: 'PopulateStackName'

};

3. Now as you already created Script Includes and method you want to call from the action.4. Navigate to Blueprint --> Form --> Form Parameter.5. Click on NodeName attribute of Virtual Server ResourceIt will be named as _NodeName. Like : Virtual_Server_NodeName6. Scroll down to see the rules defined for this parameter.7. Click on New buttons in rule8. Fill the value like Name, SourceFiled, Order and Event. Like below 9. Click on Action tab below.10. Click on new buttons

11. Fill the following values for Action

As you can see in image you don’t need to specify datasource for SetValueByScript function.12. Specify Name and ActionType as “SetValueByScript”.

13. Target Field as “StackName”

14. Value should be expression like below

$(Script:PopulateStackName.getStackName[arg=${parameter.Virtual_Server_NodeName}])

Please validate below checks.

1. Script must start with “$(Script:”2. PopulateStackName.getStackName<Method defined in script include>3. [arg=${parameter.Virtual_Server_NodeName}]You need to define parameter for method you can have multiple parameters like below4. [arg=${parameter.Virtual_Server_NodeName},</p> <p>arg=${parameter.Virtual_Server_NodeValue}]</p> <p>Labels: </p> <ul> <li><a href="https://community.servicenow.com/community/itom-articles/tkb-p/it-operations-management-kb/label-name/cloud%20provisioning%20and%20governance?labels=cloud+provisioning+and+governance" target="_blank" rel="noopener">Cloud Provisioning and Governance</a></li> <li><a href="https://community.servicenow.com/community/itom-articles/tkb-p/it-operations-management-kb/label-name/multiple%20versions?labels=multiple+versions" target="_blank" rel="noopener">Multiple Versions</a></li> </ul> <p><a href="https://community.servicenow.com/now-platform/latest-release.html" target="_blank" rel="noopener"><img src="https://community.servicenow.com/community/s/html/assets/Xanadu-Community_Banner_1_314x314.jpg" alt="image"></a></p>

View original source

https://www.servicenow.com/community/itom-articles/setting-variable-value-through-script-includes/ta-p/2325500