How to update form fields from UI Page Client script?

MBarrott
Mega Sage

I have a pop-up window which gives the user two options; Cancel and OK. 

 

If they hit cancel I want to simply change the value of a the type field and destroy the GlideDialogWindow. 

 

If they hit OK it will close out the demand record and set all fields to non-mandatory. 

 

I'm pulling over the sys_id for the respective demand but how do you pass back or set form fields from the UI Page?

 

I attempted with a GlideRecord but it didn't seem to be functioning correctly. 

 

Any help would be appreciated. 

 

onChange Client Script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Type appropriate comment here, and begin script below
	if(g_form.getValue('type') == 'no_conversion')
	{
		var dialog = new GlideDialogWindow("no_conversion_popup");
		dialog.setSize(600,600);
		dialog.setTitle("No Conversion Selected"); 
		dialog.setPreference('sys_id', g_form.getUniqueValue()); //pass current object id
		dialog.render(); //Open the dialog
	}
   
}

 

 

UI Page Script:

function continueOK() {
	alert("OK clicked");
	GlideDialogWindow.get().destroy();
}
function continueCancel() {
	var gdw = GlideDialogWindow.get(); // attempting to get the sys_id value
	var sys_id = gdw.getPreference('sys_id'); // attempting to get the sys_id value
	alert("Cancel clicked - sys id is: " + sys_id);
	
	var gr = new GlideRecord('dmn_demand');
	gr.addQuery('sys_id', sys_id);
	gr.query();
	
	var rc = gr.getRowCount();
	alert('rows found is: ' + rc);
	
	GlideDialogWindow.get().destroy();
}

 

 

1 ACCEPTED SOLUTION

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @MBarrott 

To achieve your requirements, you need to pass data back and forth between the UI Page and the client script using GlideDialogWindow preferences and then handle the logic accordingly. Here's how you can modify your code:
Client Script: 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Type appropriate comment here, and begin script below
    if (g_form.getValue('type') == 'no_conversion') {
        var dialog = new GlideDialogWindow("no_conversion_popup");
        dialog.setSize(600, 600);
        dialog.setTitle("No Conversion Selected");
        dialog.setPreference('sys_id', g_form.getUniqueValue()); // Pass current object id
        dialog.render(); // Open the dialog
    }
}

 

 

UI Page Script:

 

function continueOK() {
    var sys_id = g_dialog.getWindow().getPreference('sys_id'); 
    alert("OK clicked - sys id is: " + sys_id);

    var gr = new GlideRecord('dmn_demand');
    if (gr.get(sys_id)) {

        gr.setMandatory(false);

        gr.setValue('state', 'Closed'); 
        gr.update();
    }

    g_dialog.getWindow().destroy(); 
}

function continueCancel() {
    var sys_id = g_dialog.getWindow().getPreference('sys_id'); 
    alert("Cancel clicked - sys id is: " + sys_id);


    var gr = new GlideRecord('dmn_demand');
    if (gr.get(sys_id)) {
        gr.setValue('type', 'other_value'); 
        gr.update();
    }

    g_dialog.getWindow().destroy(); // Close the GlideDialogWindow
}

 

 

In the UI Page script:

  • g_dialog.getWindow().getPreference('sys_id') is used to retrieve the sys_id value passed from the client script.
  • In the continueOK() function, the demand record with the retrieved sys_id is updated to set all fields to non-mandatory and close out the demand record.
  • In the continueCancel() function, the demand record with the retrieved sys_id is updated to change the value of the type field.

Make sure to replace 'dmn_demand' with the correct table name according to your configuration. Additionally, adjust the logic in the continueOK() and continueCancel() functions as per your specific requirements.

 

Mark as helpful & Accepted Solution !!

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

View solution in original post

2 REPLIES 2

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @MBarrott 

To achieve your requirements, you need to pass data back and forth between the UI Page and the client script using GlideDialogWindow preferences and then handle the logic accordingly. Here's how you can modify your code:
Client Script: 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Type appropriate comment here, and begin script below
    if (g_form.getValue('type') == 'no_conversion') {
        var dialog = new GlideDialogWindow("no_conversion_popup");
        dialog.setSize(600, 600);
        dialog.setTitle("No Conversion Selected");
        dialog.setPreference('sys_id', g_form.getUniqueValue()); // Pass current object id
        dialog.render(); // Open the dialog
    }
}

 

 

UI Page Script:

 

function continueOK() {
    var sys_id = g_dialog.getWindow().getPreference('sys_id'); 
    alert("OK clicked - sys id is: " + sys_id);

    var gr = new GlideRecord('dmn_demand');
    if (gr.get(sys_id)) {

        gr.setMandatory(false);

        gr.setValue('state', 'Closed'); 
        gr.update();
    }

    g_dialog.getWindow().destroy(); 
}

function continueCancel() {
    var sys_id = g_dialog.getWindow().getPreference('sys_id'); 
    alert("Cancel clicked - sys id is: " + sys_id);


    var gr = new GlideRecord('dmn_demand');
    if (gr.get(sys_id)) {
        gr.setValue('type', 'other_value'); 
        gr.update();
    }

    g_dialog.getWindow().destroy(); // Close the GlideDialogWindow
}

 

 

In the UI Page script:

  • g_dialog.getWindow().getPreference('sys_id') is used to retrieve the sys_id value passed from the client script.
  • In the continueOK() function, the demand record with the retrieved sys_id is updated to set all fields to non-mandatory and close out the demand record.
  • In the continueCancel() function, the demand record with the retrieved sys_id is updated to change the value of the type field.

Make sure to replace 'dmn_demand' with the correct table name according to your configuration. Additionally, adjust the logic in the continueOK() and continueCancel() functions as per your specific requirements.

 

Mark as helpful & Accepted Solution !!

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

MBarrott
Mega Sage

Hi @Sohithanjan G 

 

I was actually able to get it working shortly after this your script is very helpful. Thank you for the help!