Onchange client script help

EricG
Kilo Sage

I'm attempting to add a "Pop UP" window that will generate a prepopulated Outage record we a field changes value.

 

So i have field u_maintenance_type you can select Major or Minor.

 

On the Onchange client script i have

 if(newValue=='major'){
	//g_form.save();
	openNewWin();
	}

 

This is calling my UI Action

function openNewWin(){
	
	var tyP = g_form.getValue('u_maintenance_type');
	if(tyP=='major'){
		tyP = 'planned';
	} else {
		tyP = '';
	}
		var bDate = g_form.getValue('start_date');
		var eDate = g_form.getValue('end_date');
		var mCI = g_form.getValue("cmdb_ci");
	
    	var sysID = g_form.getUniqueValue(); // get the sys_id value from the change request
        //Create and open the dialog form
        var dialog = new GlideModalForm('Outage Task','cmdb_ci_outage');//was GlideModalForm
        dialog.setSize("modal-lg",300);
        dialog.setPreference('sys_id','-1'); //-1 to get create new record
        var q = 'short_description='+g_form.getValue("short_description") + "^cmdb_ci="+ mCI +"^begin=" + bDate +"^end=" + eDate + "^type=" + tyP +'^task_number='+sysID;
		
		dialog.setPreference('sysparm_query',q); //copy the change sys_id to the parent field
        
        //Fuction called on feedback submit, to add success message and refresh Knowledge Feedback Task related list
        dialog.setCompletionCallback(handleAdapterCreatedOrUpdated);
		dialog.render();
}
function handleAdapterCreatedOrUpdated(test,sys_id,table,displayValue){
	location.reload();	
	
}

 

The issue i'm having is that when you click the Save button on the pop-up, you get the following message.

EricG_0-1737986552167.png

Reload - will remove the Onchange value in the field and reload the form. I get the new outage, but Maintenance Type reverts to old value and rated lists show the new outage.

Cancel - closes the pop-up but the outage doesn't show in related lists until you save the form.

 

I've looked at this way to many times and I know i'm missing the obvious.

 

How can i get the the form to save prior to the  popup opening?

Or save it  when you "Save" the Outage record.

1 ACCEPTED SOLUTION

I figured it out.

 

I added the save function to my Create outage UI action.

It saved and reloaded the Change form as expected.

Reload was important to show Outage in the related lists.

 Updated last lines as follows.

 

function handleAdapterCreatedOrUpdated(test,sys_id,table,displayValue){
    //location.reload();
    mySave();
}

function mySave(){
    g_form.save();
}

View solution in original post

3 REPLIES 3

-O-
Kilo Patron
Kilo Patron

If you don't want the browser window to be reloaded, why are you writing code to reload it?

I figured it out.

 

I added the save function to my Create outage UI action.

It saved and reloaded the Change form as expected.

Reload was important to show Outage in the related lists.

 Updated last lines as follows.

 

function handleAdapterCreatedOrUpdated(test,sys_id,table,displayValue){
    //location.reload();
    mySave();
}

function mySave(){
    g_form.save();
}

There is an API to reload just a related list, you know.

In UI 16 there is a global object named GlideLists2 that has GlideList2 objects attached to it as properties - one for each related list on the current form.

So in order to refresh the list of child incidents (for instance) on an incident form, without all the totally unnecessary reloads, one might just write:

GlideLists2['incident.incident.parent_incident'].refresh();

Simple as that.