
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2024 09:29 AM
I have a simple incident record producer.
On it, I have a reference field looking at the sys_user table. if the user is not available, I have manual fields for the user to enter the employee details.
The fields have onchange scripts to detect whether the user is a duplicate.
The record producer then has a script that inserts the manual record in the user table:
if (producer.is_employee_available == true || producer.is_employee_available == "true") {
var gr = new GlideRecord("sys_user");
gr.initialize();
gr.user_name = producer.email;
gr.first_name = producer.first_name;
gr.last_name = producer.last_name;
gr.email = producer.email;
gr.phone = producer.phone;
gr.company = producer.account;
gr.employee_number = producer.employee_number;
gr.insert();
gs.flushMessages();
}
I now want to reference this new record in a new reference field on the incident and im not sure how I would do this.
im also struggling to get gs.flushMessages(); to work as it still shows the messages to say that the default roles have been added.
Could anyone point me in the right direction?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 07:23 AM
@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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 06:04 AM
@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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 06:48 AM
@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...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 07:23 AM
@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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 07:45 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2024 12:06 PM
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();