How can I determine if the email ID is present in the group member record?

shareef_223
Tera Contributor

I have a requirement involving two variables: "Email ID" and "Type of Access." The "Type of Access" variable is a reference field linked to the group table.

Here's the scenario: When a user enters their email ID and selects a type of access, the system needs to check if the email is already associated with any group or user records. If the email is found within the group or user records, it should trigger an alert message stating, "You are already present in the group. Please select another group." If the email is not found, the system should proceed to create a request.

Could anyone assist me with this?

6 REPLIES 6

Onkar Pandav
Tera Guru

Hi,

I had answered on one similar type of requirement. Please check below link.

https://www.servicenow.com/community/virtual-agent-forum/need-to-have-a-validation-check-for-that-fi... 

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @shareef_223 ,

Create an OnChange Client Script on a table to trigger a Script Include using GlideAjax. Validate whether the provided email ID is associated with other groups or users. Ensure that the Script Include is configured as Client Callable.

 

 

// Client Script 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var emailId = g_form.getValue('email_id');   // Change value with your field name 
    var typeOfAccess = g_form.getValue('type_of_access');  // Change value with your field name 

    if (emailId && typeOfAccess) {
        var ga = new GlideAjax('CheckEmailAndGroup');   // Script include Name 
        ga.addParam('sysparm_name', 'checkEmailAndGroup');  // Function Name 
        ga.addParam('sysparm_emailId', emailId);   // Parameter to pass 
        ga.addParam('sysparm_groupSysId', typeOfAccess); // Parameter to pass 
        ga.getXML(handleResponse); // Callback Function 
    }
}

function handleResponse(response) {
    var result = response.responseXML.documentElement.getAttribute('answer');

    if (result === 'present') {
        alert('This email id is already associated with other Group or User .');
    } else {
        // Proceed with request creation or any other action
    }
}

 

Script include : (Client callable) 

 

var CheckEmailAndGroup = Class.create();

CheckEmailAndGroup.prototype = {
    initialize: function() {},

    checkEmailAndGroup: function(emailId, groupSysId) {
        // Check if the email is associated with any group records
        var groupGr = new GlideRecord('sys_user_grmembers');
        groupGr.addQuery('email', emailId);
        groupGr.addQuery('group', groupSysId);
        groupGr.query();

        if (groupGr.hasNext()) {
            return 'present';
        }

        // Check if the email is associated with any user records
        var userGr = new GlideRecord('sys_user');
        userGr.addQuery('email', emailId);
        userGr.query();

        if (userGr.hasNext()) {
            return 'present';
        }

        return 'not_present';
    },

    type: 'CheckEmailAndGroup'
};

 

 Kindly mark correct and helpful if applicable 

Hi @Chetan Mahajan ,

Thanks for the reply, I tried this but it is not working as expected. when i select a client callable in the script include, it is showing the following systax

var CheckEmailAndGroup = Class.create();
CheckEmailAndGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    type: 'CheckEmailAndGroup'
});
 
instead of this syntax 
var CheckEmailAndGroup = Class.create();

CheckEmailAndGroup.prototype = {
    initialize: function() {},


Hello @shareef_223 ,
The Syntax you are getting is correct please go ahead with it.