
- 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-03-2024 09:39 AM
@M_iA You can update your script as follows and assign the newly created user to the caller_id field of the incident (you can choose to assign the user to any other reference field. I took example of the caller_id reference field.)
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;
current.caller_id = gr.insert();
gs.flushMessages();
}
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2024 11:20 AM
@Sandeep Rajput- Thanks so much for
current.caller_id = gr.insert();
It worked well. However, the gs.flushMessages() did not work and I still see the message role confirmation. Any ideas?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-03-2024 12:12 PM
You can try and create an onAfter insert business rule on sys_user_has_role table and in script use
gs.flushMessages();
This way those multiple info messages may get flushed.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 01:54 AM
@Sandeep RajputUnfortunately that didnt work and im still getting them.
If I change the insert so that it gets inserted via an onSubmit client script, the flushmessages works. But then any ideas on how I would link the record producer submission to the newly created record? ive tried with a gliderecord query looking for the record, but think the onsubmit is too slow