The CreatorCon Call for Content is officially open! Get started here.

How to declare value in if loop condition either value A or B

BanuMahalakshmi
Tera Contributor

Hi,

 

Below is the script, i need to pass the value  Group or User to function by declaring if(validateContactType(locationId, 'Authorized Requester', ['Group','User']) ). This script has to print this msg if its not Group or User.  please advise the correction in the script:


function checkifotherContTypes(locationId) {
    var msg = '';
   
    if (validateContactType(locationId, 'Authorized Requester', ['Group','User']) ) {
        msg += 'Authorized Requester must be user or group.' + '\n';
    }
    return msg;

   
}

function validateContactType(locationId, contactType, allowedTypes) {

    var gr = new GlideRecord('u_location_point_of_contact');
    gr.addQuery('u_location', locationId);
    gr.addQuery('u_contact_type', contactType);
    gr.addQuery('u_active', true);
    gr.query();

    while (gr.next()) {
        var typeValue = [];
        typeValue.push(gr.getValue('u_group_user'));
       
        for (var i = 0; i < typeValue.length; i++) {
            if (typeValue[i] != allowedTypes) {
                gs.print("inside the loop" + gr.u_location.name + typeValue[i] +allowedTypes);
                return true;
            }
        }
    }
}
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

I don't know if I'm entirely following given the custom table, but it doesn't make any logical sense to execute a for loop INSIDE of a while loop when the for loop is based on the length of an array that is initialized in the while loop.  So as written the for loop will ALWAYS run only once, for each record returned by the GlideRecord, unless and until it returns true.  Also, is the value of u_group_user actually either 'Group', 'User', etc. (case-sensitive)?  It seems like typeValue[i] will then never = ['Group,'User']. 

 

So I would say first either close the while loop before executing the for loop:

while (gr.next()) {
    var typeValue = [];
    typeValue.push(gr.getValue('u_group_user'));
}

for (var i = 0; i < typeValue.length; i++) {
    if (typeValue[i] != allowedTypes) {
        gs.print("inside the loop" + gr.u_location.name + typeValue[i] +allowedTypes);
        return true;
    }
}

or get rid of the for loop all-together if you want to evaluate against the allowedTypes during each iteration:

while (gr.next()) {
    if (gr.getValue('u_group_user' != allowedTypes) {
        gs.print("inside the loop" + gr.u_location.name + gr.getValue('u_group_user') +allowedTypes);
        return true;
    }
}

But in either case you need to change the if condition to something that evaluates against the array.  Here's one approach:

if (allowedTypes.toString().indexOf(typeValue[i]) == -1) { //u_group_user value not 'Group' or 'User'

 

Hi Brad, Thanks for reply.. I have attached log screenshot..The error message displays into location contact field itself.  i have shared the entire script - there are 4 functions - check if not user contact type, check if not group contact type, check if other contact type(contacttype must be user or group), check if groupcontact type( contact type must be user or other). my script works correctly for first two function. but it didnt works correctly rest of the two.

 

pls advise the correction in the script:

var LocationContactIssue = Class.create();
LocationContactIssue.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getlocationcontactissue: function() {
        var locationsysid = this.getParameter('sysparm_value');
        var errorMessage = '';
        var locationGR = new GlideRecord('cmn_location');
        locationGR.addQuery('u_active', true);
        if (locationsysid != '') {
            locationGR.addQuery('sys_id', '=', locationsysid);
        }
        locationGR.query();
        while (locationGR.next()) {
            var locationId = locationGR.sys_id.toString();
            
            errorMessage += this.checkifnotuserContTypes(locationId);
            errorMessage += this.checkifnotgroupContTypes(locationId);
            errorMessage += this.checkifotherContTypes(locationId);
            errorMessage += this.checkifgrpContTypes(locationId);
        }
        return errorMessage.trim();
    },
 
    checkifnotuserContTypes: function(locationId) {
        var msg = '';
        if (this.validateContactType(locationId, 'Primary Site Contact', ['User'])) {
            msg += 'Primary Site Contact type must be user.' + '\n';
        }
        if (this.validateContactType(locationId, 'Secondary Site Contact', ['User'])) {
            msg += 'Secondary Site Contact type must be user.' + '\n';
        }
      
        return msg;
    },
 
    checkifnotgroupContTypes: function(locationId) {
        var msg = '';
        if (this.validateContactType(locationId, 'Local Support', ['Group'])) {
            msg += 'Local Support type must be  group.' + '\n';
        }
        return msg;
    },
 
    checkifotherContTypes: function(locationId) {
        var msg = '';
        if (this.validateContactType(locationId, 'Authorized Requester', ['Group']) && this.validateContactType(locationId, 'Authorized Requester', ['User'])) {
            msg += 'Authorized Requester must be user or group.' + '\n';
        }
 
        if (this.validateContactType(locationId, 'Primary Network Contact', ['Group']) && this.validateContactType(locationId, 'Primary Network Contact', ['User'])) {
            msg += 'Primary Network Contact must be user or group.' + '\n';
        }
     
        return msg;
    },
 
    checkifgrpContTypes: function(locationId) {
        var msg = '';
        if (this.validateContactType(locationId, 'Primary Off-Hours Contact', 'User') && this.validateContactType(locationId, 'Primary Off-Hours Contact', 'Other')) {
            msg += 'Primary Off-Hours Contact must be user or other.' + '\n';
        }
        if (this.validateContactType(locationId, 'Secondary Off-Hours Contact', 'User') && this.validateContactType(locationId, 'Secondary Off-Hours Contact', 'Other')) {
            msg += 'Secondary Off-Hours Contact must be user or other.' + '\n';
        }
return msg;
    },
 
    validateContactType: function(locationId, contactType, allowedTypes) {
        try {
var typeValue = [];
            var gr = new GlideRecord('u_location_point_of_contact');
            gr.addQuery('u_location', locationId);
            gr.addQuery('u_contact_type', contactType);
            gr.addQuery('u_active', true);
            gr.query();
 
            while(gr.next())
{
            
typeValue.push(gr.getValue('u_group_user'));
 
}
for(var i=0; i<typeValue.length;i++)
{
            if (typeValue[i] != allowedTypes) {
                return true;
            }
}
        } catch (e) {
            gs.error("record not found");
        }
    },
 
    type: 'LocationContactIssue'
});

Brad Bowman
Kilo Patron
Kilo Patron

With this version of the script, it would be helpful to add a log line inside the for loop as the first step to show which u_group_user record is being evaluated, and the allowedTypes it is being evaluated against.  Note that with this structure, if ANY u_group_user values are not User or Group it will return true.  You are showing 5 records for this location that are a User or Group, but are there any other records for the same location?