which is best and efficient method, kindly suggest

Supriya25
Tera Guru

Hi all,

I request you please suggest me which is best and efficient methods for long run ???

gr.fieldname;

gr.fieldname.toString();

gr.getValue('fieldName');

 

 

if -statement Reference field /String

Assigned to : Ref field  in Scoped Aplication

if(global.JSUtil.nil(gr.assigned_to))     Or

if(global.JSUtil.nil(gr.assigned_to.toString()))   Or

if(global.JSUtil.nil(gr.getvalue('assigned_to'))) 

Assigned to : Ref field  in Global Aplication

if(gr.assigned_to)     Or  if(gr.assigned_to.toString())   Or  if(gr.getvalue('assigned_to')) 

 

 

 

Total count : string field on form

if(gr.total_count >1)  Or if(gr.total_count.toString() >1)   or if(gr.getValue('total_count') >1)  

 

 

 

 Date and Time field IF -Statement

if(gr.received_on) Or if(gr.getValue('received_on')) Or if(gr.received_on.toString())

in Scoped application :  if(global.JSUtil.notNil(gr.received_on)) Or if(global.JSUtil.notNil(gr.getValue('received_on')) Or if(global.JSUtil.notNil(gr.received_on.toString()))

 

 

 

 passing value :

gr.comments = gr.assigned_to Or

gr.comments = gr.assigned_to.toString() Or

gr.comments = gr..getValue('assigned_to');

 

 

Date/timeField GlideDateTime

var g = new GlideDateTime(gr.received_on)   Or new GlideDateTime(gr.received_on.toString())  Or new GlideDateTime(gr.getValue('received_on'))

2 REPLIES 2

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Supriya25 ,

 

Choosing the best method depends on your specific use case and requirements. However, here are some considerations for each scenario:

 

1. **Field Name:**

   - `gr.fieldname;` is straightforward and efficient.

 

2. **Check if Assigned:**

   - For Scoped Application: `if(global.JSUtil.notNil(gr.received_on))` is preferable for null/undefined checks.

   - For Global Application: `if(gr.assigned_to)` is concise if you only need a truthiness check.

 

3. **Total Count:**

   - `if(gr.total_count > 1)` is concise and suitable if 'total_count' is expected to be a numeric field.

 

4. **Date and Time Field:**

   - `if(gr.received_on)` is suitable for Scoped Application. 

   - `if(global.JSUtil.notNil(gr.received_on))` is safer for avoiding null/undefined issues.

 

5. **Passing Value to Comments:**

   - `gr.comments = gr.assigned_to;` is straightforward.

 

6. **GlideDateTime:**

   - `var g = new GlideDateTime(gr.received_on);` is efficient and clear for creating a GlideDateTime object.

 

In general:

- Use direct field access when possible for simplicity.

- Consider null/undefined checks, especially in Scoped Applications.

- Ensure the chosen method aligns with your data types and expected outcomes.

 

It's essential to balance readability, efficiency, and adherence to best practices based on your platform's guidelines and your team's coding standards.

 

Thanks,

Danish

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @Supriya25,

 

Let me give you my view about this.

 

gr.field_name : This will return the Glid Element of the object based on data type of field and more over it returns the reference to the value instead of the actual value.

 

If you want to follow strict type comparision this will help you.

 

The major issue that we face with this approach is, if you use this approach inside a loop and want to store the values in an array, it will not give you the expected result. For example try the following script in  background Script of a sub prod instance and observe the output you will get same state for all incidents, that's not correct.

 

var incGr = new GlideRecord('incident');

incGr.setLimit(5);

incGr.query();

var states = [];

while(){

   states.push(gr.state);

}

gs.print(states);

 

gr.getValue('field_name'): This will give you the string version of the actual value that is stored in the database instead of the actual type. And this will give you the value instead of reference.

 

This will be useful most of the times if you don't want to perform strict comparison, but compare everything like a string. You can avoid the issue that we had in the first approach when used inside a loop.

 

The issue with this is, if you want the original data type, you need to convert it back.

 

 Try this Script,

 

var incGr = new GlideRecord('incident');

incGr.setLimit(5);

incGr.query();

var states = [];

while(){

   states.push(gr.getValue('state'));

}

gs.print(states);

 

gr.field_name.toString(): This will almost produce the similar result as getValue approach. But if you want to retrieve the value of dot walked fields, use this approach instead of getValue.

 

Finally

 

For single loop (no looping over the GlideRecord variable) and strict comparison use gr.field_name.

 

For consistent results even inside the loops, with no strict data type comparison, use gr.getValue('field_name'). If your target is a dot walked fields, use gr.field_name.dot_walked_field.toString().

 

 

For date time/date fields, use getValue method and construct back the GlideDateTime / GlideDate object like this,

 

var date_time = gr.getValue('field_name');

var date_time_object = new GlideDateTime(date_time); // By default it will be in UTC time zone.

 

Please mark my answer helpful and accept as a solution if it helped 👍

Thanks,
Anvesh