The CreatorCon Call for Content is officially open! Get started here.

What are good practice to handle undefined ,null and empty errors while doing gliderecord

Rajyalakshmi
Tera Expert

What are good practice to handle undefined ,null and empty errors while doing gliderecord in servicenow

4 ACCEPTED SOLUTIONS

Nilesh Pol
Tera Guru
Tera Guru

@Rajyalakshmi here is some of examples to handle undefined ,null and empty errors while doing gliderecord

1.  Before using a GlideRecord, ensure it actually found something, Always wrap logic inside if (gr.next()) or while (gr.next()).

2. Sometimes a record exists, but a specific field may be null, undefined, or empty, then  gs.nil(value) safely checks if a variable is null, undefined, or an empty string ("").

3. When dealing with dynamic tables or uncertain fields use GlideRecord.IsValidRecord() or IsValidField() that will Prevents “invalid field name” or “undefined” errors at runtime.

4. Add clear logs for missing/empty data during development.

Summery:

Case Recommended Practice
Check record existence if (gr.next()) or if (gr.get(sys_id))
Check null/empty if (!gs.nil(value))
Default value `var val = gr.field
Validate record gr.isValidRecord()
Validate field gr.isValidField('field_name')
Handle JSON safely try { JSON.parse() } catch(e)
Avoid direct field access Always confirm existence first

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Rajyalakshmi 

what's your actual context or business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

Nawal Singh
Giga Guru

Hi @Rajyalakshmi ,

 

Please review the below link that will definitely answer your query-

 

https://www.servicenow.com/community/developer-articles/performance-best-practices-for-server-side-c...

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

View solution in original post

Hi @Ankur Bawiskar ,

Thank you for update. Just for understanding and to get good practice while development.

 

Regards,

Rajyalakshmi

 

View solution in original post

5 REPLIES 5

Nawal Singh
Giga Guru

Hi @Rajyalakshmi ,

 

Please review the below link that will definitely answer your query-

 

https://www.servicenow.com/community/developer-articles/performance-best-practices-for-server-side-c...

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh