- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2024 04:34 AM
In order to create a HR case we fill all the mandatory fields category and sub category. when the user changes his mind to select other category ,its dependence field sub category value is cleared ,where I am failing to popup the alert message post the attachment.
I would request community friends to help me on this
Requirement would be :
If the category is changed post adding attachments, an alert message should pop up ,however it should not stop the requester in submitting the case .
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 10:33 AM
Hi @hema26,
Hope you are doing well.
I tried to find the solution for your scenario or requirement and implement it on my Personal Developer Instance, so that you can easily understand and get the perfect solution in one go.
Solution Proposed
As a solution, you need to create an "On Change - Catalog Client Script" that will execute the logic or implementation as per the "Category" Field changes. Attaching the script that will work for both the "Portal" as well as "Non-Portal" Environment.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
// Portal
if (this.document.getElementsByClassName('get-attachment').length != 0) {
alert("Hey, Post Attachment - Category can't be changed.");
// return false; - Seems it does not work in on-change client script
g_form.clearValue('category');
}
} catch (e) {
// Non-Portal
var sysid = g_form.getElement('sysparm_item_guid').value;
var rowcount = 0;
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
while (attachment.next()) {
rowcount++;
}
if (rowcount != 0) {
alert("Hey, Post Attachment - Category can't be changed.");
g_form.clearValue('category');
}
}
}
For your reference, also attaching screenshots of the outputs that will give you better insights of how this script is working or the best thing will be to follow the solution and execute the script on your instance.
Note: - I tried that the "Category" Field value will not change Post Attachment by using the "return false" as you can see a line in my script as a comment, but unable to do so. With this, I found that it may not be useful in an "On Change Client Script". This only works in an "On Submit Client Script" where "return false" is used to abort the action or submitting the request.
If you find this answer/solution/suggestion as helpful to your question asked or meet with your requirement, you can mark this solution/answer/suggestion as helpful, and correct.
Thanks
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 10:33 AM
Hi @hema26,
Hope you are doing well.
I tried to find the solution for your scenario or requirement and implement it on my Personal Developer Instance, so that you can easily understand and get the perfect solution in one go.
Solution Proposed
As a solution, you need to create an "On Change - Catalog Client Script" that will execute the logic or implementation as per the "Category" Field changes. Attaching the script that will work for both the "Portal" as well as "Non-Portal" Environment.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
// Portal
if (this.document.getElementsByClassName('get-attachment').length != 0) {
alert("Hey, Post Attachment - Category can't be changed.");
// return false; - Seems it does not work in on-change client script
g_form.clearValue('category');
}
} catch (e) {
// Non-Portal
var sysid = g_form.getElement('sysparm_item_guid').value;
var rowcount = 0;
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
while (attachment.next()) {
rowcount++;
}
if (rowcount != 0) {
alert("Hey, Post Attachment - Category can't be changed.");
g_form.clearValue('category');
}
}
}
For your reference, also attaching screenshots of the outputs that will give you better insights of how this script is working or the best thing will be to follow the solution and execute the script on your instance.
Note: - I tried that the "Category" Field value will not change Post Attachment by using the "return false" as you can see a line in my script as a comment, but unable to do so. With this, I found that it may not be useful in an "On Change Client Script". This only works in an "On Submit Client Script" where "return false" is used to abort the action or submitting the request.
If you find this answer/solution/suggestion as helpful to your question asked or meet with your requirement, you can mark this solution/answer/suggestion as helpful, and correct.
Thanks
Aakash Garg
ServiceNow Developer