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

Yes... Below works. Fetching data and putting data into mrvs2 is not a problem... After clicking button and before page reloads, I see data showing up in mrvs2 for one second. But when page reloads automatically, mrvs2 is blank.

 

<?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 mrvs1Str = g_form.getValue('mrvs1');
			var mrvs1Obj = JSON.parse(mrvs1Str);
			var mrvs2Obj = [];
			alert(mrvs1Str);
        	var ritm = new GlideRecord('sc_req_item');
			ritm.get(g_form.getValue('request_item'));
			alert(ritm.number.toString());
		
			for (var i=0; i &lt; mrvs1Obj.length; i++) {
				var tempObj = {};
				tempObj.field4 = mrvs1Obj[i].field1;
				tempObj.field5 = mrvs1Obj[i].field3;

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

your script is saving the form and reloading it

what you need to do is this

1) use GlideAjax and send RITM sys_id and 1st MRVS json & update the variables like this

var ritm = new GlideRecord('sc_req_item');

ritm.get(this.getParameter('sysparm_ritm'));

ritm.variables.mrvs2 = this.getParameter('sysparm_mrvs1');

ritm.update();

Regards
Ankur

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

But I am not copying as is... I need to pick and choose fields from mrvs1 to update in mrvs2. Lets say:

mrvs2.field4 gets mrvs1.field1 + '-' + mrvs1.field2

mrvs2.field5 gets mrvs1.field3

Regards,

Sharad

Hi,

then perform the same manipulation in script include function within Ajax

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

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

It works and does not work.

I had added ajax call first and it didn't work.

After that, since, its ajax, I added half a second timeout after which, mrvs1 is copied to mrvs2 and is saved but the page reloads to a new task form.

 

I do have an alternative solution to use a checkbox instead of a button but button seems more appealing.

 

Regards,

Sharad