- 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-29-2024 05:56 AM
Hi @SNow Learner41,
There is a much easier and Out Of Box (OOB) field that is available and already handle this for you.
You don't need to write your own Client Script or Script Include.
Instead, follow the below steps. Essentially, any field from a related table can easily be pulled into the form.
- Right-click on the form header and click on 'Configure > Form Layout'
- On the left-hand side of the 'Slush Bucket', navigate to the 'Assignment group field. You'll notice it's green and has a + symbol next to it. This tells us it's a related table where more fields are available.
- With the Assignment Group field highlighted, click on the top icon above the arrow icons in the center of the page between the 2 columns of data (See screenshot below)
- This allows us to now show the Assignment Group fields.
- Either double-click on the manager of select the manager field and click on the right-hand point icon to move the field from the left side to the right side.
- You may now want to move the Assignment Group Manager field up to under where the current Assignment group field as shown below.
- Hit 'Save'
- Your form will now have the Assignment Group Manager field and it will display, update and change whenever the Assignment Group is populated or changed (Provided the Manager field is of course populated)
- Once tested, last but not least - delete/remove the Client Script you created, Script Include, and the u_manager field you created as they're not needed.
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
This last screenshot is just to show how you can use the OOB field called 'manager'. You'll notice it shows the who path to get to this value: incident.assignment_group.manager. You don't need to change or do anything more with it..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2024 11:03 AM
Hi Robbie,
Thank you for your reply and efforts but my scenario is to complete this one using with client script only i tried it already but not getting output properly please help me for finding the error.
- 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
04-29-2024 06:08 AM
use GlideAjax to call script include from client script.
refer below link for GlideAjax
https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet/ta-p/2312430
Thanks
dgarad