logo

NJP

Copy variables from one record to another

Import · Aug 14, 2019 · article

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'
});
View original source

https://www.servicenow.com/community/now-platform-articles/copy-variables-from-one-record-to-another/ta-p/2327541