Script action: move a function of a script include and execute it in a script action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 03:26 AM - edited 01-22-2025 03:27 AM
Hi community, I don't have much experience with script actions, my request is to move this function and make it work in the background. I was thinking of a script action, but I don't know how to structure it and then call it in my script includes. this is my function:
generateCHILDBIA: function(current) {
var bia_generated = false;
var parentProcessGR = new GlideRecord('sn_bcm_business_process');
if (parentProcessGR.get(current.applies_to)) {
var childProcessGR = new GlideRecord('sn_bcm_business_process');
childProcessGR.addQuery('u_parent', parentProcessGR.sys_id);
gs.info('Query BCM F: ' + childProcessGR.getEncodedQuery());
childProcessGR.query();
var count = 0;
try {
while (childProcessGR.next()) {
var templateGR;
gs.info('Child found: ' + count++);
templateGR = new GlideRecord('sn_bia_template');
templateGR.addQuery('u_company', current.company);
templateGR.addQuery('u_bcm_pre_screening_bia', 'bia_standard');
templateGR.query();
var templateId;
if (templateGR.next()) {
templateId = templateGR.sys_id;
gs.info("Template found with company: " + templateId);
var biaAnalysisGR = new GlideRecord('sn_bia_analysis');
biaAnalysisGR.initialize();
biaAnalysisGR.name = 'BIA-' + childProcessGR.u_name;
biaAnalysisGR.template = templateId;
biaAnalysisGR.u_parent_bia = current.sys_id;
biaAnalysisGR.applies_to_table = 'sn_bcm_business_process';
biaAnalysisGR.applies_to = childProcessGR.sys_id;
biaAnalysisGR.company = current.company;
biaAnalysisGR.insert();
bia_generated = true;
gs.info("Record created in 'sn_bia_analysis' for process child: " + childProcessGR.u_name);
} else {
gs.info('Template not found');
}
}
} catch (e) {
var message = e.message;
gs.info('Message Error BCM: ' + message);
}
} else {
return false;
gs.info("Impossible");
}
return bia_generated;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 03:55 AM
Do this
1) create event in event registry
2) create script action and link it with above event
3) tirgger event using gs.eventQueue('event_name', glideRecordObject.sys_id);
4) then in script action get the sys_id using event.parm1 and query and then paste the script include code
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
01-22-2025 04:02 AM
your script include function is returning something.
So some script might be calling it and getting the value and processing it further.
this is not possible when you use script action and event as events are processed asynchronously
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
01-22-2025 07:28 AM
it does not work, in my script include/script action:
generateCHILDBIA: function(current) {
gs.eventQueue('sn_bia.bcm_generate_child_bia', current)
},
////Script Action
//var bia_generated = false;
var parentProcessGR = new GlideRecord('sn_bcm_business_process');
if (parentProcessGR.get(current.applies_to)) {
var childProcessGR = new GlideRecord('sn_bcm_business_process');
childProcessGR.addQuery('u_parent', parentProcessGR.sys_id);
gs.info('Query BCM F: ' + childProcessGR.getEncodedQuery());
childProcessGR.query();
var count = 0;
try {
while (childProcessGR.next()) {
var templateGR;
gs.info('Child found: ' + count++);
templateGR = new GlideRecord('sn_bia_template');
templateGR.addQuery('u_company', current.company);
templateGR.addQuery('u_bcm_pre_screening_bia', 'bia_standard');
templateGR.query();
var templateId;
if (templateGR.next()) {
templateId = templateGR.sys_id;
gs.info("Template found with company: " + templateId);
var biaAnalysisGR = new GlideRecord('sn_bia_analysis');
biaAnalysisGR.initialize();
biaAnalysisGR.name = 'BIA-' + childProcessGR.u_name;
biaAnalysisGR.template = templateId;
biaAnalysisGR.u_parent_bia = current.sys_id;
biaAnalysisGR.applies_to_table = 'sn_bcm_business_process';
biaAnalysisGR.applies_to = childProcessGR.sys_id;
biaAnalysisGR.company = current.company;
biaAnalysisGR.insert();
// bia_generated = true;
gs.info("Record created in 'sn_bia_analysis' for process child: " + childProcessGR.u_name);
} else {
gs.info('Template not found');
}
}
} catch (e) {
var message = e.message;
gs.info('Message Error BCM: ' + message);
}
} else {
gs.info("Impossible");
}