- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2024 12:12 AM
Hi All,
My Requirement is When the assignment group on the incident form is changed, display the assignment group's manager name on form. (Hint - Use Client script and script include) so i written everything but unfortunately it is showing an error in testing time i am unable to sort out that problem can any one please help me to resolve the issue.
Please follow the below screenshots and code
Script Include:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2024 06:32 AM - edited ‎05-01-2024 10:02 AM
Hi @SNow Learner41,
I'm struggling to understand why you want to do this via an onChange Client Script and implementing a new field that already exists. I'd strongly recommend you use the OOB functionality already present and related field in my earlier post.
However, to provide you with the code and help with your understanding of calling Server-side code from the client, see the below changes and note the use of GlideAjax.
Please note - I've assumed your customer field 'u_manager_name' is a reference field. If not, comment and uncomment accordingly on the Script include provided.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//alert('running Custom manager'); //USed for testing to confirm the Script is being called.
var assignmentGroupId = g_form.getValue('assignment_group');
if (assignmentGroupId) {
var assignmentGroupManagerUtils = new GlideAjax('AssignmentGroupManagerUtils');
assignmentGroupManagerUtils.addParam('sysparm_name', 'getManagerName')
assignmentGroupManagerUtils.addParam('sysparm_assignmentGroup', assignmentGroupId);
assignmentGroupManagerUtils.getXMLAnswer(updateCustomManager);
}
}
function updateCustomManager(response) {
//alert(response); //Used for testing. Uncommon to check response value if required
if (response) {
//var returneddata = answer.evalJSON(true);
g_form.setValue("u_manager_name", response); // Set the manager name field on the form using sys_id assuming the u_manager_name field is a reference field
} else {
g_form.setValue("u_manager_name", ''); // Clear the manager name field if manager not found
}
}
Script Include: (Make sure the Script Include 'Client Callable' field is checked / set to true)
var AssignmentGroupManagerUtils = Class.create();
AssignmentGroupManagerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Function to get the manager name of an assignment group
getManagerName: function() {
var assignmentGroupId = this.getParameter('sysparm_assignmentGroup');
var managerName = '';
var group = new GlideRecord('sys_user_group');
if (group.get(assignmentGroupId)) {
managerName = group.manager; //retruning the sys_id assuming the custom u_manager_name field is a reference field.
Change accordingly
//managerName = group.manager.name.toString(); retrurning the string value if the custom u_manager_name field is a string
}
return managerName;
},
//type: 'AssignmentGroupManagerUtilss'
//};
type: 'AssignmentGroupManagerUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2024 10:02 AM - edited ‎05-01-2024 10:03 AM
Hi @SNow Learner41,
I'm struggling to understand why you want to do this via an onChange Client Script and implementing a new field that already exists. I'd strongly recommend you use the OOB functionality already present and related field as shown and discussed in my earlier post.
However, to provide you with the code and help with your understanding of calling Server-side code from the client, see the below changes and note the use of GlideAjax.
Please note - I've assumed your customer field 'u_manager_name' is a reference field. If not, comment and uncomment accordingly on the Script include provided.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//alert('running Custom manager'); //USed for testing to confirm the Script is being called.
var assignmentGroupId = g_form.getValue('assignment_group');
if (assignmentGroupId) {
var assignmentGroupManagerUtils = new GlideAjax('AssignmentGroupManagerUtils');
assignmentGroupManagerUtils.addParam('sysparm_name', 'getManagerName')
assignmentGroupManagerUtils.addParam('sysparm_assignmentGroup', assignmentGroupId);
assignmentGroupManagerUtils.getXMLAnswer(updateCustomManager);
}
}
function updateCustomManager(response) {
//alert(response); //Used for testing. Uncommon to check response value if required
if (response) {
//var returneddata = answer.evalJSON(true);
g_form.setValue("u_manager_name", response); // Set the manager name field on the form using sys_id assuming the u_manager_name field is a reference field
} else {
g_form.setValue("u_manager_name", ''); // Clear the manager name field if manager not found
}
}
Script Include: (Make sure the Script Include 'Client Callable' field is checked / set to true)
var AssignmentGroupManagerUtils = Class.create();
AssignmentGroupManagerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Function to get the manager name of an assignment group
getManagerName: function() {
var assignmentGroupId = this.getParameter('sysparm_assignmentGroup');
var managerName = '';
var group = new GlideRecord('sys_user_group');
if (group.get(assignmentGroupId)) {
managerName = group.manager; //retruning the sys_id assuming the custom u_manager_name field is a reference field.
Change accordingly
//managerName = group.manager.name.toString(); retrurning the string value if the custom u_manager_name field is a string
}
return managerName;
},
//type: 'AssignmentGroupManagerUtilss'
//};
type: 'AssignmentGroupManagerUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2024 11:05 AM
Hi dgarad,
Thank you for response and information, i tried in that way also but same error i am facing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2024 10:02 AM - edited ‎05-01-2024 10:03 AM
Hi @SNow Learner41,
I'm struggling to understand why you want to do this via an onChange Client Script and implementing a new field that already exists. I'd strongly recommend you use the OOB functionality already present and related field as shown and discussed in my earlier post.
However, to provide you with the code and help with your understanding of calling Server-side code from the client, see the below changes and note the use of GlideAjax.
Please note - I've assumed your customer field 'u_manager_name' is a reference field. If not, comment and uncomment accordingly on the Script include provided.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//alert('running Custom manager'); //USed for testing to confirm the Script is being called.
var assignmentGroupId = g_form.getValue('assignment_group');
if (assignmentGroupId) {
var assignmentGroupManagerUtils = new GlideAjax('AssignmentGroupManagerUtils');
assignmentGroupManagerUtils.addParam('sysparm_name', 'getManagerName')
assignmentGroupManagerUtils.addParam('sysparm_assignmentGroup', assignmentGroupId);
assignmentGroupManagerUtils.getXMLAnswer(updateCustomManager);
}
}
function updateCustomManager(response) {
//alert(response); //Used for testing. Uncommon to check response value if required
if (response) {
//var returneddata = answer.evalJSON(true);
g_form.setValue("u_manager_name", response); // Set the manager name field on the form using sys_id assuming the u_manager_name field is a reference field
} else {
g_form.setValue("u_manager_name", ''); // Clear the manager name field if manager not found
}
}
Script Include: (Make sure the Script Include 'Client Callable' field is checked / set to true)
var AssignmentGroupManagerUtils = Class.create();
AssignmentGroupManagerUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Function to get the manager name of an assignment group
getManagerName: function() {
var assignmentGroupId = this.getParameter('sysparm_assignmentGroup');
var managerName = '';
var group = new GlideRecord('sys_user_group');
if (group.get(assignmentGroupId)) {
managerName = group.manager; //retruning the sys_id assuming the custom u_manager_name field is a reference field.
Change accordingly
//managerName = group.manager.name.toString(); retrurning the string value if the custom u_manager_name field is a string
}
return managerName;
},
//type: 'AssignmentGroupManagerUtilss'
//};
type: 'AssignmentGroupManagerUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2024 10:17 AM
Hi Robbie,
it is working fine thank you very much for your response and for your help.