- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2024 03:17 AM
Hi, i was asked this question in an interview. please let me know what script we need to write.
On an Incident form only Assignment group members should be able to edit short description field. How will we implement it?
Thanks,
sry
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2024 04:04 AM
Hi @sry
Yes, you are correct. We don't wrote GLIDERECORD in client script. Here i am providing script include and client scripts (onLoad & onChange) this will achieve your requirements:
Script include:
var CheckGroupMembership = Class.create();
CheckGroupMembership.prototype = {
initialize: function() {},
isUserInGroup: function(assignmentGroupId, userId) {
var userInGroup = false;
// Query to check if the user is a member of the assignment group
var groupMemberGR = new GlideRecord('sys_user_grmember');
groupMemberGR.addQuery('group', assignmentGroupId);
groupMemberGR.addQuery('user', userId);
groupMemberGR.query();
if (groupMemberGR.next()) {
userInGroup = true; // User is in the assignment group
}
return userInGroup; // Return true or false
},
type: 'CheckGroupMembership'
};
onLoad Client script:
function onLoad() {
var assignmentGroup = g_form.getValue('assignment_group'); // Get the assignment group
var userID = g_user.userID; // Get the current user ID
// Proceed only if the assignment group is set
if (assignmentGroup) {
// Create a GlideAjax object
var ga = new GlideAjax('CheckGroupMembership');
ga.addParam('sysparm_name', 'isUserInGroup');
ga.addParam('sysparm_assignment_group', assignmentGroup);
ga.addParam('sysparm_user', userID);
// Call the Script Include and get the response
ga.getXMLAnswer(function(response) {
var userInGroup = response === 'true'; // Convert response to boolean
// Disable the short description field if the user is not in the group
g_form.setReadOnly('short_description', !userInGroup);
// Optional: Inform the user
if (!userInGroup) {
g_form.addInfoMessage("You do not have permission to edit the short description.");
}
});
} else {
// Optional: handle case when no assignment group is selected
g_form.addInfoMessage("Please select an assignment group.");
}
}
onChange client script: To handle the scenario where the assigned user changes after the form loads
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return; // Do not execute if the form is loading or the new value is empty
}
var assignmentGroup = g_form.getValue('assignment_group'); // Get the current assignment group
var userID = newValue; // Use the new assigned user ID
if (assignmentGroup) {
// Create a GlideAjax object
var ga = new GlideAjax('CheckGroupMembership');
ga.addParam('sysparm_name', 'isUserInGroup');
ga.addParam('sysparm_assignment_group', assignmentGroup);
ga.addParam('sysparm_user', userID);
// Call the Script Include and get the response
ga.getXMLAnswer(function(response) {
var userInGroup = response === 'true'; // Convert response to boolean
// Disable the short description field if the new user is not in the group
g_form.setReadOnly('short_description', !userInGroup);
// Optional: Inform the user
if (!userInGroup) {
g_form.addInfoMessage("You do not have permission to edit the short description.");
} else {
g_form.clearInfoMessage(); // Clear any previous message if the user is now allowed
}
});
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2024 04:14 AM
better to go with the ACL if we implement on change script the user can update in list layout if they have acesss
You can create a write acl on incident.short_desription and add the conditions
Please let me know if it is helpful