Flow action code snippet is not working on client side
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 12:35 PM
Hello,
I am trying to use the flow action on onChange Client script, but I am getting this error in the console
" Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'status')"
Below is the script I am using
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
(function() {
var site = '/sites/testsitesnow';
var dl = 'testinte';
var inputs = {};
inputs['tenant'] = { // GlideRecord
table: 'sn_sp_spoke_tenant',
sys_id: '34ba452c1bff6990ff7b20ecac4bcbe9',
};
inputs['site_relative_url'] = site; // String
inputs['document_library_relative_url'] = dl; // String
inputs['folder_location'] = ''; // String
GlideFlow.startAction('global.get_folders_inside_folder', inputs)
.then(function(execution) {
return execution.awaitCompletion();
}, errorResolver)
.then(function(completion) {
var status = completion.status;
// Available Outputs:
var outputs = completion.outputs;
// var status = outputs['status']; // Choice
var error_message = outputs['error_message']; // String
var filefolderlist = outputs['filefolderlist']; // String
var folder_count = outputs['folder_count']; // String
alert('test123 ' + folder_count);
});
function errorResolver(error) {
// Handle errors in error resolver
}
})();
}
May I know why I am getting this error?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 07:52 PM
@Saurav Bhardwa2 You are trying to user Server side code (script to trigger the flow) inside a client script which will not work.
I recommend you to create a Script Include which will have a method, call this script include and method using GlideAjax. Following is an example.
Here is the code for client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('HelloWorld'); // HelloWorld is the script include class
ga.addParam('sysparm_name','triggerFlow'); // helloWorld is the script include method
//ga.addParam('sysparm_user_name',"Bob"); // Set parameters for flow here
ga.getXML(HelloWorldParse); /* Call HelloWorld.helloWorld() with the parameter sysparm_user_name set
and use the callback function HelloWorldParse() to return the result when ready */
// the callback function for returning the result from the server-side code
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}
Here is how you can prepare a client callable script include.
var HelloWorld = Class.create();
HelloWorld.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
triggerFlow: function() {
(function() {
var site = '/sites/testsitesnow';
var dl = 'testinte';
var inputs = {};
inputs['tenant'] = { // GlideRecord
table: 'sn_sp_spoke_tenant',
sys_id: '34ba452c1bff6990ff7b20ecac4bcbe9',
};
inputs['site_relative_url'] = site; // String
inputs['document_library_relative_url'] = dl; // String
inputs['folder_location'] = ''; // String
GlideFlow.startAction('global.get_folders_inside_folder', inputs)
.then(function(execution) {
return execution.awaitCompletion();
}, errorResolver)
.then(function(completion) {
var status = completion.status;
// Available Outputs:
var outputs = completion.outputs;
// var status = outputs['status']; // Choice
var error_message = outputs['error_message']; // String
var filefolderlist = outputs['filefolderlist']; // String
var folder_count = outputs['folder_count']; // String
gs.info('test123 ' + folder_count);
});
function errorResolver(error) {
// Handle errors in error resolver
}
})();
},
type: 'HelloWorld'
});
Please make appropriate changes in the above script according to your context and then you will be able to call flow from client script by making the suggested changes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2023 10:50 AM - edited 10-15-2023 10:51 AM
Hey Saurav,
Sandeep's reply is correct. I just want to add that I would think carefully before doing this from a client script. Much better to move this to an after or async business rule or even better, just set a flow to trigger on the state change.