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

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

 

Hi @Danish Bhairag2, thanks for wonderful answer

Samaksh Wani
Giga Sage
Giga Sage

Hello @sieusaopolo15 

 

.get() is used for returning a specific element among all the elements.

 

So when we used if statement for .next() function it checks the parameter into the filtered object i.e gr.

 

gr.next() will check the record in ,get() function.

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

Hi @Samaksh Wani 

That is not quite correct. .get() will fetch more than one record, you then need to use .next() to iterate.

From the docs (https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc...)
"If multiple records are found, use next() to access the additional records."


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

Best
Daniel