
- 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 05:04 AM
@Sandeep Rajputok, thats a great step forward, thanks. the alert is now showing that its returning the sys_id. However, its not setting the value on the hidden variable. The variable is in a variable set on the record producer if that might make a difference?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 05:25 AM
@M_iA Can you confirm the following.
1. If the variable new_contact_record is a reference field referring to sys_user table. It doesn't matter if the variable is part of variable set.
2. Disable the UI policy which hides the new_contact_record variable for the testing purposes and see if the variable get populated
3. Once the field is set the record producer script will automatically pick up the value.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 05:33 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 05:39 AM
@M_iA Sent you a direct message. Please check

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 05:54 AM
@Sandeep RajputSo I updated the field on the record producer to a string to see what happens here and then updated the function in the in the script to:
function myCallbackFunction(response) {
var answer = response;
alert(answer);
g_form.setValue('new_contact_record', answer);
var usr = g_form.getValue('new_contact_record');
alert(usr);
}
The alert shows that the field is populated with the sys_id. But for some reason which i cant figure out is that the field is then cleared out by the time the record is created