Difference b/w sys_id and sys_id.toString , what it will returns

Kishore8
Kilo Guru

Please give me clear view about Difference b/w sys_id and sys_id.toString() , what it will returns

var gr1 = new GlideRecord('core_company');

gr1.addQuery('name', u_manufacturing);

gr1.query();

var gr = new GlideRecord('cmdb_ci');

gr.addQuery('manufacturer', gr1.sys_id);

gr.query();

var arr = [];

while (gr.next()) {

arr.push(gr.sys_id.toString());

}

return "sys_idIN" + arr.toString();

}

1 ACCEPTED SOLUTION

PriyaRanji
Tera Guru

Hi Kishore,



Good Day



Please find the below details, hope it will helps you out!



Case 1 : sys_id.toString()



Suppose you are writing a function that selects the sys_id's of manufactures and stores them in an array. The array could be used to pass on to another function.



If we use arr.push(gr.sys_id); in your script and let us use a test function to show the results, limiting the result set to five records:



The function will print five identical sys_id's, rather than the five different ones you might expect.



1st Iteration :-



gr.sys_id is a pointer to a GlideElement object. Pushing gr.sys_id on the array during the first iteration of the while-loop then the first element of the array, result[0], points to the gr object's sys_id property, which is a GlideElement object.




2nd Iteration :



The second iteration of the while-loop is started by calling gr.next(). This loads the data for the next record in the dataset. The gr object will remain at the same position in memory, as will all of the GlideElement-objects it refers to. Rather than destroying all GlideElements and recreating them, ServiceNow keeps the objects and fills them with the new data. This means gr.sys_id will yield the sys_id of the second record from the dataset, even though the GlideElement it points to is still the same.



The first element of the array, result[0], was not changed. It still points to the gr object's sys_id. But during the gr.next-call, the contents of this object were changed. It now contains the sys_id of the second record from the dataset. So without actually touching result[0], its value changed nonetheless, and is now equal to result[1].



3rd Iteration :-



In the same manner, the third iteration will result in the first three array elements all pointing to (still the same) GlideElement object, which now contains the sys_id of the third record in the dataset. Ultimately in the example (5 records), after five iterations, the array will contain five elements, all of which refer to the sys_id of the same (fifth) record.



Solution - sys_id.toString() :-



The trick is to store something else than the reference in the array. If we were to retrieve the sys_id's string value from the GlideElement object, we would create a new string. This value would not change when calling next() on the GlideRecord, because the gr object has no knowledge of the new string's existence. So instead of


arr.push(gr.sys_id);



push the string-converted value on the array. Use whichever of the three options has your preference:



1. arr.push("+gr.sys_id);


2. arr.push(gr.sys_id.toString());


3. arr.push(gr.getValue('sys_id'));



Change the line in the script and run it again; the script will now print five different id's.



Please refer the below url for more information!



https://www.servicenowguru.com/system-definition/relationships/defined-related-lists/



Case 2 :- sys_id



It will returns you the sys_id's of the specific record.



Please Mark it as Correct/like/comment based on the impact of the response provided by me. Hope it will helps you



Thanks,


Priyanka R


View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kishore,



toString() is mostly used with sys_id while pushing into an array since array functionality requires toString() to separate out array elements. If you don't use toString() while pushing into array the last element of the array will be repeated.


Also while populating a reference field it is recommended to use toString() before populating it.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Deepak Ingale1
Mega Sage

toString() uses casting and it is quite a slower method.


Better way is to use getValue() method, eg. gr1.getValue('sys_id') . getValue() gives you the string always.


Just don't forget getDisplayValue() for reference fields.


PriyaRanji
Tera Guru

Hi Kishore,



Good Day



Please find the below details, hope it will helps you out!



Case 1 : sys_id.toString()



Suppose you are writing a function that selects the sys_id's of manufactures and stores them in an array. The array could be used to pass on to another function.



If we use arr.push(gr.sys_id); in your script and let us use a test function to show the results, limiting the result set to five records:



The function will print five identical sys_id's, rather than the five different ones you might expect.



1st Iteration :-



gr.sys_id is a pointer to a GlideElement object. Pushing gr.sys_id on the array during the first iteration of the while-loop then the first element of the array, result[0], points to the gr object's sys_id property, which is a GlideElement object.




2nd Iteration :



The second iteration of the while-loop is started by calling gr.next(). This loads the data for the next record in the dataset. The gr object will remain at the same position in memory, as will all of the GlideElement-objects it refers to. Rather than destroying all GlideElements and recreating them, ServiceNow keeps the objects and fills them with the new data. This means gr.sys_id will yield the sys_id of the second record from the dataset, even though the GlideElement it points to is still the same.



The first element of the array, result[0], was not changed. It still points to the gr object's sys_id. But during the gr.next-call, the contents of this object were changed. It now contains the sys_id of the second record from the dataset. So without actually touching result[0], its value changed nonetheless, and is now equal to result[1].



3rd Iteration :-



In the same manner, the third iteration will result in the first three array elements all pointing to (still the same) GlideElement object, which now contains the sys_id of the third record in the dataset. Ultimately in the example (5 records), after five iterations, the array will contain five elements, all of which refer to the sys_id of the same (fifth) record.



Solution - sys_id.toString() :-



The trick is to store something else than the reference in the array. If we were to retrieve the sys_id's string value from the GlideElement object, we would create a new string. This value would not change when calling next() on the GlideRecord, because the gr object has no knowledge of the new string's existence. So instead of


arr.push(gr.sys_id);



push the string-converted value on the array. Use whichever of the three options has your preference:



1. arr.push("+gr.sys_id);


2. arr.push(gr.sys_id.toString());


3. arr.push(gr.getValue('sys_id'));



Change the line in the script and run it again; the script will now print five different id's.



Please refer the below url for more information!



https://www.servicenowguru.com/system-definition/relationships/defined-related-lists/



Case 2 :- sys_id



It will returns you the sys_id's of the specific record.



Please Mark it as Correct/like/comment based on the impact of the response provided by me. Hope it will helps you



Thanks,


Priyanka R