insert the array from object into the List type filed on the table

geet
Tera Guru
{"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:

geet_0-1696456233119.png

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:

 

geet_2-1696456435826.png

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.

 

 

1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

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'
};
Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

10 REPLIES 10

Vishal Birajdar
Giga Sage

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'
};
Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates