官术网_书友最值得收藏!

GlideUser

GlideUser or g_user is a class all about the attributes of the user. It is not the biggest class, but some of the properties and methods can be used quite frequently.

One of the most helpful of these is being able to get the user ID, the sys_id of the user, through a property. This can be very helpful to send to another script so that the script knows which user is currently logged in or for getting the user record.

Let's have a look at how to get this ID:

var userSys_ID = g_user.userID;

In the example, the userSys_ID variable will be the sys_id of the logged-in user.

When getting a user record, it is best practice to use the sys_id rather than a name to obtain the record. This is because there is a chance users in the database may have the same name, whereas the sys_id of a user will always be unique.

We can also use g_user to get the full name of the logged-in user. This can be helpful if we want to display a user's name in messages displayed to them or in fields. 

We can get a user's full name by using the following example:

var name = g_user.getFullName();
alert('The logged in user is ' + name);

This preceding example sets the name variable to be the user's full name. Let's assume I am the logged-in user here. The alert method shows a pop-up message to the user, so in this scenario, it would say The logged in user is Andrew Kindred.

Sometimes when scripting, we want to know whether the user has a specific role so we can decide whether or not a piece of code should run for them or not. To do this on the client side, we can use the hasRole method. It works in a very similar way to the server-side GlideSystem method.

Let's look at an example of the hasRole method:

if (g_user.hasRole('itil') {
//Run code for user with the itil role only
}

As you can see, this code is very similar to the server-side code. We can use an if statement to ensure that we only run certain code for users with a certain role.

There are a few other methods relating to basic user data and roles, but we have covered the most useful ones here.

主站蜘蛛池模板: 上饶县| 图们市| 白山市| 舟山市| 右玉县| 荃湾区| 陈巴尔虎旗| 读书| 静宁县| 固镇县| 志丹县| 德江县| 陈巴尔虎旗| 射洪县| 宿州市| 罗定市| 三原县| 岳阳县| 中宁县| 南京市| 宁乡县| 高雄县| 瓦房店市| 莱阳市| 南汇区| 泰顺县| 麻栗坡县| 永清县| 公主岭市| 西青区| 绥化市| 台安县| 五原县| 唐海县| 万荣县| 西吉县| 新兴县| 横峰县| 龙江县| 长沙县| 福鼎市|