The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Alerting in UI’s page client script

EH Desev
Tera Contributor

Hello experts,

 

I am trying to alert in UI’s page client script and the message to be displayed on the UI form but the script is not working for some reason.

 

I want a message to be displayed on below form when the user tries to press OK without filling the reference fields (Select a Parent and Select  Type of Relationship). 

UI form.PNG

 

Below is my UI Page script:

 

HTML section:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	 <j:set var="jvar_ObjList" value="${sysparm_checkedRecord}"/>
	<g:ui_form id="f_id">
	<center>	
	<table>
	<tr>
	<td>Select a Parent: </td>
    <td><g:ui_reference name="nameCi" table = "cmdb_ci_server" query="sys_idNOT IN${sysparm_checkedRecord}"/> </td>
	</tr>
	<tr style="height: 25px;"/>
	<tr>
	<td>Select Type of Relationship:  </td>
    <td><g:ui_reference name = "nameRel" table = "cmdb_rel_type"/> </td>
	</tr>		

	<tr style="height: 25px;"/>
	<tr>
		<td>Click OK to create these relationships</td>
		</tr>
		<tr>
         <td align="right" colspan="2">
            <br />
			 <input type="hidden" id="childCis" name="childCis" value="${sysparm_checkedRecord}"/>
		<g:dialog_buttons_ok_cancel ok="return validate()" />

         </td>
	</tr>	
		
	</table>
			
		</center>
		 </g:ui_form>
</j:jelly>

 

 Client script section:

 

function validate() {

    if (nameCi == '') {

        alert('Please fill in Parent field');
    } 
	
	else if (nameRel == '') {
        alert('Please fill in Type of Relationship field');

    }


    GlideDialogWindow.get().destroy();
    return true;
}

 

 Thanks!

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @EH Desev ,

 

the issue might be related to how you are trying to access the field values in your validation function. In Jelly, you should use '${}' syntax to reference variables, but it seems you are trying to access `nameCi` and `nameRel` directly. 

 

Here's how you can modify your validation function:

 

function validate() {

    var nameCiValue = '${nameCi}'; // Use ${} to get the value of the Jelly variable

    var nameRelValue = '${nameRel}';

 

    if (nameCiValue == '') {

        alert('Please fill in Parent field');

        return false;

    } else if (nameRelValue == '') {

        alert('Please fill in Type of Relationship field');

        return false;

    }

 

    GlideDialogWindow.get().destroy();

    return true;

}

 

 

Make sure to use '${}' to reference the Jelly variables (`nameCi` and `nameRel`). Also, I added `return false;` after each `alert` statement to prevent the form submission when validation fails.

 

If you still face issues, consider checking your browser's console for any JavaScript errors, which can provide more information about the problem.

 

Thanks,

Danish

 

View solution in original post

7 REPLIES 7

Maik Skoddow
Tera Patron
Tera Patron

Hi @EH Desev 

you write " but the script is not working for some reason." 

--> What exactly is not working? Please describe the observed behavior.

Maik

Hi Maik,

 

It is not displaying the alert with the message.

 

Best regards,

Danish Bhairag2
Tera Sage
Tera Sage

Hi @EH Desev ,

 

the issue might be related to how you are trying to access the field values in your validation function. In Jelly, you should use '${}' syntax to reference variables, but it seems you are trying to access `nameCi` and `nameRel` directly. 

 

Here's how you can modify your validation function:

 

function validate() {

    var nameCiValue = '${nameCi}'; // Use ${} to get the value of the Jelly variable

    var nameRelValue = '${nameRel}';

 

    if (nameCiValue == '') {

        alert('Please fill in Parent field');

        return false;

    } else if (nameRelValue == '') {

        alert('Please fill in Type of Relationship field');

        return false;

    }

 

    GlideDialogWindow.get().destroy();

    return true;

}

 

 

Make sure to use '${}' to reference the Jelly variables (`nameCi` and `nameRel`). Also, I added `return false;` after each `alert` statement to prevent the form submission when validation fails.

 

If you still face issues, consider checking your browser's console for any JavaScript errors, which can provide more information about the problem.

 

Thanks,

Danish

 

Thank you very much, Danish! Changed the code and that displayed the alert.