GlideModalForm Set a Field Value on the form

Matthew Knight
Tera Guru

I have created a UI action to display a GlideModalForm once clicked. This all works as expected but when I try to use either the addParm or setPreference options to preset a field to certain value it continues to remain blank.

I have included my script below:

MatthewKnight_0-1700822716987.png

 

The Modal is displayed but the description field remains empty

MatthewKnight_1-1700822767558.png

Appreciate any assistance here as the addParm or setPreference options are not working to achieve this.

1 ACCEPTED SOLUTION

Totally my bad, I misunderstood the requirements.

What you need has nothing to do with any UI Page or UI Macro.

What you need is to use parameter named sysparm_query; e.g:

var gmf = new GlideModalForm('Create Purchase Order', 'x_icaew_work_items_purchase_order');
gmf.setPreference('sysparm_query', 'description=Some description');
gmf.setPreference('sys_id', '-1');
gmf.render();

Basically that is the parameter that forms will look at to set initial values.

 

I'm assuming x_icaew_work_items_purchase_order is the name of the table containing Purchase Order records that you want to create.

View solution in original post

29 REPLIES 29

Hi @Matthew Knight ,

 

I wonder if we should be starting a separate thread LOL

The modal is generating from alm_hardware table, unless you mean where is the UI Action generating from then, that is the sc_task table.

 

var af= new GlideModalForm('Update Asset','alm_hardware');
...
af.render();

 

Here is the full details (screen shot) of my UI Action:

JGerrity_0-1732114393493.pngJGerrity_1-1732114440220.png

 

Cheers!

 

 

-Z-
Tera Contributor

Wrong account

@JGerrityyour case is different from the one described in the original post. You want to load an existing record and modify it.

I can see two ways to do it:

1. add some custom URL parameter and intercept it in a display business rule and apply modifications to the loaded record

2. add to your script (of course setPreference is not needed) something like:

 

 

var aTag = '...'
var af = new GlideModalForm('Update Asset JG', 'alm_asset');

af.setSysID(aTag);
af.addParm('sysparm_form_only', 'true');
af.setOnloadCallback(onAssetModalLoadCallback());
af.render();

function onAssetModalLoadCallback() {
    return function() {
        var childGForm = window.frames['dialog_frame'].g_form;
        childGForm.setValue('install_status', '1');
    };
}

 

 

Of course through childGForm you can interact with the loaded form the same as you would interact with any form through g_form.

JGerrity
Tera Contributor

Hi @-O- ,

 

Thanks for jumping in here.


That's a creative approach with childGForm but unfortunately, that doesn't seem to work either.

Here is my code:

 

 

function scanHardware(){
	var currentUser = g_form.getValue('sc_task.request_item.requested_for');	
	var gm = new GlideModal("glide_prompt", true, 600);
	gm.setTitle("Scan Hardware");
	gm.setPreference("title", "Asset Tag:");
	gm.setPreference("onPromptComplete", function(value) {
	if (value!=""){
		var gr = new GlideRecord('alm_asset');
		gr.addQuery('asset_tag', value);
		gr.query();
		if (gr.next()) {
			var aTag = gr.sys_id; 
			}
			alert(currentUser+"AssetTag="+aTag);
		var af= new GlideModalForm('Update Asset','alm_asset');
		af.setSysID(aTag);
		af.addParm('sysparm_form_only', 'true');
		af.setOnloadCallback(onAssetModalLoadCallback());
		af.render();
	}else{
		alert("you didn't enter a value!");
	}
	});
	gm.setPreference("onPromptCancel", function(value) {alert("You clicked on 'Cancel', value was: "+value)});
gm.render();

}
function onAssetModalLoadCallback() {
    return function() {
        var childGForm = window.frames['dialog_frame'].g_form;
        childGForm.setValue('install_status', '1');
		childGForm.setValue('comments', 'Hi Jason');
    };
}

 

But the record loaded into the modal isn't getting modified:

JGerrity_0-1732126884419.png

Thanks again everyone for your help!

J.

You may need to replace window with top or self. I'm suspecting the code is executed in a "protected"/isolated context.

Where does all this code reside?

For me, when executed form the browser console, the code works as expected.