Write a client script comparing current logged in user.

Ankita NA
Tera Contributor

Hie All,

 

I have a requirement to write a client script to achieve below scenario.

If current logged in user belongs to "Education_Dept" Group , then restrict it to update Hardware status to missing, decommissioned or NA in cmdb_ci_hardware table both form view and list view.

 

Here the group Education_Dept does not have user added directly to it, this group has several other groups approx 100 from where the user value is coming.

So, I have written the below script to get the value its fetching correct users from group's --> group but it not able to

compare user please suggest modifications.

 

 

var loggedInUser = g_user.userID;

var groupGR = new GlideRecord('sys_user_group');
groupGR.addQuery('parent','Education_Dept');
groupGR.query();
while(groupGR.next()){
    var groupId = groupGR.sys_id.toString();
    var groupMemberGR = new GlideRecord('sys_user_grmember');
    groupMemberGR.addQuery('group',groupId);
    groupMemberGR.query();
while(groupMemberGR.next()){
        var groupMember = groupMemberGR.user.toString();
if(groupMember === loggedInUser){
// then should not be able to change hardware status to missing or decommed or NA
}
    }

 
       
}

 

 

 

 

 

 

4 REPLIES 4

newhand
Mega Sage

@Ankita NA 
How about using  below code.. and it only works on server side .

You should move the check logic to business rule or script include ( call it via glideajax).

var currentUser = gs.getUser();

gs.info(currentUser.isMemberOf(­'Capacity Mgmt'));

 

 

Please mark my answer as correct and helpful based on Impact.

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Ankita NA ,

 

The best approach for this would be to go with GlideAjax n script include as it is not recommended to use GlideRecord in client script.

Mentioning few articles with different approach for these. 

 

https://www.servicenow.com/community/hrsd-articles/validate-logged-in-user-is-member-of-current-assi...

https://www.trylearngrowrepeat.com/check-if-member-is-already-in-group/

 

Thanks,

Danish

 

 

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @Ankita NA 

 

It is not good practice to use GlideRecord at client side, instead create a client callable script include and call it in your client script. Try the following code,

 

1. CLient Callable Script Include:

 

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

	checkLoggedInUser: function(){
		var gr = new GlideRecord("sys_user_grmember");
		gr.addEncodedQuery("group.parent.name=Education_Dept^user.sys_id" + gs.getUserID());
		gr.query();

		if(gr.hasNext()){
			return "true";
		}
		return "false";
	},

    type: 'CheckEducationDeptUser'
});

 

AnveshKumarM_0-1699414972147.png

 

 

2. Client Script:

 

Use the following client script snippet in any of your client script like onLoad, onChange or onCellEdit. Just add the logic to restrict user.

 

var ga = new GlideAjax("CheckEducationDeptUser");
   ga.addParam("sysparm_name", "checkLoggedInUser");
   ga.getXMLAnswer(checkUser);

   function checkUser(answer){
	if(answer == 'true'){
		//Restrict the user from editing the field you want
	}
   }

 

Please mark my answer helpful and accept as solution if it helped 👍✔️

Thanks,
Anvesh

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita NA 

why not handle it via field level WRITE ACL?

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