Automatically fill the Multi row variable set from data in the custom table

Bhavana Reddy
Mega Guru

Hello Team,

We have a catalog item which has a multi row variable set and initially the User fills the data( we are storing these details in the Custom table) and submits the request and next time when they want to resubmit the request we need to show in the multi row variable set what data they entered previously

for ex: if they add 3 rows and submit the request , next time when they select "Resubmit By editing" then we need to show the previous values entered by the user in the Multi row variable.( we have 4 fields in the MRV)

Please help me guys!!

Thanks,

Bhavana

1 REPLY 1

Subrahmanyam2
Giga Guru

Hi Bhavana,

Please try this.
I am taking an example where my MRVS variable name is "mr", and the previous MRVS variable value stored in the database i.e. custom table field is [{"user":"a8f98bb0eb32010045e1a5115206fe3a","manager":"62826bf03710200044e0bfc8bcbe5df1"}]
user and manager are the variables which stores user and manager information for each row.
In MRVS variable, when this field value is stored, it is stored as array of JSON objects (You may already know this.

In your custom table if you are storing the MRVS object as an array directly, and I am assuming you are using GlideAjax call to pull the previous MRVS object info.

On the server side script include, convert the JSON object to String using the the JSON.stringify() and send it to browser, and in the browser you can use client script to set the variable value.

g_form.setValue('mr', '[{"user":"a8f98bb0eb32010045e1a5115206fe3a","manager":"62826bf03710200044e0bfc8bcbe5df1"}]');


Also you do not even need the custom table I guess, you can just identify the latest RITM raised by the same user prior to the current record submission and pull the value of the MRVS variable from your client callable script include. If this fits your need, you can save on custom table licensing cost. [I assume you have already identified a way to trigger the MRVS variable autofill logic only if user clicks on Resubmit By Editing button (like using a URL parameter or session variable), if that is the case, you may include that logic in the client script I suggested below to tune it to your need]

 

Here is the approach I took in PDI to implement above suggestion.

In my PDI, for a test catalog item, I created MRVS variable with name "mr".

Please find the client script below which makes an AJAX call to server to pull the MRVS variable value of recent same items RITMS opened by the same user and sets the "mr" variable value same as the previous one.
find_real_file.png

function onLoad() {
    var ga = new GlideAjax("TestGA");
    ga.addParam("sysparm_name", "getMRVSVarVal");
    ga.addParam("sysparm_catItem", g_form.getUniqueValue()); //Make sure that this script only executes on the catalog item view, this will not work on RITM view. If you need on ritm view, you should create separate on load client script and change this line to ga.addParam("sysparm_catItem", g_form.getValue("cat_item"));
    ga.addParam("sysparm_varName", 'mr'); //MRVS variable name must be passed here
    ga.getXML(setMRVSVar);

    function setMRVSVar(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer != '')
            g_form.setValue('mr', answer);
    }
}

 

Please find the server-side script include below.

find_real_file.png

var TestGA = Class.create();
TestGA.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getMRVSVarVal: function() {
        var ritmGR = new GlideRecord('sc_req_item');
		ritmGR.addQuery("cat_item", this.getParameter('sysparm_catItem'));
		ritmGR.addQuery("opened_by", gs.getUserID());
		ritmGR.orderByDesc("sys_created_on");
		ritmGR.setLimit(1);
		ritmGR.query();
		if(ritmGR.next()){
            return ritmGR.variables[this.getParameter('sysparm_varName')].toString();
        }
		return '';
    },
    type: 'TestGA'
});

 

Thanks and regards,

Subrahmanyam Satti