How to pass If condition in UI page

kartikey
Tera Contributor

Hi Everyone,

i have passed the assignment group of a current record from client script to UI page:

var grpname = RP.getWindowProperties()["sysparm_group"];

now i want to validate it for multiple groups, but how do i do it, simply doing:

if(grpname='sysid1') - works
if(grpname='sysid1'||grpname='sysid2') - does not work


how do i check multiple if condition with OR operator?

kartikey_0-1720005644724.png



Thanks,
kartikey



3 REPLIES 3

Dnyaneshwaree
Mega Sage

Hello @kartikey,

Instead of "=" try "==" like below code:

if (grpname == 'sysid1' || grpname == 'sysid2') {
    // Your code block when grpname matches 'sysid1' or 'sysid2'
}


Please accept my solution if it works for you and thumps up to mark it as helpful.

Thank you!!

Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

Hi, 

sorry for the late response, but i tried and it does not work 😞
i'm using != but it only validates for one group if i add multiple groups using || (or condition) it does not validate.

Thanks,
Kartikey

Hi @kartikey 

 

Please try this code below 

 

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null">
  
  <g:evaluate>
    // Get assignment group name from UI Page parameter
    var grpname = RP.getWindowProperties()["sysparm_group"]; 

    // Array of valid assignment group sys_ids
    var validGroups = ['sys_id_of_group1', 'sys_id_of_group2'];  // Replace with your actual sys_ids

    // Check if the group is valid
    if (validGroups.indexOf(grpname) != -1) {
        // Valid group: Proceed with displaying tasks (or your desired action)
        var grRITM = new GlideRecord('sc_task');
        grRITM.addQuery('request_item', jelly.sysparm_request_item_id); 
        grRITM.addQuery('stage', '!=', 'closed_complete'); 
        grRITM.orderBy('number'); 
        grRITM.query();

        while(grRITM.next()){
            var taskNumber = grRITM.getValue('number');
            var shortDescription = grRITM.getValue('short_description');
            <div>${taskNumber} - ${shortDescription}</div>
        }  

    } else {
        // Invalid group: Display error message (or alternative action)
        <div class="error-message">Invalid assignment group!</div> 
    }

  </g:evaluate>
</j:jelly>

 

Key Changes and Explanations:
1. Retrieving Group Name:
* var grpname = RP.getWindowProperties()["sysparm_group"]; gets the group name (sys_id) passed as a parameter from the client script.
2. Valid Groups Array:
* var validGroups = ['sys_id_of_group1', 'sys_id_of_group2']; defines an array of the allowed group sys_ids. Replace the placeholders with the actual sys_ids of your valid assignment groups.
3. Validation with indexOf():
* if (validGroups.indexOf(grpname) != -1) checks if the grpname exists within the validGroups array:
* If the group is found, its index will be a non-negative number (>= 0), so the code inside the if block will execute (e.g., display tasks).
* If the group is not found, indexOf() returns -1, so the code inside the else block will execute (e.g., display an error message).
4. Conditional Logic:
* The rest of the script remains the same, with the task display logic moved inside the if block to ensure it only runs if the group is valid

 

Also make sure client script pass the group_sysid as a parameter 

Client Ajex Script 

 

function openUIPage() {
  var ga = new GlideAjax('YourScriptIncludeName');
  ga.addParam('sysparm_name', 'getAssignmentGroup'); // Call a Script Include to get the sys_id
  ga.getXMLAnswer(function(response) {
    var grpname = response;
    var url = 'your_ui_page.do?sysparm_request_item_id=' + g_form.getUniqueValue() + '&sysparm_group=' + grpname;
    window.open(url, '_blank');
  });
}

 

If you like this solution. Please kindly mark this as your best answer and help to contribute more to this community

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia