UI macro variable: Copy data from one mrvs to another

Sharad Patel
Tera Expert

Hi team,

I had a requirement to copy some fields from one mrvs to another mrvs within catalog task. So I had created a custom variable with UI macro which uses g_form.setValue & JSON.stringify to set data in destination mrvs. The macro works but it reloads the page and when page reloads, the desination mrvs looses the data and its empty.

Anyway I can prevent UI macro to not reload or cancel reload? or any alternate solution to copy data from one mrvs to another in a catalog item.

Regards,

Sharad

13 REPLIES 13

Sure

Do share the solution so that it helps members

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Sagar Agarwal
Mega Guru

Hi Sharad,

 

I do not prefer UI macro, though setting g_form.save() should have saved the details I won't dive into that.

 

I'll implement this with a different approach,

I'll have a client script or UI action to execute the code. instead of copying data from moves to moves, I'll run a glide ajax code and save the data on the "question_answer" table which actually stores the information in variables.

 

Mrvs is saved as a different variable on this table instead of one single variable with data as JSON string, so it would be just filtering and updating each column from your moves as a record on this table

 

Table: question_answer

filter using: table name, table_sys_id (record sys_id), question.

 

 

If my answer helped you in any way, please then mark it as helpful.

Kind regards,

Sagar

MVRS is not a question. MRVS data is saved in a different table altogether: sc_multi_row_question_answer & not in question_answer.

Sharad Patel
Tera Expert

Here's the alternate solution using a checkbox:

 

//OnChange script on the checkbox between MRVS1 & MRVS2
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    if (newValue == 'true') {
        //alert(newValue);
        try {
            var ajax = new GlideAjax('MyDevAJAX');
            ajax.addParam('sysparm_name', 'getMRVS2');
            ajax.addParam('sysparm_mrvs1', g_form.getValue('mrvs1'));
            ajax.getXML(setmrvs2);
        } catch (e) {
            g_form.clearMessages();
            g_form.addInfoMessage('Error: ' + e);
        }
    }
}

function setmrvs2(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    //alert(answer);
    if (answer) {
        g_form.setValue('mrvs2', answer);
        var setTimer = '';
        setTimer = setTimeout(setTimerDelay, 500); // to set the timer in milliseconds
        g_form.setValue('chk_copy_from_mrvs1_to_mrvs2', 'false');
    }
}

function setTimerDelay() {
    alert('Completed copying Billing Information to Pay Information table');
}
//AJAX function
getMRVS2: function () {
    var mrvs1 = this.getParameter('sysparm_mrvs1') + '';
    mrvs1Obj = JSON.parse(mrvs1);
    var mrvs2Obj = [];

    for (var i = 0; i < mrvs1Obj.length; i++) {
        var tempObj = {};
        tempObj.field4 = mrvs1Obj[i].field1.toString() + '-' + mrvs1Obj[i].field2.toString();
        tempObj.field5 = mrvs1Obj[i].field3.toString();

        mrvs2Obj.push(tempObj);

        var jsonStr = JSON.stringify(mrvs2Obj);
        return jsonStr;
    }
}

 

Hope it helps.

 

Regards,

Sharad