Script Include Error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 02:52 AM
Hello all,
Good day!!
For a string type of variable, I have written code to validate if the user is giving correct oracle number or not.
When I give correct Oracle number, the Customer name and Region are auto populating as expected.
But when I give wrong Oracle number, instead of executing the else part on the code, it is showing like,
"There is a java script error in your browser console"
So, please help me in correcting m code:
Note: Oracle no is Not there log is also coming in logs.
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 03:11 AM
Hello @Lucky1
The issue occurs because the server-side script returns "No" as a plain string when no record is found, which causes JSON.parse() in the client script to throw an error.
Update script on server side:
getOracleIDs: function() {
var orc_num = this.getParameter('sysparm_onum');
var obj = {};
var cust_account = new GlideRecord('ast_contract');
cust_account.addEncodedQuery('account.u_ims_account=' + orc_num);
cust_account.query();
if (cust_account.next()) {
obj.cname = cust_account.account.toString();
obj.region = cust_account.u_region.toString();
} else {
obj.error = "No record found";
}
return JSON.stringify(obj);
}
Update script on client side:
ga.getXMLAnswer(function(response) {
try {
var answer = JSON.parse(response);
if (answer.cname && answer.region) {
g_form.setValue('u_customer_name', answer.cname);
g_form.setValue('u_region', answer.region);
g_form.setReadOnly('u_customer_name', true);
g_form.setReadOnly('u_region', true);
} else if (answer.error) {
alert("Error: " + answer.error);
g_form.clearValue('u_oracle_id');
}
} catch (e) {
console.error("Failed to parse the response:", response);
alert("An unexpected error occurred. Please check the logs.");
}
});
This ensures consistent JSON handling and prevents JavaScript errors.
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 06:52 AM
Hello Juhi,
No, it's not working.
Even when I give incorrect Oracle number, the alert for answer is giving [Object, Object]
It is not going to else part
I checked the logs and log messages are coming good in Script Include.
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2024 08:12 AM - edited 11-23-2024 08:13 AM
Initialize the object before use, as in:
var obj = { //Create an object to store the User data
cname: "Unknown",
region: "Unknown",
error: "None",
};