GlideRecord.get() is not checking record exists or not exists ?

sieusaopolo15
Tera Guru

Hi,

I'am using GlideRecord.get() to get single record and I have found this behavior strange happened during writing logic for my handler. If I put the an non-existence sys_id to the GlideRecord.get(), result of that function is an new object is created with the non-existence sys_id I put in ? And it get even worst when at the end of that process there is an GlideRecord.update(), the function automatically insert an new record to my desired table. Are these behaviors normal when using this function ? Also, is there any alternative function to get single record rather than using GlideRecord.get() ?
Below here are attachments of functions behaviors:

- Object is created with non-existence sys_id:

sieusaopolo15_0-1697259463013.png

- An object is created when using gr.update():

sieusaopolo15_1-1697259614813.png

sieusaopolo15_3-1697259650760.png

 

 

 

 

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @sieusaopolo15 ,

 

Always use an if condition whenever u want to fetch 1 record. U can try with below code.

 

var gr = new GlideRecord('u_testing_table_api');

if(gr.get('enter your sys Id here'){

gs.info('inside if');

}else{

gs.info('inside else');

}

 

Mark my answer helpful & accepted if it helps you resolve your query.

 

Thanks,

Danish

 

View solution in original post

5 REPLIES 5

Daniel Oderbolz
Kilo Sage

Unfortunately, .get() is very unsafe when you use the version with 2 parameters. 

In this case, it does not check if the field you are querying on exists and still returns true (and selects a random record!).

I can reproduce this even with Washington D.C patch2-hotfix1:

 

 

var str_number = 'TSK0008985';

var gr= new GlideRecord('task');
if (gr.get('an_impossible_field_name',str_number))
{
gs.print('all good?');
gs.print(gr.getValue('sys_id'));
}

// Unfortunately, this returns 'all good?' and a random sys_id.

 

I created a case for this and demand it to be fixed.

Best
Daniel


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel