I need to execute a powershell script onChange or onSubmit of catalog client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday
Dear developers,
I have a powershell script which works fine in flow action. However, I need to execute that snippet in catalog client script as onChange or onSubmit script. By doing that, based on the response of that script, form will be allowed or denied submission.
I am okay to execute that PS script as a flow action or Script Include or Business rule.
Could someone please help me here?
Thanks,
Suhas Marathe
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday
you can trigger that flow action via script using GlideAjax
syntax here
Scripting with Flows, Subflows, and Actions
You can also open that flow action, click 3 dots and click Create code snippet and use that script
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday - last edited Thursday
Please follow below steps:
Setup:
Create Script Include with name: PowerShellExecutor (Client callable: yes)
Replace 'YOUR_FLOW_SYS_ID' with your actual Flow sys_id
Replace 'your_field_name' with your catalog variable name
Create Catalog Client Script with Type: onChange
Script Include:
var PowerShellExecutor = Class.create();
PowerShellExecutor.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/**
* Execute PowerShell script and return result
*/
executePowerShellScript: function() {
var inputParam = this.getParameter('sysparm_input_param');
var scriptType = this.getParameter('sysparm_script_type');
try {
// Option 1: Execute via REST Message
var result = this._executeViaRestMessage(inputParam, scriptType);
// Option 2: Execute via Flow (if preferred)
// var result = this._executeViaFlow(inputParam, scriptType);
return JSON.stringify({
success: true,
result: result,
message: 'PowerShell script executed successfully'
});
} catch (error) {
gs.error('PowerShell execution failed: ' + error.getMessage());
return JSON.stringify({
success: false,
result: null,
message: 'Script execution failed: ' + error.getMessage()
});
}
},
/**
* Execute PowerShell via REST Message
*/
_executeViaRestMessage: function(inputParam, scriptType) {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('YOUR_POWERSHELL_ENDPOINT_URL'); // Configure your endpoint
request.setHttpMethod('POST');
request.setRequestHeader('Content-Type', 'application/json');
var requestBody = {
script_type: scriptType,
input_parameter: inputParam,
execution_context: 'servicenow_catalog'
};
request.setRequestBody(JSON.stringify(requestBody));
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if (httpStatus == 200) {
var parsedResponse = JSON.parse(responseBody);
return parsedResponse.result;
} else {
throw new Error('HTTP ' + httpStatus + ': ' + responseBody);
}
},
/**
* Execute PowerShell via Flow Action
*/
_executeViaFlow: function(inputParam, scriptType) {
// Trigger Flow and wait for response
var flowAPI = new sn_fd.FlowAPI();
var flowInputs = {
'input_parameter': inputParam,
'script_type': scriptType
};
var flowResult = flowAPI.startFlow('YOUR_FLOW_SYS_ID', flowInputs);
if (flowResult && flowResult.status == 'success') {
return flowResult.output;
} else {
throw new Error('Flow execution failed');
}
},
/**
* Validate input parameters
*/
validateInput: function() {
var inputParam = this.getParameter('sysparm_input_param');
if (!inputParam || inputParam.trim() == '') {
return JSON.stringify({
success: false,
result: null,
message: 'Input parameter is required'
});
}
// Add your validation logic here
return JSON.stringify({
success: true,
result: 'valid',
message: 'Input validation passed'
});
},
type: 'PowerShellExecutor'
});
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') return;
// Replace 'your_field_name' with actual field name
if (control.getFieldName() == 'your_field_name') {
// Show loading message
g_form.addInfoMessage('Validating... Please wait');
// Call PowerShell script via GlideAjax
var ga = new GlideAjax('PowerShellExecutor');
ga.addParam('sysparm_name', 'executePowerShell');
ga.addParam('sysparm_input_value', newValue);
ga.getXMLAnswer(function(response) {
var result = JSON.parse(response);
if (result.success) {
g_form.addInfoMessage('Validation passed: ' + result.result);
} else {
g_form.addErrorMessage('Validation failed: ' + result.message);
}
});
}
}
[ Architect | Certified Professional]
Was this response helpful? If so, please mark it as ✅ Helpful and ✅ Accept as Solution to help others find answers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday
Glad I could help! If this solved your issue, please mark it as ✅ Helpful and ✅ Accept as Solution so others can benefit too.
Chavan A.P. Technical Architect | Certified Professional
[ Architect | Certified Professional]
Was this response helpful? If so, please mark it as ✅ Helpful and ✅ Accept as Solution to help others find answers.