@M_iA This is super strange. Can you check if there is a UI Policy Action applied on this field with a clear value check box set to checked.

@Sandeep Rajput No ui policies as a new field.

Ive been taking a look in the community and im thinking that because its asynchronous GlideAjax, then the record producer is already submitted before the script is returning the sys_id? Will this being on the Service Portal be the reason for this?

 

But then another article ive found is: https://www.servicenow.com/community/developer-forum/on-submit-client-script-variable-set-value-is-g...

 

@M_iA Ah! you are right your GlideAjax is an asynchronous call and your form is already getting submitted before you get the response from the server.

 

Please update your client script based on the following code.

 

function onSubmit() {
if (g_scratchpad.isFormValid){
	return true;
}
    var checkboxResponse = g_form.getValue('is_employee_available');
    if (checkboxResponse == true || checkboxResponse == "true") {
       var actionName = g_form.getActionName();
        var ga = new GlideAjax('global.CustomerHelpCenterUtils');
        ga.addParam('sysparm_name', 'insertUserRecord');
        ga.addParam('sysparm_user_first', g_form.getValue('first_name'));
        ga.addParam('sysparm_user_last', g_form.getValue('last_name'));
        ga.addParam('sysparm_user_email', g_form.getValue('email_address'));
        ga.addParam('sysparm_user_telephone', g_form.getValue('telephone'));
		ga.addParam('sysparm_user_company', g_form.getValue('account'));
		ga.addParam('sysparm_user_id', g_form.getValue('employee_id_number'));
        ga.getXMLAnswer(myCallbackFunction);
    }

    function myCallbackFunction(response) {
                g_scratchpad.isFormValid = true;
	g_form.submit(actionName);
		var answer = response;
		alert(answer);
		g_form.setValue('new_contact_record', answer);
    }
return false;
}

 

Source: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579

 

Hope this helps.

View solution in original post

@Sandeep RajputI had to move

g_form.submit(actionName);

to underneath the setValue line. But this worked.

 

Thanks so much for your help, its really appreciated

debendudas
Mega Sage

Hi @M_iA ,

The gr.insert() function returns the sys_id of the inserted record, or null if the record was not inserted.


You can store the return value in a variable or you can directly use the return value in the reference field.

current.caller_id = gr.insert();