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

AnishSasidharan
Mega Expert

Hi Sachin,



Requirement is not that much clear... Are you trying to create the change request through record producer with help of transform script?



Transform map is used to create / update the record, so you can create a request with help of transform map itself.. And workflow will get attached, once you insert a new record in the change request table...



You can able to accomplish through transform script but i am not sure why you are trying to through transform script..



Thanks


Daniel Draes
ServiceNow Employee
ServiceNow Employee

I have updated this script include as mentioned above to cope for scoped application development.

 

- replace gs.print with gs.info

- Replace applyTemplated with Scoped equivalent

- Added execution of the script block within the record producer:
Some special note on this, the variable producer is not available in this script at this point. Current though works just fine.

 

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 grItemOption = new GlideRecord("item_option_new");
				grItemOption.addQuery("cat_item", this.producerSysId);
				grItemOption.query();
				
				while (grItemOption.next()) {
					var obj   = {};
						var 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;
					}
					
					return variables;
				},
				
				
				type: 'Base_RecordProducer'
			};

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?

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.

Fogg,

Very useful. How would I add a Variable Set to this?