Is substring() method available in the String object in non global scope?

SwarnadeepNandy
Mega Sage

I am using below code in some non global   application scope

current.floor   =   parseInt(current.room.substring(0,1)); // where 'room' is string type and 'floor' is integer type.

but it is not working, i am getting error

find_real_file.png

but when i am using

current.floor = parseInt(current.room.toString().substring(0,1));

it is working perfectly fine.

Why do i need to use toString() for a string type variable?

5 REPLIES 5

Rushit Patel2
Tera Guru

Hi Swarnadeep,



current is reference of GlideRecord. so it is a object. so when u do current.anyfield it returns propertiy of object..


see below image.


find_real_file.png


short descripton has type string but it will return Object. see below



find_real_file.png


so u must use toString() method to convert it to proper String.



I hope it helps.



mark correct/helpful/like based on impact.



Regards,


Rushit Patel


Hi Rushit,


Try running the below code



var gr = new GlideRecord('incident');


gr.addQuery('number', 'INC0000055');//take any incident number for any particular


gr.query();


while(gr.next()) {


  gs.log(typeof gr.short_description);


}



this will also return "object".



var a = new String('abc');


gs.log(typeof a); // it will return object.



While,


var a = 'abc';


gs.log(typeof a);//it will return string, because it is primitive.



This is because short_description might not be a primitive string. Its an object of wrapper class String() and String() class has a toString() method overriden to that of Object class. And moreover String() class also has substring() method.


So my question is why it is not using that method, it might be because of application scope issue. Restricted access in non global application scope might be the issue.



Thanks & Regards,


Swarnadeep Nandy


I checked in global application scope and it is working. I mean the substring() method for short description.


gs.addInfoMessage(current.short_description.substring(0,10));// It will return the first 10 letters from the short description.


Yeah man..i checked it too. this is super wierd. there are lots of wierd thing about scoped API,im not surprised.!