Pre-populate a condition field in a new record

Max Nowak
Kilo Sage

Hi,

 

I need to pre-populate a condition type field on a new form. For context, the field is on a custom table, and users should be able to create copies of the records stored in this custom table. This copy shall not be inserted immediatley (so, just copying the record on the server side and using gr.insert() is not an option), users must have the chance to change fields and validate the content before saving.

 

Now, I've tried using the URL (sys_id=-1&sysparm_query=...) to populate the fields on the new record with the values from the old record, but this stopped working once I got to the condition field, because the condition is saved as encoded query, and ServiceNow is apparently unable to parse that query, even when it's URL-encoded and using "%3D" instead of "=", and "%5E" instead of "^", like this:

 

https://instance.service-now.com/x_custom_table.do?sys_id =-1&sysparm_query=condition_field=condition1%3Dresult1%5Econdition2%3Dresult2

 

This only adds the first condition to the condition field, and ignores any condition after that.

 

So, since that didn't work, I tried parsing the URL parameters in an onLoad client script, and the using the parsed value to update the condition field with g_form.setValue().

This only works to an extent, I am able to parse the condition from the parameters, but g_form.setValue does not update the form. If I save the record, the conditions are shown, but before that, it's just an empty condition field.

 

So, I guess, my question is, does anyone know how to update a condition field via client script in a way that change is instantly displayed, or, if that's not possible, is there another way to pre-fill condition fields on new records?

 

Thanks,

Max

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

Hi Max,

You can use action.openGlideRecord to open either a new record (using an instantiated GlideRecord) or an existing record.

 

For example, take the following UI action script

(function(gs, action, current){

	var newRecordGR = new GlideRecord(current.getTableName()); //create a new empty GR
	
	var elements = current.getElements(); //get all the elements of the existing record
	var elSize = elements.length; //use .size() in global

	for (var i = 0; i < elSize; i++){

		newRecordGR.setValue( elements[i].getName() , elements[i].toString()); //copy the values over

	}

	action.openGlideRecord(newRecordGR) //open the intialised but unsaved record


})(gs, action, current)

msedge_UtGGLUAn9W.gif

View solution in original post

3 REPLIES 3

Kieran Anson
Kilo Patron

Hi Max,

You can use action.openGlideRecord to open either a new record (using an instantiated GlideRecord) or an existing record.

 

For example, take the following UI action script

(function(gs, action, current){

	var newRecordGR = new GlideRecord(current.getTableName()); //create a new empty GR
	
	var elements = current.getElements(); //get all the elements of the existing record
	var elSize = elements.length; //use .size() in global

	for (var i = 0; i < elSize; i++){

		newRecordGR.setValue( elements[i].getName() , elements[i].toString()); //copy the values over

	}

	action.openGlideRecord(newRecordGR) //open the intialised but unsaved record


})(gs, action, current)

msedge_UtGGLUAn9W.gif

Hi Kieran,

 

That works, thank you! That's a way better solution than what I had planned!

Glad that worked and saved you some future pain 🙂