Script action: move a function of a script include and execute it in a script action

fabrizio95360
Tera Expert

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;
},

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@fabrizio95360 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@fabrizio95360 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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");
    }