Populate MRVS with values retrieved from previously submitted MRVS values

KBKennedy
Tera Contributor

I was recently looking for a solution to accomplish this but could not find one here. Now that I have worked it out, I'm going to leave my solution here for anyone else that may be having this same question.

 

Backstory: Client has a catalog item that uses a MRVS to submit information that was previously done with Excel sheets. Each row has 10 questions, and they will be submitting anywhere from 10-20 rows each time. It will be the same group of users submitting this item each time, and they will need to do it monthly, often with much of the same values. So, they wanted a way to reference the RITMs they've previously submitted for this item, and then have the MRVS be automatically populated with the values they previously submitted. They could then make any adjustments as needed.

 

I hope this helps someone!

 

onChange Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
   
    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('populateStandardCostTransferMRVS'); //script include to call
    ga.addParam('sysparm_name', 'autofillMRVS'); //function to call within script
    ga.addParam('sysparm_reference', newValue); //pass sysID of previously submitted RITM for this item
    ga.getXMLAnswer(callBack);
 
    function callBack(answer) {
        //process returned JSON from GlideAjax call
        if (answer) {
            var parsed = JSON.parse(answer);
            var count = parsed.count;
            var arr = [];
 
            for (i=0; i<count; i++) {
                var content = parsed[i];
                content = content.toString();
                arr.push(content);
            }
            arr = "[" + arr + "]";
           
            g_form.setValue('standard_cost_transfer_mrvs', arr);
   
        } else {
            g_form.addErrorMessage("Was not able to make script call");
        }
    }
 
}

 

 

Script Include

var populateStandardCostTransferMRVS = Class.create();
populateStandardCostTransferMRVS.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    //function to populate current MRVS values with previously submitted values
    //@params include 'reference' - sys_id of previously submitted RITM

    autofillMRVS: function() {
        var reference = this.getParameter('sysparm_reference'); //sysID of previously submitted item to reference
        //query the RITM table for referenced record
        var ritm = new GlideRecord('sc_req_item');
        ritm.addQuery('sys_id', reference);
        ritm.query();
        if (ritm.next()) {
            //get mrvs variables from ritm. This is an array of objects
            var mrvs = ritm.variables.standard_cost_transfer_mrvs;
            var arrObj = {};
            var rows = mrvs.getRows();
            arrObj["count"] = rows.length.toString();
            for (i=0; i < rows.length; i++) {
                var content = mrvs[i];
                content = content.toString();
                arrObj[i] = content;
            }
        }
        var strObj = JSON.stringify(arrObj);
       
        return strObj;
       
    },
 
    type: 'populateStandardCostTransferMRVS'
});

 

0 REPLIES 0