Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Get field value of GlideRecord (best practice method)

xiaix
Tera Guru

var gr = new GlideRecord('incident');

gr.addQuery('active', 'true');

gr.query();

if (gr.next())

{

  // This way ?

  gr.sys_id;

  // Or this way ?

  gr.getValue('sys_id');

}

Which is safest/best method to get the value?   Dotwalk the gr object or use the built-in method, getValue() ?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

I prefer gr.getValue('sys_id');



gr is a pointer. If you pop the sys_ids in to an array, you get a list of all of the same values (of the last record.)   getValue() copies the value rather than using the reference.



getValue() has saved my life more times than I care to admit. While it's not perfect, it's often the better option to ensure you have the value (especially in loops.)



As a general rule, getters/setters are a better way to go in object oriented programming.


View solution in original post

7 REPLIES 7

Chuck Tomasi
Tera Patron

I prefer gr.getValue('sys_id');



gr is a pointer. If you pop the sys_ids in to an array, you get a list of all of the same values (of the last record.)   getValue() copies the value rather than using the reference.



getValue() has saved my life more times than I care to admit. While it's not perfect, it's often the better option to ensure you have the value (especially in loops.)



As a general rule, getters/setters are a better way to go in object oriented programming.


I concur with your opinion and suggestion.   Thank you for taking the time to explain.  


And I just found this too, which backs up your suggestion:   Coding Best Practices - ServiceNow Wiki



find_real_file.png


He he. It backs up what I said because I wrote them both. 🙂