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

shareef2
Tera Contributor

Hello Everyone,

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?

Thank you,

Shareef

2 ACCEPTED SOLUTIONS

Appanna M
Tera Guru

Hello @shareef2 ,

 

You can write an onChange Client script on the field Type of Access and making call using GlideAjax to check the email is member of any group and achieve the requirement. 

Try with the below. I have tested in my PDI and It's working fine for me. 

 

// OnChange CS:
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   var email = g_form.getValue('email_id');
   var access = g_form.getValue('type_of_access');
   var ga = new GlideAjax('TestSI');
   ga.addParam('sysparm_name','checkGrpMembership');
   ga.addParam('sysparm_email',email);
   ga.addParam('sysparm_group',access);
   ga.getXML(callback);


	function callback(response){

		var answer = response.responseXML.documentElement.getAttribute("answer");
		//alert("Ans:"+answer);
		if(answer == 'true'){
			alert("You are already member of the group please select a different group.");
			g_form.clearValue('type_of_access');
			g_form.addErrorMessage("Please select a group which you are not part of...");
		}
	}

   
}

//Script Include: TestSI
checkGrpMembership: function(){
		var res = false;
		var email = this.getParameter('sysparm_email');
		var group = this.getParameter('sysparm_group');
		//gs.info("Testing User Email:"+email+":Group:"+group);
		var query = 'user.email='+email+'^group='+group;
		//gs.info("Testing User Email query:"+query);
		var getRec = new GlideRecord('sys_user_grmember');
		getRec.addEncodedQuery(query);
		getRec.query();
		if(getRec.next()){
			res = true;
		}
		return res;
	},

Appanna_0-1712248236927.png

 

Appanna_1-1712248281803.pngAppanna_2-1712248298251.png

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

View solution in original post

Appanna M
Tera Guru

Hello @shareef2 ,

 

Please Accept this as Solution and Mark my answer as Helpful. 

 

 

View solution in original post

3 REPLIES 3

Appanna M
Tera Guru

Hello @shareef2 ,

 

You can write an onChange Client script on the field Type of Access and making call using GlideAjax to check the email is member of any group and achieve the requirement. 

Try with the below. I have tested in my PDI and It's working fine for me. 

 

// OnChange CS:
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   var email = g_form.getValue('email_id');
   var access = g_form.getValue('type_of_access');
   var ga = new GlideAjax('TestSI');
   ga.addParam('sysparm_name','checkGrpMembership');
   ga.addParam('sysparm_email',email);
   ga.addParam('sysparm_group',access);
   ga.getXML(callback);


	function callback(response){

		var answer = response.responseXML.documentElement.getAttribute("answer");
		//alert("Ans:"+answer);
		if(answer == 'true'){
			alert("You are already member of the group please select a different group.");
			g_form.clearValue('type_of_access');
			g_form.addErrorMessage("Please select a group which you are not part of...");
		}
	}

   
}

//Script Include: TestSI
checkGrpMembership: function(){
		var res = false;
		var email = this.getParameter('sysparm_email');
		var group = this.getParameter('sysparm_group');
		//gs.info("Testing User Email:"+email+":Group:"+group);
		var query = 'user.email='+email+'^group='+group;
		//gs.info("Testing User Email query:"+query);
		var getRec = new GlideRecord('sys_user_grmember');
		getRec.addEncodedQuery(query);
		getRec.query();
		if(getRec.next()){
			res = true;
		}
		return res;
	},

Appanna_0-1712248236927.png

 

Appanna_1-1712248281803.pngAppanna_2-1712248298251.png

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

Hi @Appanna M ,

 

Thanks for the reply. I have tried this and it's working perfectly. 

 

Thanks,

Shareef

Appanna M
Tera Guru

Hello @shareef2 ,

 

Please Accept this as Solution and Mark my answer as Helpful.