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

KrishnaMohan
Giga Sage

Hi @BanuMahalakshmi 
Not sure about requirement, one observation  in checkifnotgroupContTypes method
you missed this keyword while calling validateConactType Method  in if statement.

    checkifnotgroupContTypes: function(locationId) {
        var msg = '';
    if (this.validateContactType(locationId, 'Local Support', ['Group'])) {
        msg += 'Local Support type must be group.' + '\n';
    }
        return msg;
    },

 Note:  this keyword refers to the object that is currently executing the code. Make sure to add this when we calling methods which are existing in same script include.

If this helped to answer your query, please mark it helpful & accept the solution.
Thanks!
Krishnamohan

Ankur Bawiskar
Tera Patron
Tera Patron

@BanuMahalakshmi 

changes below

1) you should use this operator to call the method, you were missing that

checkifnotgroupContTypes: function(locationId) {
        var msg = '';
        if (this.validateContactType(locationId, 'Local Support', ['Group'])) {
            msg += 'Local Support type must be group.' + '\n';
        }
        return msg;
    },

2) update this method validateContactType

	validateContactType: function(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();

    var recordExists = false;
    while(gr.next()) {
        recordExists = true;
        var typeValue = gr.getValue('u_group_user'); // 'User', 'Group', etc.
        if (!allowedTypes.includes(typeValue)) {
            return false; // Invalid type found
        }
    }
    return recordExists ? true : true; // If no records, treat as valid (no error)
},

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks for reply, i tried recordExist but it didnt works. 😞

Rafael Batistot
Kilo Patron

Hi @BanuMahalakshmi 

 

The problem is in your function checkifnotgroupContTypes.

 

Right now you have:


checkifnotgroupContTypes: function(locationId) {
var msg = '';
if (validateContactType(locationId, 'Local Support', ['Group'])) {
msg += 'Local Support type must be group.' + '\n';
}
return msg;
},

 

 

Issues:

  1. You forgot the this. prefix → It should be this.validateContactType(...), not just validateContactType(...).
    Without this., it is calling nothing (or global scope), and behaves unexpectedly.
  2. Your validation logic is inverted
    Right now, you are adding the error message if validation passes.
    But the intention is to add the error message if the validation fails (same as your other checks).

Corrections

 

checkifnotgroupContTypes: function(locationId) {
var msg = '';
if (!this.validateContactType(locationId, 'Local Support', ['Group'])) {
msg += 'Local Support type must be group.' + '\n';
}
return msg;
},

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.