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 12:54 AM
Hi @Kunal33 ,
You can use evaluate tag to check teh value from CI field and then use can use it in the script where ever you needed. Please fine below code for your refernce:
<g2:evaluate var="jvar_ci_value” object="true">
var ci_value = current.cmdb_ci;
gs.info('Value is: ' +ci_value);
ci_value;
</g2:evaluate>
//check variable value in condition for further execultion
if($[jvar_ci_value] == null && $[jvar_ci_value] == ‘’)
{
g_form.showFieldMsg(jvar_ci_value,’Print Message’,’error');
}
If I am able to help you with your question, Please click the Thumb Icon and mark as Correct.
Regards,
Pushkar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 05:00 AM
Can you align this code in the above code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2023 06:35 AM
I don't have full environment to test this code 😀. But check adding below code to your instance and let me know if it is working for you. Highlighted in Blue is the code I modified.
Also check what value you are getting in the variable [ciValue]. Because in IF condition we are checking if that value is null or empty.
Let me know what is the result.
======= Code Starts here ===
<?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>
<g2:evaluate var="jvar_ci_value” object="true">
var ci_value = current.cmdb_ci;
gs.info('Value is: ' +ci_value);
ci_value;
</g2:evaluate>
<script>
function addMyGroup(reference) {
var groupID = '';
var ciValue = $[jvar_ci_value] ;
//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){}
if(ciValue == null && ciValue == ‘’)
{
g_form.showFieldMsg(current.cmdb_ci,’Print Message’,’error');
}
else
{
//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:34 AM
You can just add an if block to the grpRec section like this - before or after it populates Assignment group and/or Assigned to:
if(grpRec.next()){
//Populate Assignment group and Assigned to fields
if (g_form.getValue('cmdb_ci') == '') {
g_form.showFieldMsg(referenceField,'Please select a Configuration Item before assigning.','error');
} else {
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');
}