How to convert Object to string and load List collector

AnilM99
Tera Expert

Hi Team,

I have a requirement 

I am using scripted rest API, the incoming payload like this ["Anil","Abeltuter","Androw"];

I want convert this to string and load List collector

 

help me on the same

AnilM

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

 

You should be able to take the payload object and add: .toString() and then simply set your list collector to that value as it accepts a comma separated list anyway. Keep in mind that the list collector would need appropriate values, so if those names in your payload are actual values, then that's find, if not, and it's a reference to the sys_user table, for example, then you'd want to set the list collector to a comma separated list of sys_ids, instead, for example.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hi,

 

You should be able to take the payload object and add: .toString() and then simply set your list collector to that value as it accepts a comma separated list anyway. Keep in mind that the list collector would need appropriate values, so if those names in your payload are actual values, then that's find, if not, and it's a reference to the sys_user table, for example, then you'd want to set the list collector to a comma separated list of sys_ids, instead, for example.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Amit Gujarathi
Giga Sage
Giga Sage

Hi @AnilM99 ,
I trust you are doing fine.

Here's one way you can accomplish this in ServiceNow:

  1. First, you'll need to create a new Scripted REST API resource. Go to "System Web Services" > "Scripted REST APIs" and click on "New".

  2. Give your new API a name and a relative path. For example, you could name it "ConvertToLC" and give it a relative path of "/convert-to-lc".

  3. In the "Script" tab of your new API, you can write the code to handle the incoming payload. Here's an example script that converts the incoming array to a comma-separated string and loads it into a List collector:

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    // Get the incoming payload from the request body
    var payload = request.body.data;

    // Convert the payload array to a comma-separated string
    var payloadString = payload.join(',');

    // Create a new List collector and set its value to the payload string
    var lc = new GlideListCollector();
    lc.setValue(payloadString);

    // Return a success message and the List collector object
    return {
        message: 'Payload converted successfully',
        lc: lc
    };
})(request, response);

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi