add condition to check if CI is empty in UI macro
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 12:11 AM
Hi ,
We have created a UI Macro which is used on incident form to populate assigned to . We need to add a condition in macro if configuration item is empty then it will not filled assigned to and give error message that please select configuration item first.
UI Macro code:-
<?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_n" value="add_my_group_${ref}"/>
<span id="${jvar_n}" onclick="addMyGroup('${ref}')" title="Add my group" alt="Add my group" tabindex="0" class="btn btn-default icon-user-group">
<span class="sr-only">Add my group</span>
</span>
<script>
function addMyGroup(reference) {
var groupID = '';
//Get the group reference field and populate with Helpdesk T1 group
var rec = new GlideRecord('sys_properties');
rec.addQuery('name', 'helpdesk.t1');
rec.query();
if(rec.next()){
groupID = rec.value;
}
var s = reference.split('.');
var referenceField = s[1];
var grpRec = new GlideRecord('sys_user_grmember');
grpRec.addQuery('user', '$[gs.getUserID()]');
grpRec.addQuery('group', groupID);
grpRec.query(groupCallback);
//After the server returns the query recordset, continue here
function groupCallback(grpRec){
//Hide any previous field messages
try{
g_form.hideFieldMsg(referenceField, true);
}catch(e){}
//Check to see if the user is a member of a single group
if(grpRec.next()){
//Populate Assignment group and Assigned to fields
g_form.setValue(referenceField, grpRec.group);
g_form.setValue('assigned_to', '$[gs.getUserID()]');
}
else{
g_form.showFieldMsg(referenceField,'You are not a member of the ******** group. Please select a group manually.','error');
}
}
}
</script>
</j:jelly>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 04:57 AM
cmdb_ci field is on incident table not on user group member ship table. how system know that to which table we refer to if we directly get value ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 05:15 AM
That's what g_form.getValue does - gets the value of the field on the current form - the same way g_form.setValue knows which table to populate assigned_to,...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 08:15 AM
function addMyGroup(reference) {
var groupID = '';
//Get the group reference field and populate with Helpdesk T1 group
var rec = new GlideRecord('sys_properties');
rec.addQuery('name', 'helpdesk.t1');
rec.query();
if(rec.next()){
groupID = rec.value;
}
var s = reference.split('.');
var referenceField = s[1];
if(g_form.getValue('cmdb_ci')=='')
{
alert("if");
g_form.showFieldMsg(referenceField,'Please enter correct configurationn item before selecting Assigned to','error');
}
else{
alert("else");
var grpRec = new GlideRecord('sys_user_grmember');
grpRec.addQuery('user', '$[gs.getUserID()]');
grpRec.addQuery('group', groupID);
grpRec.query(groupCallback);
//After the server returns the query recordset, continue here
function groupCallback(grpRec){
//Hide any previous field messages
try{
g_form.hideFieldMsg(referenceField, true);
}catch(e){}
******
I have added the if condition as above mentioned in the code but issue is now if i enter value in configuration item and without saving if i click on UI macro button then it is going into else and execute code. Without saving, it should not go to else. Assigned to only be filled when configuration item is selected and saved on the form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 09:55 AM
Alternatively, clear the Assigned to if the cmdb_ci changes to blank with a UI Policy or Client Script. One way to meet this requirement in the same script is to add another GR in place of
if (g_form.getValue('cmdb_ci')=='')
function addMyGroup(reference) {
var groupID = '';
//Get the group reference field and populate with Helpdesk T1 group
var rec = new GlideRecord('sys_properties');
rec.addQuery('name', 'helpdesk.t1');
rec.query();
if(rec.next()){
groupID = rec.value;
}
var s = reference.split('.');
var referenceField = s[1];
var inc = new GlideRecord('incident');
if (inc.get(g_form.getUniqueValue()) {
if (inc.cmdb_ci == '') {
alert("if");
g_form.showFieldMsg(referenceField,'Please enter correct configurationn item before selecting Assigned to','error');
} else {
alert("else");
var grpRec = new GlideRecord('sys_user_grmember');
grpRec.addQuery('user', '$[gs.getUserID()]');
grpRec.addQuery('group', groupID);
grpRec.query(groupCallback);
//After the server returns the query recordset, continue here
function groupCallback(grpRec){
//Hide any previous field messages
try{
g_form.hideFieldMsg(referenceField, true);
}catch(e){}
}