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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

can you share your relevant scripts and screenshots

Regards
Ankur

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

Below is the setup I was able to recreate the issue in my personal Dev instance. Same macro works well on catalog item intake form. But on catalog task, it refresh on click and I loose new data in MRVS2:

 

find_real_file.png

 

find_real_file.png

 

Below is the macro script:

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<style>#copy_recs { background-color: #278efc; color: #ffffff;}</style>
<button id="copy_recs" onClick="fillMRVS2();">Copy from MRVS1 to MRVS2</button>
	<script>
		function fillMRVS2() {
			var mvrs1Str = g_form.getValue('mrvs1');
			var mvrs1Obj = JSON.parse(mvrs1Str);
			var mvrs2Obj = [];
		
			for (var i=0; i &lt; mvrs1Obj.length; i++) {
				var tempObj = {};
				tempObj.field4 = mvrs1Obj[i].field1;
				tempObj.field5 = mvrs1Obj[i].field3;

				mvrs2Obj.push(tempObj);
				
			}
			g_form.setValue("mrvs2", JSON.stringify(mvrs2Obj));
			g_form.save();
		}
	</script>
</j:jelly>

 

Note: Maroonbyte = Sharad Patel 🙂

 

Did you try to add alert and check what comes in MRVS?

you can try to query and get RITM variable value

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <style>#copy_recs { background-color: #278efc; color: #ffffff;}</style>
    <button id="copy_recs" onClick="fillMRVS2();">Copy from MRVS1 to MRVS2</button>
    <script>
        function fillMRVS2() {
        var mvrs1Str = g_form.getValue('mrvs1');
        alert(mvrs1Str);
        
        // query RITM with g_form.getValue('request_item') if you are on sc_task form
        
        var mvrs1Obj = JSON.parse(mvrs1Str);
        var mvrs2Obj = [];

        for (var i=0; i &lt; mvrs1Obj.length; i++) {
        var tempObj = {};
        tempObj.field4 = mvrs1Obj[i].field1;
        tempObj.field5 = mvrs1Obj[i].field3;

        mvrs2Obj.push(tempObj);

        }
        g_form.setValue("mrvs2", JSON.stringify(mvrs2Obj));
        g_form.save();
        }
    </script>
</j:jelly>

Regards
Ankur

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