Script include return only one line

tsoct
Tera Guru

Hello, everyone.
I tested the identical set of scripts from the script background and was given a list of results (which is correct)

tsoct_0-1720625242062.png

 

However, when the same script is used in script include, it just returns only the first line.

tsoct_1-1720625341686.png

 

What might have gone wrong?

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi @tsoct,

 

If you want to return multiple data points from the Script Include, within your script include you'll need create an array to store the multiple values, then load the value for each pass of the loop, and then return this array (which is storing the multiple values).

 

Script Include syntax similar to the below:

var answers = []; //Creates the array and placeholder

 

Within you loop where you are currently attempting to print the result using gs.info(answer) - load into the array for each pass:

answers.push(answer);

 

Where you return in the Script Include:

return answers.toString();

 

Then, from within your Client Script which is calling the Script Include, you'll need something similar to the below:

 

var answers = [];

function returnValues(response){
        var answer = response; 
	alert("Answer: " + answer);
	answers=answer.split(",");
}

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.


Thanks, Robbie

View solution in original post

5 REPLIES 5

Trupti94
Tera Guru

Hi @tsoct ,

 

have you used .toString() function while returning array?

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Can you share your script include?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Hello @Mark Roethof 

var ItemCheck = Class.create();
ItemCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getAvailability: function() {
        var gr = new GlideRecord('sc_req_item');
        gr.addEncodedQuery("cat_item=y5q654a872da110bcefdd383cbb3552^state!=4");
        gr.orderBy('actual_start');
        gr.addAggregate('SUM', 'quantity');
        gr.query();

        var dateCounts = {};
		var answer =[];

        while (gr.next()) {
            var startDate = new GlideDateTime(gr.getValue('actual_start')).getLocalDate().getDisplayValue();
            var endDate = new GlideDateTime(gr.getValue('actual_end')).getLocalDate().getDisplayValue();
            var quantity = parseInt(gr.getValue('quantity'));


            var currentDate = new GlideDateTime(startDate);
            var endDateTime = new GlideDateTime(endDate);

            while (currentDate <= endDateTime) {
                var dateStr = currentDate.getLocalDate().getDisplayValue();

                if (!dateCounts[dateStr]) {
                    dateCounts[dateStr] = {
                        count: 0,
                        quantity: 0
                    };
                }

                dateCounts[dateStr].count += 1;
                dateCounts[dateStr].quantity += quantity;

                currentDate.addDaysLocalTime(1);
            }
        }

        for (var date in dateCounts) {
            var available = 10 - dateCounts[date].quantity;
            if (available <= 0) {
                answer = ('No item available on : ' + date);
            }
            if (available == 1) {
                answer = (date + ': Only ' + available + ' item available');
            }
            if (available > 1) {
                answer = (date + ': Only ' + available + ' item available');
            }
            return (answer);
        }

    },
    type: 'ItemCheck'
});

And if you rewrite your code, having the return outside of the for loop, and within the for loop build a variables of 1 to N lines with the results? That should work fine.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn