Catalog OnSubmit Script is not working in ESC portal

Mark Wood
Tera Contributor

Hello Team,

I have written the following code for my requirement. I need to check whether the user's manager is active or not. If the manager is inactive, a confirmation popup should appear before submitting the request. If the user clicks "OK," the form should submit; otherwise, if they click "Cancel," the form should not be submitted.

I have written the client script and script include code below.

Could you please guide me on this?

Thank you!

 

Client Script:

function onSubmit() {
  
    var gr = new GlideAjax('check_hrs');
    gr.addParam('sysparm_name', 'manager_active');
    gr.addParam('sysparm_manager', g_form.getValue('subject_person_s_manager'));
    gr.getXMLAnswer(setAnswer);

    return false; // Prevent form submission until the response is received
}

function setAnswer(answer) {
    if (answer == 'false') {
        var anna = confirm('Please Confirm: User does not have a manager.');
        if (!anna) {
            return false; // Stop execution if the user cancels
        }
    }
  if (g_scratchpad.isFormValid) {
        return true;
    }

    g_scratchpad.isFormValid = true;
    g_form.submit(g_form.getActionName());
}

Script Include

var check_hrs = Class.create();
check_hrs.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    demo: function() {


        var sdate = this.getParameter('sysparm_form');
        var gh = new GlideDateTime(sdate);

        var gd = new GlideDateTime();
        var gdd = gd.getDisplayValue();
        var gddd = new GlideDateTime(gdd);

        var gt = GlideDateTime.subtract(gddd, gh);
        var sec = gt.getNumericValue();
        if (sec > 14400000) {
            return true;
        } else {
            return false;
        }
    },

    check_manager: function() {
        var gr = new GlideRecord('sys_user');
        gr.get(this.getParameter('sys_user_id'));
        if (gr.manager) {
            return true;
        } else {
            return false;
        }
    },

    manager_active: function() {
        var gr = new GlideRecord('sys_user');
        if(gr.get(this.getParameter('sysparm_manager'))){
			if(gr.active.toString() == 'true'){
				return true;
			}else{
				return false;
			}
            
        } else{
            return 'No Manager';
        }
    },
    type: 'check_hrs'
});
5 REPLIES 5

Rajesh Chopade1
Mega Sage

Hi @Mark Wood 

one observation - the script include manager_active is almost correct, but you are returning true or false (booleans) directly from the server-side code, while the client script expects these responses to be processed as strings ('true' or 'false'), not booleans.

Anand Kumar P
Giga Patron
Giga Patron

Hi @Mark Wood ,

 

In client side use alerts or info message to check what exactly your getting into answer ,

Try returning from SI as string below,Insted Boolean 0 or 1.

if (gr.active) {

 return 'true';

} else {

 return 'false';

}

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

Ankur Bawiskar
Tera Patron
Tera Patron

@Mark Wood 

try this

manager_active: function() {
        var gr = new GlideRecord('sys_user');
        if(gr.get(this.getParameter('sysparm_manager'))){
			if(gr.active.toString() == 'true'){
				return 'true';
			}else{
				return 'false';
			}
            
        } else{
            return 'No Manager';
        }
    },

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Mark Wood 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader