Why is gr.update() duplicating the current record and not updating?

Ulrich Matthew
Tera Contributor

When i click on ok, the record I'm working with is being duplicated and not updated

 

 

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

function onSubmit() {
    var reason = document.getElementById('termination_reason').value;
    var date = document.getElementById('termination_date').value;
    var sysId = g_form.getUniqueValue();
    /*g_form.setValue('u_reason', reason);
	g_form.setValue('u_termination', date);

	g_form.save();
*/

    var employees = new GlideRecord('u_itechag_employees');
    employees.addQuery('sys_id', sysId);
    employees.query();


    if (employees.next()) {
        employees.u_reason = reason;
        employees.u_termination = date;
        employees.update();
    }


    location.reload();

    return true;
}

1 ACCEPTED SOLUTION

var terminate_employee = Class.create();
terminate_employee.prototype = Object.extendsObject(AbstractAjaxProcessor, {
Terminate:function()
{ 
 var employees = new GlideRecord('u_itechag_employees');
    employees.addQuery('sys_id', this.getParameter('sysparm_id'));
    employees.query();


    if (employees.next()) {
        employees.u_reason = this.getParameter('sysparm_reason');
        employees.u_termination = this.getParameter('sysparm_date');
        employees.update();
    }
},
    type: 'terminate_employee'
});

Replace the script below and try 

please mark my answer correct if it helps you

View solution in original post

32 REPLIES 32

<?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="termination_reason" label="Termination Reason" mandatory="true" />
				</td>
			</tr>
			<tr>
				<td>
					<g:ui_date id="termination_date" name="termination_date" label="Termination Date" />
					
				</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>

 

is coded like this 

okay this should also work ideally but not sure what is happening I tried this in my instance and its working  properly

Please check harshavardhans answer in below link on the same issue which says g_form.setValue()

https://community.servicenow.com/community?id=community_question&sys_id=ef94fc9edbdd5cd05ed4a851ca96...

But it can be done in another way  lets go with below route which is a workaround

 lets go with glide ajax then .Place this script below var sys_id= g_form.getUniqueValue()

var ga = new GlideAjax('your script include name');//this is the script include
ga.addParam("sysparm_name", "your function name"); //this is the function within the script include
ga.addParam("sysparm_user", g_form.getUniqueValue());
ga.addParam('sysparm_reason',reason);
ga.addParam('sysparm_date',date);
ga.getXML(getResponse);

function getResponse(response) {
// do nothing
}

Create a script include like below

script incude:(client callable) 

your function name:function()
{ 
 var employees = new GlideRecord('u_itechag_employees');
    employees.addQuery('sys_id', this.getParamter('sysparm_id'));
    employees.query();


    if (employees.next()) {
        employees.u_reason = this.getParameter('sysparm_reason');
        employees.u_termination = this.getParameter('sysparm_date');
        employees.update();
    }
},

 

function Terminate ()
{ var employees = new GlideRecord('u_itechag_employees');
    employees.addQuery('sys_id', this.getParamter('sysparm_id'));
    employees.query();


    if (employees.next()) {
        employees.u_reason = this.getParamter('sysparm_reason');
        employees.u_termination = this.getParamter('sysparm_date');
        employees.update();
    }
}

 

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

function onSubmit() {
    var reason = document.getElementById('termination_reason').value;
    var date = document.getElementById('termination_date').value;
    var sysId = g_form.getUniqueValue();
    /*g_form.setValue('u_reason', reason);
	g_form.setValue('u_termination', date);

	g_form.save();
*/

    /*var employees = new GlideRecord('u_itechag_employees');

    employees.addQuery('sys_id', sysId);
		    employees.query();




    if (employees.next()) {

        employees.u_reason = reason;
        employees.u_termination = date;
        employees.update();




    }*/
var ga = new GlideAjax('terminate_employee');//this is the script include
ga.addParam("sysparm_name", "Terminate"); //this is the function within the script include
ga.addParam("sysparm_user", g_form.getUniqueValue());
ga.addParam('sysparm_reason',reason);
ga.addParam('sysparm_date',date);
ga.getXML(getResponse);

function getResponse(response) {
// do nothing
}


    location.reload();

    return true;
}





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

 

 

am i doing it right? i think its not reaching the script include, wrote a console.log there and had no response 

 

sorry for bring this newby hahahha

No console log wont work in script include you need to use gs.info

But send the code which you wrote in script include lets debug

the SI is the one at the start of the comment