Difference between gr.getValue('fieldname') vs gr.fieldname vs gr.fieldname.getValue()

JohnnySnow
Kilo Sage

Hi Team,

 

I have read few posts to understand the difference between gr.getValue('fieldname') vs gr.fieldname vs gr.fieldname.getValue() but I'm not clear when to you or what the advantages are of using one over the other.

 

  var gr = new GlideRecord("incident");
    var logQ = [];
gr.addEncodedQuery("opened_at>=javascript:gs.dateGenerate('2022-11-14','16:36:44')");
    gr.orderByDesc("number");
    gr.query();
    while (gr.next()) {
        logQ.push("The Priority is " + gr.priority + " The Getvalue is " + gr.getValue("priority") + " " + gr.priority.getValue());
    }

for(i=0;i<logQ.length;i++){
gs.print(logQ[i]);
}

In the above code, when I printed the values, all 3 brought in proper backend values, so I would appreciate if you can help me understand.

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.
2 REPLIES 2

KaviBharathi D
Tera Contributor

Hello this link will be helpful to understand why we use getValue ().

https://www.servicenow.com/community/developer-blog/tnt-the-importance-of-using-quot-getvalue-quot-w...

 

Manmohan K
Tera Sage

Hi @JohnnySnow ,

 

gr.getValue('fieldname') gives you the string representation of the field while  gr.fieldname returns the GlideElement object (depending on field type it can vary from string,integer etc)

 

Refer below link for more details

https://www.servicenow.com/community/developer-blog/tnt-the-importance-of-using-quot-getvalue-quot-w...