Gliderecord not working

EH Desev
Tera Contributor

Hi there experts,

 

I am working on requirement to create a function which accepts an incident as a parameter and returns comma-separated list of  incident numbers with the same Business Service as the one that has been created.

 

For that purpose I have created a business rule which triggers on Insert and passes a sys id to a script include. Then I've created a glide record in the script include which should find the incident numbers with the matching Business Service and create a message in the log. Below is my script include code. The problem I am facing is that in the log the message I get is one and the same inc number repeating 4 times: INC0010348,INC0010348,INC0010348,INC0010348. Four is actually the number of incidents that have the same Business Service as the one I create but it is not printing the inc numbers. I tried multiple changes but nothing helped to resolve. 

 

 findMatchingInc: function(sysId) {

        var arr = [];

        var grSame = new GlideRecord("incident");
	grSame.addQuery(current.business_service, "business_service");
        grSame.query();
        while (grSame.next()) {
            arr.push(grSame.number);
        }

        gs.info(arr);
	
    },

 

 

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

GlideRecord works just fine - or at least how it was designed.

The problem is the programmer who (did not RTFM 😄 and) pushes into the array a reference to the same object (grSame.number) - in this case - 4 times.

Here number (the property of grSame) is an object that remains the same object no matter how many times next() is called (so not the value or display value of the field with the same name of the current record).

For such an object to "reveal" (or return) the value or the display value in the field of the current record one must call that object's toString() or getDisplayValue() methods.

When the code reaches instruction

 

gs.info(arr);

 

(since that function expects a string) arr will be converted into a string.

This means that for each element of the array that element's toString() method will be called - because all items in this case are objects.

Since all 4 elements of the array are pointers to the same object, each 4 calls (for the 4 elements) will in fact be calls to the toString() method of the same object.

Because at that point the current record is the last record looped, that will be printed 4 times.

That is why you get the same value 4 times.

The solution is simple: don't add references (to the same object) in the loop, but the underlying value (or display value), by changing your script to

 

arr.push(grSame.number.toString());

 

or:

 

arr.push(grSame.number.getDisplayValue());

 

but in this case it would return the same value.

The two would be different in case of fields that have separate values for the two, like a choice or a reference field.

View solution in original post

6 REPLIES 6

Danish Bhairag2
Tera Sage
Tera Sage

Hi @EH Desev ,

 

Issue is with the below line

 

grSame.addQuery(current.business_service, "business_service");

 

Replace it with

 

grSame.addQuery ("business_service",current.business_service);

 

Thanks,

Danish

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @EH Desev ,

Please give a try to the script below and let me know how it works for you.

findMatchingInc: function(sysId) {

    var arr = [];

    // Assuming sysId is the sys_id of the incident you are working with
    var gr = new GlideRecord("incident");
    gr.get(sysId); // Retrieve the incident based on sysId

    if (gr.isValidRecord()) {
        // Get the Business Service of the incident
        var businessService = gr.business_service;

        // Now, query for other incidents with the same Business Service
        var grSame = new GlideRecord("incident");
        grSame.addQuery("business_service", businessService);
        grSame.query();

        while (grSame.next()) {
            arr.push(grSame.number);
        }

        gs.info("Matching Incidents: " + arr.join(", "));
    } else {
        gs.error("Invalid incident sys_id: " + sysId);
    }
},

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,
Aniket