Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Catalog OnSubmit Script is not working in ESC portal

AnkurBaruah
Tera Contributor

 

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var a = g_form.getValue('group_name');
    var ga = new GlideAjax('isGroupCreated');
    ga.addParam('sysparm_name', 'isNameExists');
    ga.addParam('Group_Name', a);
ga.getXMLWait();

var answer = ga.getAnswer();



var mem = g_form.getValue('EU06') ? g_form.getValue('EU06').split(',').length : 0;

 if (answer == 'false') {
        alert('Group wih the same name already exists:\nPlease try with Different Name');
        return false;
    } else if (answer == 'true') {
        alert('Name is Unique');
		var anna = confirm('Please Confirm Your Request Details: \nGroup Name: '+g_form.getValue('group_name')+'\nDescription: '+ g_form.getValue('EU02')+'\nGroup Email: '+g_form.getValue('EU03')+'\nManager for the group: '+ g_form.getDisplayBox('EU04').value+'\nNumber of Members: '+ mem);
		
		if (anna == false){
		return false;}
        }

    }
	

 

Hi Community, 

This above code is working fine , when I use 'Try It' in Catalog Item and try to submit the item, but doesnt work the same when I happen to use this in my EmployeeCenter [ESC] it says 'Javascript Error'.

 

Also Above the Catalog Client Script this info pops up: 

This catalog client script is not VA supported due to the presence of the following variables in the script: g_form.getDisplayBox.value, confirm
New client-scripts are run in strict mode, with direct DOM access disabled. Access to jQuery, prototype and the window object are likewise disabled. To disable this on a per-script basis, configure this form and add the "Isolate script" field. To disable this feature for all new globally-scoped client-side scripts set the system property "glide.script.block.client.globals" to false.

 

1 ACCEPTED SOLUTION

JenniferRah
Mega Sage

You have to put it in a function for portal. Try this:

function onSubmit() {

    if (g_scratchpad.isFormValid) //need to check to see if you should process or not
        return true;

    var a = g_form.getValue('group_name');
    var ga = new GlideAjax('isGroupCreated');
    ga.addParam('sysparm_name', 'isNameExists');
    ga.addParam('Group_Name', a);
    ga.getXMLAnswer(getResponse);
    return false;
}

function getResponse(response) {
    var answer = response;
    var mem = g_form.getValue('EU06') ? g_form.getValue('EU06').split(',').length : 0;

    if (answer == 'false') {
        alert('Group wih the same name already exists:\nPlease try with Different Name');
        return false;
    } else if (answer == 'true') {
        alert('Name is Unique');
        var anna = confirm('Please Confirm Your Request Details: \nGroup Name: ' + g_form.getValue('group_name') + '\nDescription: ' + g_form.getValue('EU02') + '\nGroup Email: ' + g_form.getValue('EU03') + '\nManager for the group: ' + g_form.getDisplayBox('EU04').value + '\nNumber of Members: ' + mem);
        if (anna == false) {
            return false;
        }
    }
    var actionName = g_form.getActionName();
    g_scratchpad.isFormValid = true;
    g_form.submit(actionName);
}

 

View solution in original post

6 REPLIES 6

Hello @JenniferRah 

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!

 

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'
});

 

 

I don't see any glaring issues with the code. How far are you getting? And do you have Isolate Script checked or unchecked? It should probably be unchecked.