
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 08-14-2019 03:59 PM
If you have a Record Producer generate a record and needed copy those variables to an additional record (for example, when creating an incident from new call), then it's not enough to simply copy the variables in the question_answer table. That will copy the values, but the catalog policies and catalog client scripts will not be applied.
To get ServiceNow to work as though these variables were entered into your new record, you need to also update the table sc_item_produced_record. So duplicate the entries in this table for your new record as well, setting the fields task and the record_key to the new record's sys_id
(Thanks to Andrea Broglia for figuring this out)
var copyVariables = Class.create();
copyVariables.prototype = Object.extendsObject(AbstractAjaxProcessor, {
recordProducer: function(fromSysID, toTable, toSysID, ignoreBlanks, ignoreQuestions, whichRecordProducer){
//Example of usage
//new copyVariables().recordProducer('0af30e28dba0c0d03ea3cd051496193d', 'incident', 'b962dd24dba844d010a3a5840596199e', true, 'this_variable,that_variable,another_variable,u_affected_user','32bc1564db20c0d03ea3cd05149619f5')
//Note: this will NOT copy multi-row variable sets
fromSysID = gs.nil(fromSysID) ? this.getParameter('sysparm_fromSysID') : fromSysID;
toTable = gs.nil(toTable) ? this.getParameter('sysparm_toTable') : toTable;
toSysID = gs.nil(toSysID) ? this.getParameter('sysparm_toSysID') : toSysID;
ignoreBlanks = gs.nil(ignoreBlanks) ? this.getParameter('sysparm_ignoreBlanks') : ignoreBlanks;
ignoreQuestions = gs.nil(ignoreQuestions) ? this.getParameter('sysparm_ignoreQuestions') : ignoreQuestions ;
ignoreQuestions = ignoreQuestions.split(',');//Form an array so we can distinguish between questions to ignore
var originalVar = new GlideRecord('question_answer');
originalVar.query('table_sys_id',fromSysID);
while (originalVar.next()){
var question = originalVar.question.name.toString() || 'not-specified';//getDisplayValue(); //<<== gets the question rather than the name
var questionType = originalVar.question.type.getDisplayValue();
var validFieldName = question != '';
var validValue = (!gs.nil(originalVar.value));
if(ignoreBlanks && !validValue && validFieldName && !(questionType=='Container Start' || questionType=='Container End')){
//ignore variables without a valid value
}else{
//Ignore any sensitive fields
var ignoreThis = ignoreQuestions.filter(function(thisQ){return thisQ == question;}).length;
if(ignoreThis==0 || questionType=='Container Start' || questionType=='Container End'){
var newVar = new GlideRecord('question_answer');
newVar.initialize();
newVar.setWorkflow(false);
newVar.autoSysFields(false);
for(var eachField in newVar){
newVar[eachField] = originalVar[eachField];
}
newVar.table_name = toTable;
newVar.table_sys_id = toSysID;
var thisCopy = newVar.insert();
///////////////////////////////////////////////////////////////
var newRecordProd = new GlideRecord('sc_item_produced_record');
newRecordProd.initialize();
newRecordProd.setWorkflow(false);
newRecordProd.autoSysFields(false);
newRecordProd.producer = whichRecordProducer;
newRecordProd.record_table = toTable;
newRecordProd.record_key = toSysID;
newRecordProd.insert();
}
}
}
},
type: 'copyVariables'
});
- 6,723 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This is a good start, but can you share a script for accomplishing this? I can't figure out how I would know which records to copy from question_answer table. Also, when I look entries in sc_item_produced_record the field record_key is always empty

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Sorry for the late reply on this... record_key LOOKS empty, but it's not... Try running this in a background window with the sys_id for any record in the table sc_item_produced_record
var gr = new GlideRecord('sc_item_produced_record')
gr.get('9e278ddddbdc88903ea3cd0514961933')
gs.info(gr.record_key);
I've updated the article with the script
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Additional material if required
https://www.servicenow.com/community/developer-forum/dynamic-record-producer-records/td-p/1433706