Submit a Record Producer from Run Script action on Workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2020 06:02 AM
We have a catalog item (CI) for report requests. A question on this CI asks if the report is to contain HR data. If the answer to the HR question is yes, we have been asked to create a new HR case concurrent with the resulting RITM. In addition, we want the variables on the RITM to be contained on the Variable Editor of the resulting HR Case.
The solution of the top of my head is to set the CI’s workflow to submit a Record Producer (RP) with identical variables and copy the variable values from the CI to the RP, and then submit. The reason for the variables present on the HR Case is for reporting (I would prefer not to create brand new fields on the HR Case table).
I found another post in using a Script Include to create and submit a RP, but am having trouble populating the variables from the CI to the RP. My inability is likely due to my own lack of understanding/experience with scripting.
I have successfully called the Script Include from the Run Script action in the workflow (as evidenced by log entries), but am unsure how to populate the variables on the Record Producer and submit.
Any suggestions?
SCRIPT INCLUDE BELOW:
var Submit_RecordProducer = Class.create();
Submit_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();
},
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();
targetRecord.applyTemplate(this.producer.template.name);
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: 'Submit_RecordProducer'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2020 06:12 AM
Is there any reason you have to use a record producer? You can create records directly in the table using very simple scripting methods. This way seems unnecessarily complex.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2020 06:34 AM
The approach is admittedly complex. While I can easily create the HR Case via script, I am unable to copy the variables from the RITM to the Variables on the HR Case. The only way I know how to create variables on the HR Case is to submit a Record Producer.