How to initialize standard change via UI action using a standard change template ...

Zod
Giga Guru

Hi,

I need to initiate a standard change via a template from an UI script, but failed so far.

NOT talking about form templates! Talking about standard change templates used to create standard changes from predefined & approved templates.

Any quick help on this?

Thank you!

1 ACCEPTED SOLUTION

Here is the Script Include (may be a bit easier to read here).
And as you can see, different variables on an RP can be called using:

var r = new global.RBA_Base_RecordProducer('54654654654654');
r.setVariable('description','This will be added to Description.');
r.submit();

var RBA_Base_RecordProducer = Class.create();
RBA_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.print("producerSysId: " + producerSysId);
		gs.print("target table: " + this.targetTable);
	},
	getProducer: function (producerSysId) {
		var gr = new GlideRecord("sc_cat_item_producer");
		if (gr.get(producerSysId)) {
			gs.print("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),
		producer,
		current;
		targetRecord.initialize();
		targetRecord.applyTemplate(this.producer.template.name);
		/* This should allow executing the Producer Script. */
		producer = this.userVariables; // Record Producer scripts have access to a producer variable that functions as a variable map like userVariables
		current = targetRecord; // Record Producer scripts have access to the target record as a current variable
		var scriptEval = new GlideScopedEvaluator(); // Evaluate the script
		scriptEval.putVariable('current', current);
		scriptEval.putVariable('producer', producer);
		scriptEval.evaluateScript(this.producer, 'script', null);

		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] || "");
			}
		}
		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: 'RBA_Base_RecordProducer'
};

View solution in original post

8 REPLIES 8

Zod
Giga Guru

I also need create the standard change via a script in a workflow ... 

Behshid Khairdi
Mega Expert

Hello,

Just go through the following link :

https://docs.servicenow.com/administer/form_administration/reference/r_ScriptedTemplates.html

 

Regards,

Behshid K

find_real_file.png

Thank you. But I'm refering to "Standard Change Templates" .. no to templates that are available on any form. 

felladin
Tera Guru

I have not tried using SC Templates, but I found a solution to create a record using a predefined Record Producer through a script (we use it to create standard changes through scheduled jobs).

I used this as basis for the Script Include:
https://community.servicenow.com/community?id=community_question&sys_id=c5440f29dbd8dbc01dcaf3231f961942

And then the schedule is called with:
// Execute RP A
runRecordProducer('82222f480fc5170079d87f5ce1050ec7');
// Execute RP B
runRecordProducer('a24ac49c0f09170079d87f5ce1050e2f');

function runRecordProducer(x) {
    var r = new global.RBA_Base_RecordProducer(x);
    r.submit();
}