How to Trigger change workflow/record producer from transform script

Sachin Jain1
Giga Contributor

We have a requirement to create a change request with workflow and approval from a transform map script.

to create the change record, we have a record producer in place. But i'm not sure how to trigger the record producer from transform script and how to ploulate theRP variables.

I tried using Catalog API as mention below but it is creating a RITM instead of change request.

 

var cart = new Cart();
var item = cart.addItem('sys id of record producer');
cart.setVariable(item, 'RP variable name', source.TransformMapVariableName);
.....
var rc = cart.placeOrder();
gs.log(rc.number);

 

above code is not working, is there any other mathod to invoke change_request with workflow from a transform script ?

 

Pradeep Sharma can you help ?

37 REPLIES 37

Revanth7
Tera Contributor

i followed a similar approach and created the entries in question_Answer table but the catalog ui policies and the catalog client scripts are not working on the form. 

Did anybody fix this issue?

 

 

Robert Duca
Tera Contributor

Can anyone describe how to invoke the above script include from a transform script? I have a spreadsheet with about 1500 legacy HR case records that need to be imported and I would like to set a few of the variable answers on them. I also would like to set 'placeholder' values for all the variables I do not have answers for so they can go in later and update them manually. I have to this point broken it up into two separate imports one to the record table and the other to the 'question_answer' table once I know the record sys_ids but I would love to do this all with one import and transform script.

Also.. the record producer has about 75 variables on it so it would need to loop through all of them and set 'null' values for the questions to show up on the html variable formatter.

Any assistance would be of great help.

Sagar Rathore
Kilo Contributor

- replace gs.print with gs.info

- Replace applyTemplated with Scoped equivalent

- Added execution of the script block within the record producer

GP6
Giga Contributor

Hi @Daniel.Draes & @ianj and everyone.... 🙂

So we have our scripted Record Producer record - wonderful.  Now, how do we associate that back to the Record Producer so that all of the Catalog Client scripts are honored?  I went to the table 'sc_item_produced_record' and noticed that there was no record there...  I manually created a record there (after running the script above) and still - no luck.

How do we get this scripted record to link back to the producer and honor the catalog client scripts? 

Daniel A-C
Tera Expert

Below is a new updated version which also inserts a record correctly into the sc_item_produced_record table. This helps variable formatters to discover the relevant variable sets for the record producer. This ensures that variable set titles render correctly, Catalog UI policies attached to variable sets are applied and their catalog client scripts are run.

Apologies if I missed any credits.

//Copied from https://community.servicenow.com/community?id=community_question&sys_id=c5440f29dbd8dbc01dcaf3231f961942
//Credit: ianJ, Travis Toulson, Daniel.Draes and Cristiano
//Further Modified to insert a record in the sc_item_produced_record table
//Known gaps: This doesn't cater for multi-row variable sets (MRVS)
//
//Sample Usage:
//var rpUtil = new Base_RecordProducer('sys_id_of_record_producer_here');
//rpUtil.setVariable('variable_name','variable_value');
//var newRecordId = rpUtil.submit();
//
var Base_RecordProducer = Class.create();
Base_RecordProducer.prototype = {

    initialize: function(producerSysId) {
        this.producerSysId = producerSysId;
        this.producer = this._getProducer(producerSysId);
        this.targetTable = this.producer.table_name;
        this.userVariables = {};
        this.rpVariables = this._prepRecordProducerVariables();
    },

    setVariable: function(name, value) {
        this.userVariables[name] = value;
    },

    setVariables: function(variableObject) {
        this.userVariables = variableObject;
    },

    submit: function() {
        var targetRecord = new GlideRecord(this.targetTable);
        targetRecord.initialize();

        if (!gs.nil(this.producer.template)) {
            var template =   new GlideTemplate.get(this.producer.template);
            template.apply(targetRecord);
        }

        var v;
        // Set mapped fields on target record
        for (v in this.rpVariables) {
            if (this.rpVariables[v].mapToField) {
                targetRecord.setValue(this.rpVariables[v].field, this.userVariables[v] || '');
            }
        }

        // execute script part of Record Producer
        var _evaluator = new GlideScopedEvaluator();
        _evaluator.putVariable('current', targetRecord);
        _evaluator.putVariable('producer', this.userVariables);
        _evaluator.evaluateScript(this.producer, 'script');

        var targetSysId = targetRecord.insert();

        // if there's no target sys_id, don't create any question_answer entries
        if (targetSysId) {
            // One more loop - insert variables in question_answer table
            var qaGR;
            for (v in this.rpVariables) {
                qaGR = new GlideRecord('question_answer');
                qaGR.initialize();
                qaGR.question = this.rpVariables[v].sysId;
                qaGR.order = this.rpVariables[v].order;
                qaGR.table_name = this.targetTable;
                qaGR.table_sys_id = targetSysId;
                qaGR.value = this.userVariables[v];

                qaGR.insert();
            }

            //Insert an entry in the sc_item_produced_record table
            //This ensures that formatters render variable sets correctly
            //and apply Catalog UI Policies
            var siprGR = new GlideRecord('sc_item_produced_record');
            siprGR.initialize();
            siprGR.producer = this.producerSysId;
            siprGR.record_table = this.targetTable;
            siprGR.record_key = targetSysId;
            if (targetRecord.instanceOf('task')) {
                siprGR.task = targetSysId;
            }

            siprGR.insert();
        }

        return targetSysId;
    },

    _getProducer: function(producerSysId) {
        var scipGR = new GlideRecord('sc_cat_item_producer');

        if (scipGR.get(producerSysId)) {
            return scipGR;
        }

        return null;
    },

    _prepRecordProducerVariables: function() {
        var variables = {};
        var obj;
        var name;

        // individual variables
        var itemOptionGR = new GlideRecord('item_option_new');
        itemOptionGR.addQuery('cat_item', this.producerSysId);
        itemOptionGR.query();
		
        while (itemOptionGR.next()) {
            obj = {};
            name = itemOptionGR.getValue('name');

            obj.field = itemOptionGR.getValue('field');
            obj.mapToField = itemOptionGR.getValue('map_to_field');
            obj.sysId = itemOptionGR.getValue('sys_id');
            obj.order = itemOptionGR.getValue('order');

            variables[name] = obj;
        }

        // variable sets
        var variableSetGR = new GlideRecord('io_set_item');
        variableSetGR.addQuery('sc_cat_item', this.producerSysId);
        variableSetGR.query();

        while (variableSetGR.next()) {
            var itemOptionVSGR = new GlideRecord('item_option_new');
            itemOptionVSGR.addQuery('variable_set', variableSetGR.variable_set);
            itemOptionVSGR.addEncodedQuery('question_text!=null'); //check this line is required
            itemOptionVSGR.query();

            while (itemOptionVSGR.next()) {
                obj = {};
                name = itemOptionVSGR.getValue('name');
	
                obj.field = itemOptionVSGR.getValue('field');
                obj.mapToField = itemOptionVSGR.getValue('map_to_field');
                obj.sysId = itemOptionVSGR.getValue('sys_id');
                obj.order = itemOptionVSGR.getValue('order');
	
                variables[name] = obj;
            }
        }

        return variables;
    },

    type: 'Base_RecordProducer'
};