getGlideObject() Usage

Kalaiarasan Pus
Giga Sage

So, this is a interesting question and I haven't noted this earlier.

I was answering on one of the thread and used getGlideObject() to add few years using another field of the same table.

var gr = new GlideRecord('alm_hardware');

gr.query('asset_tag', 'P1000479');

if (gr.next()) {

var warranty =   gr.assigned.getGlideObject();

warranty.addYears(1);

gr.install_date=warranty;

gr.setWorkflow(false);

gr.update();

}

The above script looks very innocuous. But instead of updating install date alone, it also updated the assigned date.

Is it because we are actually manipulating on the original glideobject within the current scope instead of a localized value?

1 ACCEPTED SOLUTION

Hi Kalai,



I believe this happens because of object inheritance, which causes any change to an object to also change any object it references through its prototype chain.


When you stored the object in warranty, it still contains the reference to the original object, thus it also gets changed.


View solution in original post

5 REPLIES 5

Kalaiarasan Pus
Giga Sage

Bump. Anyone ? pradeepksharma mguy Can you guys take a look at this question?


I think, this is just a guess to be fair, that because you are getting the 'assigned' object and then you are adding a year to it, you are actually telling the tool to update that object.



what maybe you could do is


var gdt = new glideDateTime(gr.assigned);


GlideDateTime - ServiceNow Wiki


so that declares gdt as the object, not the actual field and then you can do the addYears to gdt and apply it to install_date.



Marc


Thank you for the response.


At the end, I did do as suggested by you but was just curiously and surprised with the way getGlideObject object worked ..


Hi Kalai,



I believe this happens because of object inheritance, which causes any change to an object to also change any object it references through its prototype chain.


When you stored the object in warranty, it still contains the reference to the original object, thus it also gets changed.