Please advise the correction in the script.

BanuMahalakshmi
Tera Contributor

Hi,

Could you please advise is this possible to consolidate both function 'val_otherconttypes' and 'val_ContType'. 

I have tried to merge both function but it didnt work. please advise. Thanks.
function checkifnotuser(locationId) {
    var msg = '';
    if (!val_ContType(locationId, 'Primary Site', ['User'])) {
        msg += 'Primary Site must be user.'+'\n'}
    if (!val_ContType(locationId, 'Secondary Site', ['User'])) {
        msg += 'Secondary Site must be user.'+'\n';} }
 
function checkother(locationId) {
    var msg = '';
    if (!val_otherconttypes(locationId, 'Primary NW', ['Other'])) {
        msg += 'Primary NW must be user or group.'+'\n';
    }
    if (!val_otherconttypes(locationId, 'Secondary NW', ['Other'])) {
        msg += 'Secondary NW must be user or group.'+'\n';
    }
}

function val_otherconttypes(locationId, contactType, allowedTypes) {
    var gr = new GlideRecord('u_location_contact');
    gr.addQuery('u_location', locationId);
    gr.addQuery('u_contact_type', contactType);
    gr.query();
    while (gr.next()) {
        var typeValue = gr.getValue('u_group_user');
        if (allowedTypes.includes(typeValue)) {
             return false;
        }
    }
    return true;
}

function val_ContType(locationId, contactType, allowedTypes) {
    var gr = new GlideRecord('u_location_contact');
    gr.addQuery('u_location', locationId);
    gr.addQuery('u_contact_type', contactType);
    gr.query();
    while (gr.next()) {
        var typeValue = gr.getValue('u_group_user');
        if (!allowedTypes.includes(typeValue)) {
               return false;
        }
    }
    return true;
}
1 ACCEPTED SOLUTION

J Siva
Tera Sage

Hi @BanuMahalakshmi 
Try the below function. It'll work.

function val_ContType(locationId, contactType, allowedTypes) {
    var type = allowedTypes[0];
    var gr = new GlideRecord('u_location_contact');
    gr.addQuery('u_location', locationId);
    gr.addQuery('u_contact_type', contactType);
    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;
            }
        }

    }
    return true;
}

 

Regards,

Siva

View solution in original post

1 REPLY 1

J Siva
Tera Sage

Hi @BanuMahalakshmi 
Try the below function. It'll work.

function val_ContType(locationId, contactType, allowedTypes) {
    var type = allowedTypes[0];
    var gr = new GlideRecord('u_location_contact');
    gr.addQuery('u_location', locationId);
    gr.addQuery('u_contact_type', contactType);
    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;
            }
        }

    }
    return true;
}

 

Regards,

Siva