- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2023 02:57 PM
{"data":[{"id":"rs-4ebd12446515b108","name":"Google dev","tags":[787025,787027,787029],"typeAlias":"ssh","typeName":"ssh","hash":"a276a08591a55f2ae17718875d866b9e"},{"id":"rs-6d44ec9d651473a7","name":"dev cluster","tags":[787025,787026],"typeAlias":"aurora-postgres","typeName":"postgres","hash":"1236edc93afab533de3123eee4297d87"},{"id":"rs-771cc8536515b2b3","name":"Google prod","tags":[787027,787028,787029],"typeAlias":"sshCert","typeName":"sshCert","hash":"8ddea064d0d8974698db7d6234f5a1bc"},{"id":"rs-7f69cf376504c5d5","name":"dogfood db","tags":[787025],"typeAlias":"postgres","typeName":"postgres","hash":"f0bece9698a2ac23a123ed477c637fa8"}],"totalCount":4,"pageInfo":{"cursor":null,"hasNextPage":false}}
Hi All,
I have a List type field on a table and I am trying to pass the values coming from the the JSON response to that field but it is not working:
var STDMResourcesUTILS = Class.create();
STDMResourcesUTILS.prototype = {
initialize: function() {},
STDMGetResources: function(response_body) {
var responsebody = response_body;
for (var i = 0; i < responsebody.data.length; i++) {
var ResourceID = responsebody.data[i].id;
var ResourceName = responsebody.data[i].name;
var TypeAlias = responsebody.data[i].typeAlias;
var TypeName = responsebody.data[i].typeName;
var HashID = responsebody.data[i].hash;
var Resourcetags = responsebody.data[i].tags;
gs.info("priniting tags " +Resourcetags);
var gr = new GlideRecord('x_stdm_access_mgmt_resources');
gr.addQuery('resource_id', ResourceID);
gr.query();
if (!gr.next()) {
gs.info("##SI-when resource ID doesn't exist ");
gr.resource_id = ResourceID;
gr.resource_name = ResourceName;
gr.resource_tags = Resourcetags;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.insert();
} else {
gs.info("##SI-when resource ID exist " + ResourceID);
var existingHashID = gr.hash;
if (existingHashID != HashID) {
gr.resource_name = ResourceName;
gr.resource_tags = Resourcetags;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.update();
}
}
}
},
type: 'STDMResourcesUTILS'
};
Here is what I am getting in the output:
Here is the payload response:
Kindly guide me as my script is working fine for other fields but not for "resource tags" list type field.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2023 07:33 PM
Hi @geet
It's because array form json object not converted to string.
You can convert the array in string using toString()
var STDMResourcesUTILS = Class.create();
STDMResourcesUTILS.prototype = {
initialize: function() {},
STDMGetResources: function(response_body) {
var responsebody = response_body;
for (var i = 0; i < responsebody.data.length; i++) {
var ResourceID = responsebody.data[i].id;
var ResourceName = responsebody.data[i].name;
var TypeAlias = responsebody.data[i].typeAlias;
var TypeName = responsebody.data[i].typeName;
var HashID = responsebody.data[i].hash;
var Resourcetags = responsebody.data[i].tags.toString(); // convert it in string
gs.info("priniting tags " +Resourcetags);
var gr = new GlideRecord('x_stdm_access_mgmt_resources');
gr.addQuery('resource_id', ResourceID);
gr.query();
if (!gr.next()) {
gs.info("##SI-when resource ID doesn't exist ");
gr.resource_id = ResourceID;
gr.resource_name = ResourceName;
gr.resource_tags = Resourcetags;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.insert();
} else {
gs.info("##SI-when resource ID exist " + ResourceID);
var existingHashID = gr.hash;
if (existingHashID != HashID) {
gr.resource_name = ResourceName;
gr.resource_tags = Resourcetags;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.update();
}
}
}
},
type: 'STDMResourcesUTILS'
};
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2023 06:54 PM
Hi @geet
Convert the array to a comma seperated string and store it in the list type field like this.
In your script after storing the resource tags in to a variable Resourcetags add another line of code as shown below.
var Resourcetags = responsebody.data[i].tags; //Your existing line
Resourcetags = Resourcetags.join(','); //Add this extra line
It will work.
Please mark my answer helpful and accept as solution if it helped you 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 09:34 AM - edited 10-05-2023 09:44 AM
Hi @AnveshKumar M & @Vishal Birajdar
Thanks for your responses.
I tried your line of code and it worked but I am confused with other responses given below like Vishal suggested to use .toString() and that too make my code work. But first I will talk about your solution(Anvesh) as you replied first.
var STDMResourcesUTILS = Class.create();
STDMResourcesUTILS.prototype = {
initialize: function() {},
STDMGetResources: function(response_body) {
var responsebody = response_body;
for (var i = 0; i < responsebody.data.length; i++) {
var ResourceID = responsebody.data[i].id;
var ResourceName = responsebody.data[i].name;
var TypeAlias = responsebody.data[i].typeAlias;
var TypeName = responsebody.data[i].typeName;
var HashID = responsebody.data[i].hash;
var Resourcetags = responsebody.data[i].tags;
gs.info("priniting tags " +Resourcetags);
Resourcetags = Resourcetags.join(','); //added as you suggested
gs.info("priniting tags after join " +Resourcetags);
var gr = new GlideRecord('x_stdm_access_mgmt_resources');
gr.addQuery('resource_id', ResourceID);
gr.query();
if (!gr.next()) {
gs.info("##SI-when resource ID doesn't exist ");
gr.resource_id = ResourceID;
gr.resource_name = ResourceName;
gr.resource_tags = Resourcetags;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.insert();
} else {
gs.info("##SI-when resource ID exist " + ResourceID);
var existingHashID = gr.hash;
if (existingHashID != HashID) {
gr.resource_name = ResourceName;
gr.resource_tags = Resourcetags;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.update();
}
}
}
},
type: 'STDMResourcesUTILS'
};
Here are the logs:
If you notice the 2 highlighted logs --> The log with "Printing Tags" has the same value as "Printing tags after join". How can we understand the difference between the 2 logs I am printing because both are giving comma separated values only? Kindly guide me on that part.
Also, the table successfully added the tags as follows, sharing 1 of the record:
................................................................................................................................
Now sharing the logs of what @Vishal Birajdar has suggested.
Code is below:
var STDMResourcesUTILS = Class.create();
STDMResourcesUTILS.prototype = {
initialize: function() {},
STDMGetResources: function(response_body) {
var responsebody = response_body;
for (var i = 0; i < responsebody.data.length; i++) {
var ResourceID = responsebody.data[i].id;
var ResourceName = responsebody.data[i].name;
var TypeAlias = responsebody.data[i].typeAlias;
var TypeName = responsebody.data[i].typeName;
var HashID = responsebody.data[i].hash;
var Resourcetags = responsebody.data[i].tags;
gs.info("priniting tags " +Resourcetags);
var ResourcetagsString = Resourcetags.toString(); //added what vishal suggested.
gs.info("priniting tags after adding tostring " +ResourcetagsString);
var gr = new GlideRecord('x_stdm_access_mgmt_resources');
gr.addQuery('resource_id', ResourceID);
gr.query();
if (!gr.next()) {
gs.info("##SI-when resource ID doesn't exist ");
gr.resource_id = ResourceID;
gr.resource_name = ResourceName;
gr.resource_tags = ResourcetagsString;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.insert();
} else {
gs.info("##SI-when resource ID exist " + ResourceID);
var existingHashID = gr.hash;
if (existingHashID != HashID) {
gr.resource_name = ResourceName;
gr.resource_tags = ResourcetagsString;
gr.type_alias = TypeAlias;
gr.type_name = TypeName;
gr.hash = HashID;
gr.update();
}
}
}
},
type: 'STDMResourcesUTILS'
};
Logs are as below:
My question would be same here if you look at the logs, what is the difference between these 2 logs when output is the same even after using .toString().
Here is the output of the same in the table:
Please guide me what is the best method "join()" or "toString".
Why the outputs are the same even when we use both the functions?
JFYI: The JSON response I am receiving from a REST API call I am making in Flow action as follows:
Then I am passing this response in a script include
.......................................................................................................................................
I know the difference b/w the both but still need to understand how to make logging better:
1) JavaScript toString() method
toString() method is not only for the arrays, but it can be used other types of objects also, it is used to convert an object’s value to the string. The values are separated by the commas.
Syntax:
object.toString();
2) JavaScript join() method
join() method is an array method and it can be used with the arrays. It is used to join the array elements and returns a string. The values are separated by the commas (by default), using join() method we can also specify the separator.
Syntax:
array.join([separator]);
Here, separator is an optional parameter, which defines the separator between the array elements in the string.
Examples:
Input: var arr = ["Manju", "Amit", "Abhi", "Radib"]; Function call Output arr.toString() "Manju,Amit,Abhi,Radib" arr.join() "Manju,Amit,Abhi,Radib" arr.join(" ") "Manju Amit Abhi Radib"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 09:54 AM
Hi @geet
Both array.join() & toString() will work as same.
It's upto you as developer which method you want to use.
That's it....!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 09:56 AM
Could you please guide on that logging part question that I have asked above.