Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

What is the ScriptingGeneratorFactory?

e_wilber
Tera Guru

Long story short, I am learning Recommended Actions and trying to build one but I am stuck at the step where you use a generator to use a script.

 

e_wilber_0-1722877576587.png

 

How do you create a script include for this? It's not a standard script include because an OOB example is

e_wilber_1-1722877639638.png

 

All the documentation says is "You must create an implementation of the scriptinggeneratorfactory template to use a script include on https://docs.servicenow.com/bundle/washingtondc-customer-service-management/page/product/customer-se... and there are no examples. Has anyone set one of these up before know how to create a proper SI for this?

9 REPLIES 9

Great!! Thank you so much @Mike-Thompson . It works for me as well. The only missing part was actually the "Extension Point" record.
I have created a corresponding record for my Script Include:
/now/nav/ui/classic/params/target/sys_extension_instance_list.do%3Fsysparm_query%3DpointSTARTSWITHsn_nb_action.ScriptingGeneratorFactory

And after that the resource generator in the Recommendation record could be mapped (sn_nb_action_recommended_action) and everything works:

alangross_0-1750911609232.png

For Reference my Script Include (which does not do much yet but recommending hardcoded a single service). 

var bjbMLOPS_incident_suggestServiceGeneratorV2 = Class.create();
bjbMLOPS_incident_suggestServiceGeneratorV2.prototype = Object.extendsObject(sn_nb_action.ScriptingGeneratorHandlerBase, {
    category: 'ra_scripting_generator', // DO NOT REMOVE THIS LINE!

    getId: function() {
        return "sn_sow_inc.bjbMLOPS_incident_suggestServiceGeneratorV2";
    },

    getOutputSchema: function() {
        var schema = [{
            'name': 'predictedRecord',
            'label': 'Predicted Record',
            'type': 'reference',
            'referenceTable': 'cmdb_ci_service'
        }];
        return {
            'status': 'success',
            'schema': schema,
        };
    },

    getOutputs: function(param) {
        try {
            //var nonMLRecommendationsHelper = new sn_sow_inc.NonMLRecommendationsHelper();
            //var similarOpenIncidentsSysIds = nonMLRecommendationsHelper.getSimilarOpenIncidentsWithFilters(param.contextRecord);
            var response = {
                'status': 'success',
                outputs: []
            };
			
			// return single service as recommendation (as a test)
			response.outputs.push({predictedRecord: "3ca79da0ad24b0106d379e95d3892905"}); // SV-JIRA

           
            return response;
        } catch (e) {
            return {
                'status': 'error',
                'errorCode': 40001,
                'errorMessage': 'Failed to get recommendations for Incident Service' // this will be logged
            };
        }
    },
    type: 'bjbMLOPS_incident_suggestServiceGeneratorV2'
});

 

 

@Mike-Thompson 

The hard coded recommendation works - but now I already bump into the next challange.

I try to grab the Incident short_description and description of the Incident. But the parameter object "param" in the function getOutputs contains a contextRecord object with all fields being empty.

Are you able to pass any information to the Scripted Resource generator from the underlying record?

@alangross 

can you try adding the following lines of code to your script include in the try statement of getOutputs to see if it will give you actual data? I assume that you tried toString or JSON.stringify on param.contextRecord and it looked like "number":{},"short_description":{}.....? I noticed the same but when i dot-walked to the field i got a value. 

gs.info("Short Description: " + param.contextRecord.short_description);
gs.info("Description: " + param.contextRecord.description);

 

@Mike-Thompson 🚀 Thank you once more!

Indeed - I was using JSON.stringify - and you are right when using param.contextRecord.short_description it works. Seems to be a special kind of Object.

@alangross Glad i was able to help, and i have put together a guide covering all of this which can be found here: https://www.servicenow.com/community/developer-articles/script-your-way-to-smarter-recommended-actio...