Show error message to user in catalog form if the same combination of city and street exists in location table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2022 04:18 AM
Hi,
I have a requirement where user fills the text fields, street , city and location name in a catalog item and i have to check whether the details entered by user exists in location table. If yes, throw the popup message that they exist.
Location name should be unique i.e; it should not exist and the same combination of street and city should not exist.
The name has to be different and the combination of street and city should at least have one different value. If both street and city exist the user should be given message to change one of the values.
I hope i made the requirement clear. Please help me in achieving this.
Thanks!
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2022 06:34 AM
Hello,
If you don't mind, can you please share below information?
What have you tried? What is working? What isn't working?
Have you tried putting log statements to see where it is reaching ?
Unfortunately, at this point, you may have got requirements and that you've now given to us. Providing solution will resolve your issue but it will not help you to learn.
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2022 10:03 PM
I have written the onChange client script on both city and street field and passing the value to server side via script include to check the combination and then returning the result in scratcpad variable and displaying message via onSubmit script.
It works fine mostly but sometimes if I change the combinations without refreshing browser then it does not give the popup message that the combination exists even if the combination is present. Below is the code:
Client Script for street field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val = g_form.getValue("city");
var st = g_form.getValue("street");
if (val == "") {
g_scratchpad.stExist = true;
}
if (val != "") {
//alert("city in street" + val);
var checkStr = new GlideAjax("CheckStrName");
checkStr.addParam("sysparm_name", "CheckStr");
checkStr.addParam("sysparm_cit", val);
checkStr.addParam("sysparm_stree", st);
checkStr.getXML(getStre);
}
function getStre(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var returneddata = JSON.parse(answer);
var error_code = returneddata.ERROR;
if (error_code == "true") {
g_scratchpad.stExist = true;
g_scratchpad.code = returneddata.CODE;
g_scratchpad.name = returneddata.NAME;
g_scratchpad.street = returneddata.STREET;
g_scratchpad.city = returneddata.CITY;
} else
g_scratchpad.stExist = false;
}
}
Script Include
var CheckStrName = Class.create();
CheckStrName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
CheckStr: function() {
var errorMessage = "false";
var ct = this.getParameter("sysparm_cit");
var str1 = this.getParameter("sysparm_stree");
var gr = new GlideRecord("cmn_location"); {
gr.addQuery('street', str1);
gr.addQuery('city', ct);
gr.setLimit(1);
gr.query();
if (gr.next()) {
errorMessage = "true";
var json = new JSON();
var results = {
"ERROR": errorMessage,
"CODE": gr.getValue("u_location_code"),
"NAME": gr.getValue("name"),
"STREET": gr.getValue("street"),
"CITY": gr.getValue("city"),
};
return JSON.stringify(results);
} else
return errorMessage;
}
},
type: 'CheckStrName'
});
The same logic is made for city field , a separate onChange client script and script include with similar logic.
on Submit script to give message
function onSubmit() {
var SubExist = g_scratchpad.streetExist;
var LocExist = g_scratchpad.locationExist;
var stee = g_scratchpad.stExist;
var cd = g_scratchpad.code;
var nm = g_scratchpad.name;
var ct = g_scratchpad.city;
var st = g_scratchpad.street;
alert("street" + stee);
alert("loc" + LocExist);
alert("city" + SubExist);
if ((SubExist == true && stee ==true) && (LocExist == false )|| (LocExist==true)) {
alert("Your request cannot be fulfilled. Following record exist in the system. cannot have duplicates " + cd + "-" + nm + "-" + st + "-" + ct + );
return false;
}
}
Wwhat could be the issue here? Is it the complexity of code?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2022 12:58 AM
Hi SK,
Using scratchpad doesn't always work to pass variable values between Client Scripts.
How about showing the error message onChange(). If the street and city fields are mandatory, these fields can be cleared when there is an error. This way, onSubmit will cause an error because these fields are not filled in.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-03-2022 02:51 AM
Hi,
Something like below.
1. onChange() on field "street"
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val = g_form.getValue("city");
var st = newValue;
if (val == '') {
return;
}
var checkStr = new GlideAjax("CheckStrName");
checkStr.addParam("sysparm_name", "CheckStr");
checkStr.addParam("sysparm_cit", val);
checkStr.addParam("sysparm_stree", st);
checkStr.getXMLAnswer(function(answer) {
var json = JSON.parse(answer);
if (json.ERROR == 'true') {
g_form.clearValue('street');
g_form.showFieldMsg('street', 'Your request cannot be fulfilled. Following record exist in the system. cannot have duplicates ' + json.CODE + '-' + json.NAME + '-' + json.STREET + '-' + json.CITY, 'error');
}
});
}
2. onChange() on field "city"
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val = newValue;
var st = g_form.getValue("street");
if (st == '') {
return;
}
var checkStr = new GlideAjax("CheckStrName");
checkStr.addParam("sysparm_name", "CheckStr");
checkStr.addParam("sysparm_cit", val);
checkStr.addParam("sysparm_stree", st);
checkStr.getXMLAnswer(function(answer) {
var json = JSON.parse(answer);
if (json.ERROR == 'true') {
g_form.clearValue('city');
g_form.showFieldMsg('city', 'Your request cannot be fulfilled. Following record exist in the system. cannot have duplicates ' + json.CODE + '-' + json.NAME + '-' + json.STREET + '-' + json.CITY, 'error');
}
});
}
3. Script Include
var CheckStrName = Class.create();
CheckStrName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
CheckStr: function() {
var errorMessage = "false";
var ct = this.getParameter("sysparm_cit");
var str1 = this.getParameter("sysparm_stree");
var gr = new GlideRecord("cmn_location"); {
gr.addQuery('street', str1);
gr.addQuery('city', ct);
gr.setLimit(1);
gr.query();
if (gr.next()) {
errorMessage = "true";
var json = new JSON();
var results = {
"ERROR": errorMessage,
"CODE": gr.getValue("u_location_code"),
"NAME": gr.getValue("name"),
"STREET": gr.getValue("street"),
"CITY": gr.getValue("city"),
};
return JSON.stringify(results);
} else
return errorMessage;
}
},
type: 'CheckStrName'
});
Execution result. Both fields are mandatory so trying to submit will cause error.