GlideRecord | Issue with iterating through all records

Sylvain12
Tera Contributor

Hello Community,

 

I am working on a catalog client script to retrieve informations from existing requests.

Let's say when user is filling the field "Project Name" in the catalog item, the script has to check all the requests in the sc_req_item table to check if another RITM already exists with the same "Project Name" value. 

 

I used a Script Include + "on change" Catalog Client Script.

 

My problem is the script occurs only at one RITM instead of going through all the RITM in the table, even though i'm using while (gr1.next()) { method.

 

Here is the Script Include:

var CheckProjectName= Class.create();
CheckProjectName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getVariablesFromRITM: function() {
        var gr1 = new GlideRecord("sc_req_item");
        gr1.query();
        while (gr1.next()) {
            var obj = {};
            obj.checked_project_name= gr1.variables.project_name.toString();
            return JSON.stringify(obj);
        }
    }
});

 

And the Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('CheckProjectName');
ga.addParam('sysparm_name', 'getVariablesFromRITM');
ga.getXML(checkExistingProjectName);

function checkExistingProjectName(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);

alert(result.checked_project_name+" is already used in existing RITM");

}
}
}

 

Result of this: the Project Name of the checked RITM is displayed, but only one RITM is checked (and not the first or last one of the table).

 

I think the issue comes from the GlideRecord, but I can't find what I am doing wrong with it.

 

Could someone support on this topic ? 

Thank you very much in advance.

 

Sylvain

1 ACCEPTED SOLUTION

Sebas Di Loreto
Kilo Sage

 

 

@Sylvain12 

If I understand you right, your client script is an onChange of the project_name variable in the catalog item right?

Then that is what you want to look for in the script include but you have to pass it through a parameter while calling the Ajax. The newValue on the client script is what somebody typed on the project_name variable.

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('CheckProjectName');
ga.addParam('sysparm_name', 'getVariablesFromRITM');
ga.addParam('sysparm_project', newValue);
ga.getXML(checkExistingProjectName);

function checkExistingProjectName(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);

alert(result.checked_project_name+" is already used in existing RITM");

}
}
}

 

  

The script include should receive that value

var CheckProjectName= Class.create();
CheckProjectName.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getVariablesFromRITM: function() {
        var project = this.getParameter('project');
        var gr1 = new GlideRecord("sc_req_item");
        gr1.addEncodedQuery('<add query for the fields where you want to search the project name>');
        gr1.query();
        var obj = {};
        while (gr1.next()) {
            obj.checked_project_name= gr1.variables.project_name.toString();
        }
        return JSON.stringify(obj);
    }
});

 


If I helped you with your case, please click the Thumb Icon and mark as Correct.


View solution in original post

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

Hi Sylvain,

Since your return is inside the while loop, it is returning to the client script the first time it is encountered.  Instead, push each object to an array, then return the array after the loop:

 

 

var CheckProjectName= Class.create();
CheckProjectName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getVariablesFromRITM: function() {
        var answerArr = [];   
        var obj = {};
        var gr1 = new GlideRecord("sc_req_item");
        gr1.query();
        while (gr1.next()) {
            obj.checked_project_name = gr1.variables.project_name.toString();
            answerArr.push(obj);
        }
        return JSON.stringify(answerArr);
    }
});

 

Or you can just build the object in the while loop then return it after without the array - depending on how you want to process/use it in the Client Script

 

Basheer
Mega Sage

Hi @Sylvain12 ,

You've to pass the input to script include from client script

As per me Client script

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CheckProjectName');
ga.addParam('sysparm_name', 'getVariablesFromRITM');
ga.addParam('sysparm_project',g_form.getValue("projectName"));//instead of projectName give the column name
ga.getXML(checkExistingProjectName);
function checkExistingProjectName(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
alert(result.checked_project_name+" is already used in existing RITM");
}
}
}

 

 

In Script include

 

var CheckProjectName= Class.create();
CheckProjectName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getVariablesFromRITM: function() {
var projectName = this.getParameter("sysparm_project");
        var gr1 = new GlideRecord("sc_req_item");
       gr1.addQuery("columnName",projectName);
        gr1.query();
        while (gr1.next()) {
            var obj = {};
            obj.checked_project_name= gr1.variables.project_name.toString();
}
            return JSON.stringify(obj);
    }
});

 

 

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

Sebas Di Loreto
Kilo Sage

 

 

@Sylvain12 

If I understand you right, your client script is an onChange of the project_name variable in the catalog item right?

Then that is what you want to look for in the script include but you have to pass it through a parameter while calling the Ajax. The newValue on the client script is what somebody typed on the project_name variable.

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var ga = new GlideAjax('CheckProjectName');
ga.addParam('sysparm_name', 'getVariablesFromRITM');
ga.addParam('sysparm_project', newValue);
ga.getXML(checkExistingProjectName);

function checkExistingProjectName(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);

alert(result.checked_project_name+" is already used in existing RITM");

}
}
}

 

  

The script include should receive that value

var CheckProjectName= Class.create();
CheckProjectName.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getVariablesFromRITM: function() {
        var project = this.getParameter('project');
        var gr1 = new GlideRecord("sc_req_item");
        gr1.addEncodedQuery('<add query for the fields where you want to search the project name>');
        gr1.query();
        var obj = {};
        while (gr1.next()) {
            obj.checked_project_name= gr1.variables.project_name.toString();
        }
        return JSON.stringify(obj);
    }
});

 


If I helped you with your case, please click the Thumb Icon and mark as Correct.


Thank you so much for your quick feedback !

yes your are correct, this is very helpfull.

 

Another question: is it possible to use the passed through parameter (newValue) to filter the GlideRecord with addEncodedQuery method ? What I mean is, can I use this value without having hard coded sys_id to filter ?