Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help on getting the length of object element

shaik_irfan
Tera Guru

Hi,

 

Below is the script which i have written i am trying to get the legnth of objects element but i am not getting correct result

var arr = [];
var serverName = "c56c1c49db7657c4375d5458dc961965";
//var reqFor = current.variables.Requested_for;
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child', serverName);
gr.addQuery('parent.sys_class_name', 'u_application');
gr.query();
while(gr.next()){
var obj = {};
	obj.name = gr.parent.u_accountable_application_owner.getValue().toString() +',' + gr.parent.u_technical_owner.getValue().toString(); //Concatenating reference and list type fields in obj name
		arr.push(obj);
}

gs.print(JSON.stringify(arr));
arr = JSON.stringify(arr);
var sp = arr.split(",");
gs.print(sp.length);

 

Result:

*** Script: [{"name":"557e1f107bb300002e3ddb30aa4d4dce,c77e93907bb300002e3ddb30aa4d4d0b"},{"name":"557e1f107bb300002e3ddb30aa4d4dce,efe87b277bd0d0002e3ddb30aa4d4d68,1f7e9fdc7b7300002e3ddb30aa4d4d3f"}]
*** Script: 5

I am expecting the length to get as 2 since i have two name objects within the array. Please help me how to acheive this
1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Did you also try arr.length? Instead of sp.length? sp.length being 5 seems fair. Though you are already interested in arr I think.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

1 REPLY 1

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Did you also try arr.length? Instead of sp.length? sp.length being 5 seems fair. Though you are already interested in arr I think.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn