- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 05:00 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 05:37 AM - edited 01-19-2023 05:41 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 07:52 AM
Ok it worked using:
gr1.addEncodedQuery('variables.e7ebb6731b70e4d0bf2bda42b24bcb6b='+app_code.toString());
Thank you so much ! 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 08:05 AM
Glad that I could help.
The answer to your question is YES.
newValue is what the person typed on the client for project.
If I helped you with your case, please click the Thumb Icon and mark as Correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 05:48 AM
In addition
ga.getXML(checkExistingProjectName);
function checkExistingProjectName(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
can be a bit simpler:
ga.getXMLAnswer(checkExistingProjectName);
function checkExistingProjectName(answer) {
var result = JSON.parse(answer);
If the GlideAjax call is launched/triggered using method getXMLAnswer (vs. getXML), the parameter (answer) to the callback function (here checkExistingProjectName) will be the exact string returned by the Script Include.