Copying previous MRVS values into new add entry

Rob Weiss
Giga Contributor

 

In short, I am attempting to copy the values of the previous Multi-Row Variable Set entry (if any have been made on the form yet) into the fields of new entries.

 

Basically, I want to use the previous entry values from the MRVS table to populate the defaults for any new entries.  The use case is that I have a MRVS with 6 fields, and the majority of the time, most of those fields will remain the same when a user is adding new entries.  For example, adding multiple ports to the same network switch would likely have all of the same field values except for the port number.  The goal is to save the users from having to populate the other 5 fields for every entry they do.

 

The problem is that I can't seem to find a way to access the previously entered MRVS values via g_form.  It doesn't seem to be aware of the form on the parent page, but only of the fields in the "add new" window for the new entry.  I am attempting to create the client script using the onLoad type applied to my (MRVS) Variable Set.

 

I can get and set the values on the current "add new" box/window like this:

g_form.setValue('variables.switch_name', 'myswitchname');

 

But that will not give me access to the previously added entries, which should be in a list of dictionaries, not directly in variables like that.

 

In server scripts, I can manipulate the MRVS easy enough.  It is a list of dictionaries, and would be accessed through the GlideRecord something like this:

gr.variables.my_mrvs_name[0].switch_name

 

Does anyone know how to access the MRVS values on a new form from the "add" box/window?  I have done a considerable amount of searching on this and am pretty surprised to not find a single person trying to perform this same type of function.

1 ACCEPTED SOLUTION

The solution for Service Portal required a bit of a work-around, but here is what we finally came up with:

First, create an onLoad script on your Catalog Item and set it to Mobile/Service Portal only:

function onLoad() {
   //Define this array to allow saving MRVS values here for accessing later
   this.mrvs = [];
}

 

Next, create 2 scripts within your Variable Set.

The first script is an onSubmit that we will use to save the values to the empty array we created above. Be sure to set it to Mobile/Service Portal only

function onSubmit() {
	//Add the values of the submitted MRVS to the array

	var fieldNames = g_form.getFieldNames();
	console.log(fieldNames);
	var mrvs_dict = {};
	for (var i = 0; i < fieldNames.length; i++)
	{
		var curFieldName = fieldNames[i];
		mrvs_dict[curFieldName] = g_form.getValue(curFieldName);
	}
	console.log(mrvs_dict);
	this.mrvs.push(mrvs_dict);
}

 

The second script is an onLoad script that will get the values from the array and populate the current form window. This example is actually for both Desktop and Mobile, but you could split it out into separate scripts if you prefer:

function onLoad() {
	var mrvs = [];
	if (window === null) {
		// Write your mobile compatible code here

		// Main Catalog Item sets this.mrvs = []
		// Then, onSubmit populates that list with the values of the MRVS
		mrvs = this.mrvs;
	} else {
		// Write your desktop compatible code here

		// Get the Multi-Row Table from the Parent Form, and update the current form
		var mrvsStr = parent.g_form.getValue("my_variable_set_name");
		mrvs = JSON.parse(mrvsStr);
	}
	if (mrvs === null || typeof mrvs === 'undefined')
	{
		// Initial load of the page attempts to run this prior to the MRVS variables getting defined.
		return;
	}
	// Get the value of a mandatory MRVS field
	// This gets used to prevent replacing all fields if one is already populated, such as in editing
	var check_field = g_form.getValue("your_field_name");
	var lastEntry = mrvs.length - 1;
	// If the checked field is blank and not editing, and a previous MRVS entry exists
	if (check_field === "" && lastEntry >= 0) {
		// Apply the latest entry values to the add new form fields as defaults
		for (var key in mrvs[lastEntry]) {
			if (key) {
				g_form.setValue(key, mrvs[lastEntry][key]);
			}
		}
	}
}

View solution in original post

10 REPLIES 10

Omkar Mone
Mega Sage

Hi 

 

I think that is not possible in the client side scripts, but in order to achieve this, let me provide you with a workaround and see if it suits you,

1) Create a Display BR which will get the values of the previous MRVS and store it in the g_scratchpad object.

2) Use this g_scratchpad object in the onLoad client script to set the values to the field.(will only work in onLoad client scripts)

 

Hope this helps

 

Regards

Omkar Mone 

Rob Weiss
Giga Contributor

Thank you for the suggestion.  Unfortunately, I was unable to get that to work for what I needed.  I couldn't even get the g_scratchpad to be defined in the script after creating display business rules for the table.  I read somewhere that business rules don't apply to catalog items?

 

Anyway, I was finally able to get this working with some help from my internal ServiceNow team.  The solution was this:

 

var mrvs = parent.g_form.getValue('my_variable_set_name');

From there, I had it parse out the JSON and then just accessed the data I need via loops.

 

Now I just need to figure out how to do the same thing within the Service Portal, but I can leave that for another thread.  This one can be closed as complete.

Hi rob, Not sure if this helps, but I was after something similar. I ended up creating a ui macro to create a button. I then replicated the same thing in a widget, then used a macro variable to hold both the ui macro for normal ui and widget for portal ui. Link to my post trying to get this to work: https://community.servicenow.com/community?id=community_question&sys_id=191c37c21b308890fff162c4bd4bcbbf&isInboxReq=true&anchor=&points= Let me know if this helps Thanks Sam

Hi rob, Not sure if this helps, but I was after something similar. I ended up creating a ui macro to create a button. I then replicated the same thing in a widget, then used a macro variable to hold both the ui macro for normal ui and widget for portal ui. Link to my post trying to get this to work: https://community.servicenow.com/community?id=community_question&sys_id=191c37c21b308890fff162c4bd4bcbbf&isInboxReq=true&anchor=&points= Let me know if this helps Thanks Sam