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

Its working but not setting mandetory.

Rosy14_0-1720449880928.png

 

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.

Then it's not working, which is why I provided a complete solution - correcting your formatting / logic / coding standard errors and omissions in both scripts.  You can further troubleshoot the issue yourself by adding alert statements to the client script and gs.info to the Script Include to see how far it is getting, and where it is going wrong. 

Brad Bowman
Kilo Patron
Kilo Patron

Only string values can be passed between client and server, and vice-versa, so make sure you are setting the value in the Script Include to a string -  and it is advisable to always return a value, using something unique to prevent confusion:

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

    getUserInformation: function() {
        var answer = '';
        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()) {
            answer = 'record found';
        } else {
            answer = 'record not found';
        }
        return answer;
    },

    type: 'getUserDetails'
});

 

In the Client Script, test for a string value - as it is the Script Include value is likely converted to a string and what you are testing is a boolean value, and since answer always contains a value it's always true.  Also take care when setting fields/variables mandatory and visible.  The order matters as a mandatory field / variable cannot be hidden: 

function onLoad() {
    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 == 'record not found') {
			g_form.setMandatory("requested_for", false);
			g_form.setVisible("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);
        } else {
      		g_form.setVisible("requested_for", true);
			g_form.setMandatory("requested_for", true);
			g_form.setMandatory("company_name", false);
			g_form.setMandatory("company_number", false);
			g_form.setMandatory("requested_for1", false);
            g_form.setVisible("requested_for1", false);
            g_form.setVisible("company_name", false);
			g_form.setVisible("company_number", false);
		}
}