Client script onSubmit is not working as it should be

eyal abu hamad
Mega Sage

Hey all, I have a client script that runs onsubmit that get value from field (custom user table reference) and createing new user in sys_user table. after that it populate a filed. I will attach photo to further explain.

eyalabuhamad_0-1708943824210.png

as you can see, when the caller field is empty we have a true false check box.

eyalabuhamad_1-1708943860267.png

if we press the check box all the fields disappear and we have new field that points on custom user table.
after I choose the inActive employee and press save I want to popluate the caller field and set the check box back to false.

eyalabuhamad_2-1708943995866.png

like this for example. it well populate the fields and remove the checkbox (I have ui policy that check if caller field is empty)


my problem is that when I press save first time nothing change and when I press once again it will work.
how can I do it from first try ?

I  will attach the scrupts

client script

 

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

    var func = new GlideAjax('customTable');
    var user = g_form.getValue('u_inactive_employee');
    var usercheck = g_form.getValue('u_didn_t_find_user');
    if (user != '' && usercheck == 'true') {
        func.addParam('sysparm_name', 'createUserFromOldEmployee');
        func.addParam('sysparm_Userid', user);
		func.addParam('sysparm_mamash_request', g_form.getUniqueValue());
        func.getXML(getResponse);
    }

    function getResponse(response) {
        var values = response.responseXML.documentElement.getAttribute('answer');
		if(values != ''){
			alert(values);
			
		}
    }

}

 

and the script include function

 

createUserFromOldEmployee: function() {
        var user_sys_id = '';
        var Request = this.getParameter("sysparm_request");
        var inActiveUser = this.getParameter("sysparm_Userid");
        var gr = new GlideRecord('u_past_employees');
        gr.get('sys_id', inActiveUser);
        gr.query();
        if (gr.next()) {
            var user = new GlideRecord('sys_user');
            user.addQuery('user_name', gr.u_user_id);
            user.query();
            if (user.next()) {
                user.active = true;
                user.update();
                user_sys_id= user.sys_id;
            } else {
                var newUser = new GlideRecord('sys_user');
                newUser.initialize();
                newUser.user_name = gr.u_user_id;
                newUser.first_name = gr.u_first_name;
                newUser.last_name = gr.u_last_name;
                newUser.email = gr.u_email;
                newUser.name = gr.u_name;
                newUser.state = gr.u_state_province;
                newUser.preferred_language = gr.u_language;
                newUser.title = gr.u_title;
                newUser.active = gr.u_active;
                newUser.company = gr.u_company;
                newUser.location = gr.u_location;
                newUser.department = gr.u_department;
                newUser.mobile_phone = gr.u_mobile_phone;
                newUser.u_employee_id = gr.u_employee_id;
                newUser.u_team = gr.u_team;
                newUser.manager = gr.manager;
                newUser.update();
                user_sys_id= newUser.sys_id;
            }
        }
        var grRequest = new GlideRecord('u_requests');
        grRequest.get('sys_id', Request);
        grRequest.query();
        if (grRequest.next()) {
			
			grRequest.setValue('u_inactive_employee','');
			grRequest.setValue('u_didn_t_find_user','false');
            grRequest.setValue('u_caller', user_sys_id);
			grRequest.update();
			return grRequest.u_didn_t_find_user;
        }

    },

 

 

5 REPLIES 5

Weird
Mega Sage

Using GlideAjax means you're telling the server to run some code, but you're not preventing the client side from continuing. Now you're updating the server side and the page is refreshed, but the data had yet to be updated during that time. Next time you press the button, the data is already updated and it looks like saving again was the cause.

Try refreshing the page a bit after the first save and see if the data updates correctly.

If you want to do this on client side, you should do something like this:

function onSubmit() {
    if (!g_form.submitOk) {
        var func = new GlideAjax('customTable');
        var user = g_form.getValue('u_inactive_employee');
        var usercheck = g_form.getValue('u_didn_t_find_user');
        if (user != '' && usercheck == 'true') {
            func.addParam('sysparm_name', 'createUserFromOldEmployee');
            func.addParam('sysparm_Userid', user);
            func.addParam('sysparm_mamash_request', g_form.getUniqueValue());
            func.getXML(getResponse);
			return false;
        }
    }
}

    function getResponse(response) {
        var values = response.responseXML.documentElement.getAttribute('answer');
		if(values != ''){
			g_form.submitOk = true;
			g_form.submit();
		}else{
			g_form.submitOk = false;
alert("Something went wrong");
		}
    }


So what happens here is that during the first submit we tell the script onSubmit to be false since submitOK is false.
This prevents the form from being saved.
The GlideAjax will still keep running in the background and return the response. The values are updated on server side and the script then checks that "values" is not empty and marks submitOk as true and resubmits (saves) the form. Now because submitOk it true the submit will go through without issues and you should see the saved values.

This of course assumes that the server side script is correctly working.
Also note that doing this can potentially keep the form controllable, so it might confuse the user, if the background process takes longer than a few seconds.

function onSubmit() {
    //Type appropriate comment here, and begin script below
    if (!g_form.submitOk) {
        var func = new GlideAjax('CustomTable');
        var user = g_form.getValue('u_inactive_employee');
        var usercheck = g_form.getValue('u_didn_t_find_user');
        if (user != '' && usercheck == 'true') {
            func.addParam('sysparm_name', 'createUserFromOldEmployee');
            func.addParam('sysparm_Userid', user);
            func.addParam('sysparm_mamash_request', g_form.getUniqueValue());
            func.getXML(getResponse);
            return false;
        }
    }

    function getResponse(response) {
        var values = response.responseXML.documentElement.getAttribute('answer');
        if (values != '') {
            g_form.submitOk = true;
            g_form.submit();
        } else {
            g_form.submitOk = false;
            alert("Something went wrong");
        }

    }
}

I changed the script to this but still nothing changed, 

Hi @eyal abu hamad , 

 

You have added the flower braces at wrong place please check again , remove one braces from end and add it before function starts and try,

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

it didn't work either.

function onSubmit() {
    //Type appropriate comment here, and begin script below
    if (!g_form.submitOk) {
        var func = new GlideAjax('CustomTable');
        var user = g_form.getValue('u_inactive_employee');
        var usercheck = g_form.getValue('u_didn_t_find_user');
        if (user != '' && usercheck == 'true') {
            func.addParam('sysparm_name', 'createUserFromOldEmployee');
            func.addParam('sysparm_Userid', user);
            func.addParam('sysparm_mamash_request', g_form.getUniqueValue());
            func.getXML(getResponse);
            return false;
        }
    }
}

function getResponse(response) {
    var values = response.responseXML.documentElement.getAttribute('answer');
    if (values != '') {
        g_form.submitOk = true;
        g_form.submit();
    } else {
        g_form.submitOk = false;
        alert("Something went wrong");
    }

}