Catalog client script not working

Rosy14
Tera Guru

Hi,

I want to show fields based on loggedin user.

The info msg comming correct but it is not setting(visible/mandetory) the field as per the code.

Script include:
var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getUserInformation: function() {
        var user_sys_id = this.getParameter('sysparm_user_id');
        var user = new GlideRecord('customer_contact');
        user.addQuery("sys_id",user_sys_id);
		user.query();
        if(user.next())
            return false;
        else
            return true;

    },

    type: 'getUserDetails'
});
Client script:
function onLoad() {
    //Type appropriate comment here, and begin script below
    var user = g_user.userID;
    var ga = new GlideAjax('getUserDetails');
    ga.addParam('sysparm_name', 'getUserInformation');
    ga.addParam('sysparm_user_id', user);
    ga.getXML(parseUserResponse);

    function parseUserResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.addInfoMessage(answer);
        if(answer == true){
			
			g_form.setVisible("requested_for", false);
			g_form.setMandatory("requested_for", false);

			g_form.setVisible("requested_for1", true);
            g_form.setVisible("company_name", true);
			g_form.setVisible("company_number", true);

			g_form.setMandatory("company_name", true);
			g_form.setMandatory("company_number", true);
			g_form.setMandatory("requested_for1", true);
		}
        if(answer ==false)
			g_form.setVisible("requested_for", true);
			g_form.setMandatory("requested_for", true);

			g_form.setVisible("requested_for1", false);
            g_form.setVisible("company_name", false);
			g_form.setVisible("company_number", false);

			g_form.setMandatory("company_name", false);
			g_form.setMandatory("company_number", false);
			g_form.setMandatory("requested_for1", false);
		}

   
}

 

Rosy14_0-1720448372913.png

 

3 ACCEPTED SOLUTIONS

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Rosy14 

 

In Client Script :

 

please modify like this :

 

You need to add quotations for both answer for true and false  

 

 if(answer == 'true'){

Please change for answer =false

 

 

View solution in original post

Vrushali  Kolte
Mega Sage

Hello @Rosy14 ,

 

Please modify your if condition and make it -

 

if(answer == "true");

 

If my answer solves your issue please mark it as Accepted ✔️ and Helpful 👍 based on impact.

 

View solution in original post

There are missing {}s in your second if statement - that's why the rest of the code runs anyway and makes the fields non-mandatory again.

View solution in original post

8 REPLIES 8

Anurag Tripathi
Mega Patron
Mega Patron

Hi Rosy,

 

The below script will never work as you are comparing user sys id to a record in customer_contact table.

 

 var user_sys_id = this.getParameter('sysparm_user_id');
        var user = new GlideRecord('customer_contact');
        user.addQuery("sys_id",user_sys_id);
		user.query();
        if(user.next())
            return false;
        else
            return true;

 

 

Do you want to check a field on customer_contact  table?

 

 

Also, For this scenario, instead of Glide Ajax I would suggest you use Display BR + Client script.

In Display BR check the logged in user  and set a scratchpad variable.

In your onload client script check the Check the scratchpad variable and set the field.

-Anurag

LJ86
Kilo Guru

Hi Rosy,

 

Try 'true' and 'false' instead of true/false in the if statements, that should help. 

 

Cheers,

Lukasz

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Rosy14 

 

In Client Script :

 

please modify like this :

 

You need to add quotations for both answer for true and false  

 

 if(answer == 'true'){

Please change for answer =false

 

 

Vrushali  Kolte
Mega Sage

Hello @Rosy14 ,

 

Please modify your if condition and make it -

 

if(answer == "true");

 

If my answer solves your issue please mark it as Accepted ✔️ and Helpful 👍 based on impact.