Fix script get/update issue (RESOLVED)

mikeearnheart
Kilo Contributor

Instance: Kingston  Patch 14

I am attempting to update a 'Receiving Slip Line Item' using a 'Fix Script'.

Script is as follows:

var rcli = new GlideRecord('proc_rec_slip_item');
rcli.get('RCL9999999');
rcli.query();
while (rcli.next())
	{
		gs.log('Receiving Slip Item: ' + rcli.sys_id,'me');
		gs.log('Serial Numbers: ' + rcli.u_serialnumbers, 'me');
		gs.log('Quantity: ' + rcli.quantity, 'me');
		rcli.u_serialnumbers = 'ACCC123456788|ACCC123456789';
		rcli.quantity = 2;
		rcli.update();
	}

Upon execution I get the following results:

[Domain Paths] Query against table proc_rec_slip_item restricted by domain values [ABC[<SYS_ID], global]
me: Receiving Slip Item: <SYS_ID>
me: Serial Numbers: ACCC123456788|ACCC123456789|ACCC1234567810
me: Quantity: 3
GlideSession message was modified by sanitization. [message=Unique Key violation detected by database (Duplicate entry &apos;<SYS_ID>&apos; for key &apos;PRIMARY&apos;)][sanitized=Unique Key violation detected by database (Duplicate entry &#39;<SYS_ID>&#39; for key &#39;PRIMARY&#39;)]
FAILED TRYING TO EXECUTE ON CONNECTION 4: INSERT INTO proc_rec_slip_item (`u_serialnumbers`, `quantity`, `purchase_line`, `sys_mod_count`, `u_isserialized`, `received`, `sys_updated_on`, `sys_domain_path`, `number`, `sys_id`, `received_by`, `sys_updated_by`, `receiving_slip`, `sys_created_on`, `sys_domain`, `sys_created_by`) VALUES('ACCC123456788|ACCC123456789', 2, '<SYS_ID>', 0, 1, '2019-04-24 15:13:15', '2019-04-25 20:54:01', '!!!/!!!/', 'RCL99999', '<SYS_ID>', '<SYS_ID>', '<EMAIL>', '<SYS_ID>', '2019-04-25 20:54:01', '<SYS_ID>', '<EMAIL>') /* abc021, gs:<SYS_ID>, <SYS_ID>*/
Unique Key violation detected by database (Duplicate entry '<SYS_ID>' for key 'PRIMARY')
: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '<SYS_ID>' for key 'PRIMARY': org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:132)

Based on the error statement it looks like its attempting to INSERT a duplicate record.

In the script I am executing an UPDATE.

WHY is it trying to execute an INSERT?

1 ACCEPTED SOLUTION

vkachineni
Kilo Sage
Kilo Sage
Gr.get function takes in a sys_id and not a number. Try passing in sys_id.
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

7 REPLIES 7

Also FYI when you use gr.get() you must pass in the sys_id of the record you are trying to receive. You can also specify a different unique field as in your case RCL9999999 but when you use anything besides sys_id you must specify the field to search as the first parameter like so gr.get('number', 'RCL9999999').

When you use gr.get() there also is no query() or next() methods as the system will retrieve the record specified. You just call gr.get() and assuming the record exists then you will have immediate access to the returned record object. Now to answer why you are seeing 'INSERT' errors. When you use the gr.get() function if the system does not find the record it will create one using the passed in single parameter as the new record sys_id. It's best practice to wrap gr.get() functions in IF statements like so:

if(gr.get(SYS ID)){

//Do something with gr

}

This way new records will not be created if not found using gr.get().

A-N
Tera Expert

Try this out...

var rcli = new GlideRecord('proc_rec_slip_item');
if(rcli.get('RCL9999999')){	
		gs.log('Receiving Slip Item: ' + rcli.sys_id,'me');
		gs.log('Serial Numbers: ' + rcli.u_serialnumbers, 'me');
		gs.log('Quantity: ' + rcli.quantity, 'me');
		rcli.u_serialnumbers = 'ACCC123456788|ACCC123456789';
		rcli.quantity = 2;
		rcli.update();
	}

mikeearnheart
Kilo Contributor

I can confirm that both options worked.

gr.get(<sys_id>)

and 

gr.get('number','RCL99999')

will update as expected.

Thanks,

for your help!