If the category is changed post adding attachments, an alert message should popup on front end porta

hema26
Tera Contributor

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.

hema26_0-1710848004254.png

 


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 .

1 ACCEPTED SOLUTION

AakashGarg1678
Kilo Guru

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

View solution in original post

1 REPLY 1

AakashGarg1678
Kilo Guru

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