Pls let me know the issue in the script

BanuMahalakshmi
Tera Contributor

Hi

Please let me know the correction in the script, this script executes fine except the scenario - this script prints the value "Local Support type must be user group." still though there is no record exist in the Point of contact table for the relevant location . please advise the suggestion 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.checkContactCounts(locationId);
            errorMessage += this.checkifnotuserContTypes(locationId);
            errorMessage += this.checkifnotgroupContTypes(locationId);
            errorMessage += this.checkotherContactTypes(locationId);
            errorMessage += this.checkgrpContactTypes(locationId); 
        }
        return errorMessage.trim();
    },
 
    checkContactCounts: function(locationId) {
        var message = '';
        var primaryContactCount = this.getContactCount(locationId, 'Primary Site Contact');
        if (primaryContactCount == 0) {
            message += 'Primary Site Contact is missing.' + '\n';
        } else if (primaryContactCount > 1) {
            message += 'Can have only one Primary Site Contact.' + '\n';
        }
 
        var secondaryContactCount = this.getContactCount(locationId, 'Secondary Site Contact');
        if (secondaryContactCount == 0) {
            message += 'Secondary Site Contact is missing.' + '\n';
        }
 
        var BUITcontact = this.getContactCount(locationId, 'BU IT Contact');
        if (BUITcontact == 0) {
            message += 'BU IT Contact is missing.' + '\n';
        } else if (BUITcontact > 1) {
            message += 'Can have only one BU IT contact.' + '\n';
        }
 
    
        return message;
    },
 
    getContactCount: function(locationId, contactType) {
        var contactGR = new GlideAggregate('u_location_point_of_contact');
        contactGR.addQuery('u_location', locationId);
        contactGR.addQuery('u_contact_type', contactType);
        contactGR.addQuery('u_active', true);
        contactGR.addAggregate('COUNT');
        contactGR.query();
        while (contactGR.next()) {
            return contactGR.getAggregate('COUNT');
        }
    },
 
    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 (validateContactType(locationId, 'Local Support', ['Group'])) {
        msg += 'Local Support type must be group.' + '\n';
    }
        return msg;
    },
 
    checkotherContactTypes: function(locationId) {
        var msg = '';
        if (!this.validateContactType(locationId, 'Primary Network Contact', ['Other'])) {
            msg += 'Primary Network Contact must be user or group.' + '\n';
        }
        if (!this.validateContactType(locationId, 'Secondary Network Contact', ['Other'])) {
            msg += 'Secondary Network Contact must be user or group.' + '\n';
        }
      
        return msg;
    },
 
    checkgrpContactTypes: function(locationId) {
        var msg = '';
        if (!this.validateContactType(locationId, 'Primary Off-Hours Contact', ['Group'])) {
            msg += 'Primary Off-Hours Contact must be user or other.' + '\n';
        }
        if (!this.validateContactType(locationId, 'Secondary Off-Hours Contact', ['Group'])) {
            msg += 'Secondary Off-Hours Contact must be user or other.' + '\n';
        }
   
        return msg;
    },
 
    validateContactType: function(locationId, contactType, allowedTypes) {
        var type = allowedTypes[0];
        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 = gr.getValue('u_group_user');
            if (type == "User") {
                if (!allowedTypes.includes(typeValue)) {
                    return false;
                }
            }
            if (type == "Other") {
                if (allowedTypes.includes(typeValue)) {
                    return false;
                }
            }
            if (type == "Group") {
                if (allowedTypes.includes(typeValue)) {
                    return false;
                }
            }
        }
        return true;
 
 
    },
 
    
});
7 REPLIES 7

 

Thanks for reply, i have tried the way which u suggested. but still the value "local support type must be the group". i have tried "try & catch"  also and changed with group value user or other also but it didnt works. pls asssist me.

 

Tested Script:

 

  checkifnotgroupContTypes: function(locationId) {
        var msg = '';
\\if (validateContactType(locationId, 'Local Support', ['Group'])) { // tried this way also

        if (this.validateContactType(locationId, 'Local Support', ['User'])) {
            msg += 'Local Support type must be group.' + '\n';
        }
        if (!this.validateContactType(locationId, 'Local Support', ['Other'])) {
            msg += 'Local Support type must be group.' + '\n';
        }
        return msg;
    },
 
 validateContactType: function(locationId, contactType, allowedTypes) {
        try
        {
               var type = allowedTypes[0];
        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();
        if(!gr.read())
        {
            return;
        }
        else{
            var typeValue = gr.getValue('u_group_user');
            if (type == "User") {
                if (!allowedTypes.includes(typeValue)) {
                    return false;
                }
            }
            if (type == "Other") {
                if (allowedTypes.includes(typeValue)) {
                    return false;
                }
            }
            if (type == "Group") {
                if (allowedTypes.includes(typeValue)) {
                    return false;
                }
            }
        }
        return true;
        }
        catch (e) {
        gs.error("record not found");
    }
    },

Hi @BanuMahalakshmi 

thanks for sharing your code.
The problem is in your validateContactType logic: right now it always ends up returning true or behaving incorrectly because the way you are checking User / Group / Other is inconsistent.

Let’s simplify it.

checkifnotgroupContTypes: function(locationId) {
    var msg = '';

    if (!this.validateContactType(locationId, 'Local Support', 'Group')) {
        msg += 'Local Support type must be group.\n';
    }

    return msg;
},

validateContactType: function(locationId, contactType, requiredType) {
    try {
        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();

        if (!gr.next()) {
            // no record found
            return false;
        }

        var typeValue = gr.getValue('u_group_user'); // value stored: "User", "Group" or "Other"

        // Just check if the typeValue matches requiredType
        return typeValue == requiredType;

    } catch (e) {
        gs.error("validateContactType error: " + e.message);
        return false;
    }
}

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

@BanuMahalakshmi 

Thanks for sharing your code.
The problem is in your validateContactType logic: right now it always ends up returning true or behaving incorrectly because the way you are checking User / Group / Other is inconsistent.

Let’s simplify it.

checkifnotgroupContTypes: function(locationId) {
    var msg = '';

    if (!this.validateContactType(locationId, 'Local Support', 'Group')) {
        msg += 'Local Support type must be group.\n';
    }

    return msg;
},

validateContactType: function(locationId, contactType, requiredType) {
    try {
        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();

        if (!gr.next()) {
            // no record found
            return false;
        }

        var typeValue = gr.getValue('u_group_user'); // value stored: "User", "Group" or "Other"

        // Just check if the typeValue matches requiredType
        return typeValue == requiredType;

    } catch (e) {
        gs.error("validateContactType error: " + e.message);
        return false;
    }
}
If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.