How to Trigger change workflow/record producer from transform script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-04-2014 11:45 AM
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 ?
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2022 02:00 PM
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 (Daniel A-C)
//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 targetGR = new GlideRecord(this.targetTable);
targetGR.initialize();
if (!gs.nil(this.producer.template)) {
var template = new GlideTemplate.get(this.producer.template);
template.apply(targetGR);
}
var v;
// Set mapped fields on target record
for (v in this.rpVariables) {
if (this.rpVariables[v].mapToField) {
targetGR.setValue(this.rpVariables[v].field, this.userVariables[v] || '');
}
}
// execute script part of Record Producer
var _evaluator = new GlideScopedEvaluator();
_evaluator.putVariable('current', targetGR);
_evaluator.putVariable('producer', this.userVariables);
_evaluator.evaluateScript(this.producer, 'script');
var targetSysId = targetGR.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 (targetGR.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'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2022 10:57 PM
Can anyone please tell me how to invoke this script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2022 09:40 AM
Hi,
This requirement is very urgent for me.
How to invoke Variable sets in Business Rule?
I can see the code in Script Include :
setVariables: function(variableObject) {
this.userVariables = variableObject;
},
Through setVariables, can I call Variable sets? Need to confirm what to pass in the variable object? i mean in what format, Please help as it is very much urgent.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2022 04:16 PM
Daniel, this is amazing and has saved me a bunch of time, thank you! I think this needs some higher visibility, maybe in a blog post or something.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2022 07:42 PM
One small addition:
var itemOptionGR = new GlideRecord('item_option_new');
itemOptionGR.addQuery('cat_item', this.producerSysId);
itemOptionGR.addQuery('active', '=', true); //Added so that inactive variables are not created.
itemOptionGR.query();