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

-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.

-O-
Kilo Patron
Kilo Patron

Also I'm assuming that the actual code contains

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

and not

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

Case the latter is not correct.

-O-
Kilo Patron
Kilo Patron

Also it may be that a second filter should be added, one that excludes the triggering record:

...
grSame.addQuery("sys_id", "!=", sysId);
grSame.addQuery("business_service", current.business_service);
...

KasiramanathanD
Tera Contributor

Hi @EH Desev ,


To call a script include from a business rule, use the following syntax:

new ScriptIncludeName().functionName(current.business_service);

For instance, if the script include is named 'getIncidents' and the function is 'findMatchingInc', the syntax would be:

new getIncidents().findMatchingInc(current.business_service)

In the script include, ensure you pass the value within an add query:

 findMatchingInc: function(sysId) { //sysId will hold the sys id of business service of the incident
     var incidentNumbers = [];
     var incidentRecords = new GlideRecord('incident');
     incidentRecords.addQuery('business_service', sysId);
     incidentRecords.query();
     while (incidentRecords.next()) {
         incidentNumbers.push(incidentRecords.number.toString());
     }
     gs.log("Incidents with the same business service are: " + incidentNumbers, "findMatchingInc");
 }