How to update the record using UI pages

Niranjan Danda
Tera Contributor

I am opening a dialog box using ui action from an incident record.I am passing the sys_id of the current incident record and trying to update the same record with the values entered in the multi line field of a dialog box. But after submitting the dialog box, it is redirected to the url of my ui page.

 

Here is my code in UI action:

 

UI action:

function sendEmail(){

//Initialize and open the Dialog Window
var sysid = g_form.getUniqueValue();
var dialog = new GlideDialogWindow("notification_update");dialog.setTitle("Notification Preview");
dialog.setPreference("sysid", sysid);
dialog.render();
}

Here  is my code in UI page:

 

html:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:ui_form>
<g:evaluate var="jvar_sysid"
expression="RP.getWindowProperties().sysid"/>
<table border="0" width="100%">
<tr>
<td>
<g:ui_multiline_input_field name="notes" label="update" mandatory="true" />
</td>
</tr>
<tr>
<td>
<g:dialog_buttons_ok_cancel cancel="return onCancel();" ok="return onSubmit();"/>
<input type="hidden" id="task_sys_id" name="task_sys_id" value="${jvar_sysid}"/>
</td>
</tr>
</table>
</g:ui_form>
</j:jelly>

 

Client script:

function onCancel() {
GlideDialogWindow.get().destroy();
return false;
}
function onSubmit() {
var data = gel('notes').value;
// g_form.setValue('description', data);
// g_form.save();
return true;
}

 

processing script:

var text = notes;
createRecords();
function createRecords() {
var app = new GlideRecord("incident");
if(app.get(task_sys_id)){
app.description = text;
app.update();
}
}
//response.sendRedirect('incident.do?sys_id='+task_sys_id);
var urlOnStack = gs.getUrlOnStack();
response.sendRedirect(urlOnStack);

 

 

Please let me know any suggestions/ideas so that i can fix my script.

 

Thanks,

Niranjan.

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Niranjan,

You can achieve this without using the Processing script.

Use following

HTML

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:ui_form>
<g:evaluate var="jvar_sysid"
expression="RP.getWindowProperties().sysid"/> 
<table border="0" width="100%">
<tr>
<td>
<g:ui_multiline_input_field name="notes" label="update" mandatory="true" />
</td>
</tr>
<tr>
<td>
<g:dialog_buttons_ok_cancel cancel="return onCancel();" ok="return onSubmit();"/>
<input type="hidden" id="task_sys_id" name="task_sys_id" value="${jvar_sysid}"/>
</td>
</tr>
</table>
</g:ui_form>
</j:jelly>

Client

function onCancel() {
GlideDialogWindow.get().destroy();
return false;

function onSubmit() {

var data = gel('notes').value;

var sysId = gel('task_sys_id');

var app = new GlideRecord("incident");

if(app.get(sysId)){
app.description = data;
app.update();
}

window.open('/incident.do?sys_id='+sysId);
}

 

This will update the record and reload the incident form and you should see the update happened.

 

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, 

 

I tried your HTML and client script code. 
It doesn't seem to be working , it's redirecting on the ui page again.

@jitusingh 

Can you post a new question and share all the details and tag me there?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Tuba Ahmed
ServiceNow Employee
ServiceNow Employee

Hey,

UI pages are what we call 'Client Side' components. If one wants to make database updates via Client Side, one has to use the GlideAJAX functionality. Using GlideRecord in Client Script is erroneous. 

More information of GlideAJAX can be found here: https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference/r_GlideAjax.html

 

Some enhancements you can do in the script:

1. Instead of <g:evaluate var="jvar_sysid" expression="RP.getWindowProperties().sysid"/>, you can use <j:set var="jvar_sysid" value="$[RP.getWindowProperties().get('sysid')]"/> 

2. I don't think you can access "notes" directly as you have done on in the Processing script. Use gs.log(notes) and notice that an error shows up in the Sytem logs. 

3. Better to write Server-Side Code in a Script Include which will handle the updating. Use GlideAJAX to achieve this behavior. 

 

Hope you find this helpful.

Thanks,

Tuba Ahmed.