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

Hi everyone.

I know it's been a while since the last answer to this question, but I'd like to share my 2 cents, 'cause this thread was an invaluable help for me.

The script include provided by @Daniel.Draes   works great if your record producer uses only variables, but it doesn't touch any variable sets.

That's why I've made a new version that works with both variables and variable sets.

I hope it's useful to someone.

Cheers.
Cristiano

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();
		gs.info('producerSysId: ' + producerSysId);
		gs.info('target table: ' + this.targetTable);
	},

	getProducer: function(producerSysId) {
		var gr = new GlideRecord('sc_cat_item_producer');

		if (gr.get(producerSysId)) {
			gs.info('Yep, we found the RP: ' + gr.name);
			return gr;
		}

		return null;
	},

	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 == true) {
				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);
		var _evaluator_result =  _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 qa;
			for (v in this.rpVariables) {
				qa = new GlideRecord('question_answer');
				qa.initialize();
				qa.question = this.rpVariables[v].sysId;
				qa.order = this.rpVariables[v].order;
				qa.table_name = this.targetTable;
				qa.table_sys_id = targetSysId;
				qa.value = this.userVariables[v];

				qa.insert();
			}
		}

		return targetSysId;
	},

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

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

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

			variables[name] = obj;
		}

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

		while (grVariableSet.next()) {
			var grItemOptionVS = new GlideRecord('item_option_new');
			grItemOptionVS.addQuery('variable_set', grVariableSet.variable_set);
			grItemOptionVS.addEncodedQuery('question_text!=null');
			grItemOptionVS.query();

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

		return variables;
	},

	type: 'Base_RecordProducer'
};

Cristiano,

 

This script is adding the variables to my script-created RP but the variables it needs to add are in a multi-row variable set. Any idea how this can be used for that? I'm having issues determining how to display the variables within the MRVS. They are being displayed one after the other without the MRVS functionality to add more rows.

Hey Blair.

Interesting...

I didn't test it with MRVS, 'cause I was using just single row VSs. Maybe there are other tables involved when we're dealing with MRVS.

I'll have a look at this and post my findings here.

Cheers.

GP6
Giga Contributor

Wow @Daniel.Draes - credit where it is due here big time.  This is a work of art and works very well indeed - thank you.  I have my record produced, and variables created in perfect order ready to be populated either by a script or manually (which is handy because we need both).

Thank you well done.

Daniel Draes
ServiceNow Employee
ServiceNow Employee

Thanks, happy this is helpful. It was @ianj 's work which I just enhanced a slight bit 😄