Script Include Error

Lucky1
Tera Guru

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.

 

Lucky1_0-1732186171595.png

 

 

Lucky1_1-1732186345481.png

 

 

Regards,

Lucky

3 REPLIES 3

Juhi Poddar
Kilo Patron

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

 

 

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

Initialize the object before use, as in:

 

 

		var obj = { //Create an object to store the User data
		cname: "Unknown",
		region: "Unknown",
		error: "None",
		};