Catalog client script Need help

Vurukonda Sai 1
Tera Contributor

Hello. 

I have one requirement.

User from portal end user can able access the Category GBS HR USA and GBS HR Mexico. both the category only accessible only User Department cantinas HR and Department is Human Resources.  

I have write On change catalog client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('UserDetails');
    ga.addParam('sysparm_name', 'getInfo');
    ga.addParam('sysparm_user_id', g_user.userID);
    ga.getXML(getResponse);

    function getResponse(response) {
        var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
        var department = answer.department;
        var country = answer.country;
        if (department === 'HR' && department == '487bb6a2dbb0d01020e3de0cd396197d'  && country == 'MX' || g_user.hasRole('admin')) {
            g_form.addOption('u_category', "GBS HR Mexico", "GBS HR Mexico");
            g_form.addOption('u_category', "gbs_hr_usa", "GBS HR USA");
        } else {
            g_form.removeOption('u_category', "GBS HR Mexico");
            g_form.removeOption('u_category', "gbs_hr_usa");
        }
    }
}



Kindly check the code user end. This code is not working. if i remove the "department === 'HR' &&" Only department == 'HUMAN RESOURCES' user can able to view the category. 

Kindly help me on this code. it would be helpful for my development 

2 ACCEPTED SOLUTIONS

Vrushali  Kolte
Mega Sage

Hello @Vurukonda Sai 1 ,

 

Thanks for sharing your use case. I think the issue relies in the line -" if (department === 'HR' && department == '487bb6a2dbb0d01020e3de0cd396197d')"
This will always fail, because department can't simultaneously equal both "HR" and the sys_id.

I would suggest to add the alert and check what is getting returned and accordingly update your condition.

 

Let me know if you need further help😀.

 

Please Mark my answer as Helpful👍 and Accepted✔️, if it helps!

View solution in original post

John Gilmore
Giga Guru

As Vrushali said you have two checks on department when only one can be true. The way you are querying user details the department should return the sys_id. Thus you need to update your if statement condition to remove "department === 'HR'". This would leave your script condition as:

if(department == '487bb6a2bb0d01020e3de0cd396197d' && country == 'MX' || g_user.hasRole('admin'))

 

No change is necessary to the country evaluator since in glide ajax the country code is returned.

View solution in original post

4 REPLIES 4

Vrushali  Kolte
Mega Sage

Hello @Vurukonda Sai 1 ,

 

Thanks for sharing your use case. I think the issue relies in the line -" if (department === 'HR' && department == '487bb6a2dbb0d01020e3de0cd396197d')"
This will always fail, because department can't simultaneously equal both "HR" and the sys_id.

I would suggest to add the alert and check what is getting returned and accordingly update your condition.

 

Let me know if you need further help😀.

 

Please Mark my answer as Helpful👍 and Accepted✔️, if it helps!

John Gilmore
Giga Guru

As Vrushali said you have two checks on department when only one can be true. The way you are querying user details the department should return the sys_id. Thus you need to update your if statement condition to remove "department === 'HR'". This would leave your script condition as:

if(department == '487bb6a2bb0d01020e3de0cd396197d' && country == 'MX' || g_user.hasRole('admin'))

 

No change is necessary to the country evaluator since in glide ajax the country code is returned.

Chaitanya ILCR
Kilo Patron

Hi @Vurukonda Sai 1 ,

 

try this

just added a alert for testing purpose just to check whether you are even receiving the response (remove it if you are successfully getting the response)

 

if not also share the script of script include (UserDetails) better share the screenshot of script include(what to see name API name scope etc) along with code

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('UserDetails');
    ga.addParam('sysparm_name', 'getInfo');
    ga.addParam('sysparm_user_id', g_user.userID);
    ga.getXMLAnswer(getResponse);

    function getResponse(answer) {
        alert(answer + typeof answer); // remove this just added for the testing purpose
        answer = JSON.parse(answer);
        var department = answer.department;
        var country = answer.country;
        if ((department == 'HR' || department == '487bb6a2dbb0d01020e3de0cd396197d') && country == 'MX' || g_user.hasRole('admin')) {
            g_form.addOption('u_category', "GBS HR Mexico", "GBS HR Mexico");
            g_form.addOption('u_category', "gbs_hr_usa", "GBS HR USA");
        } else {
            g_form.removeOption('u_category', "GBS HR Mexico");
            g_form.removeOption('u_category', "gbs_hr_usa");
        }
    }
}

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

UserDetails

var
UserDetails = Class.create();
UserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getInfo: function() {
        var obj = {};

        obj.title = '';
        obj.manager = '';
        obj.department = '';
        obj.location = '';
        obj.email = '';
        obj.phone = '';
        obj.company = '';
        obj.region = '';
        obj.country = '';
        obj.site = '';
        obj.name = '';

        var id = this.getParameter('sysparm_user_id');
        var gr = new GlideRecord('sys_user');
        if (gr.get(id)) {
            obj.title = gr.title.toString();
            obj.manager = gr.manager.toString();
            obj.department = gr.department.toString();
            obj.location = gr.location.toString();
            obj.email = gr.email.toString();
            obj.phone = gr.phone.toString();
            obj.company = gr.company.getDisplayValue();
            obj.region = gr.u_region.toString(); //region
            obj.site = gr.u_site_code.toString();
            obj.country = gr.country.toString();
            obj.name = gr.name.toString();
        }

        return JSON.stringify(obj);

    },
    getInfo1: function() {
        var obj = {};

        obj.title = '';
        obj.manager = '';
        obj.department = '';
        obj.location = '';
        obj.email = '';
        obj.phone = '';
        obj.company = '';
        obj.region = '';
        obj.location1 = '';

        var id = this.getParameter('sysparm_user_id');
        var gr = new GlideRecord('sys_user');
        if (gr.get(id)) {
            obj.title = gr.title.toString();
            obj.manager = gr.manager.toString();
            obj.department = gr.department.toString();
            obj.location = gr.location.toString();
            obj.location1 = gr.location.getDisplayValue();
            obj.email = gr.email.toString();
            obj.phone = gr.phone.toString();
            obj.company = gr.company.getDisplayValue();
            obj.region = gr.u_region.toString(); //region
        }

        return JSON.stringify(obj);

    },

    getInfo2: function() {

        var obj = {};

        var id = this.getParameter('sysparm_user_id');
        var gr = new GlideRecord('sys_user');

        if (gr.get(id)) {

            obj.region = gr.u_region ? gr.u_region.toString() : '';
            obj.country = gr.location.country ? gr.location.country.toString() : '';
            obj.site = gr.u_site_code ? gr.u_site_code.toString() : '';
            obj.department = gr.department ? gr.department.toString() : '';

        }

        return JSON.stringify(obj);

    },

    getMutiUserDetails: function() {
        var obj = {};
        var temp = [];
        var id = this.getParameter('sysparm_user_id');
        var gr = new GlideRecord('sys_user');
        gr.addEncodedQuery("sys_idIN" + id);
        gr.query();
        while (gr.next()) {
            temp.push(gr.name.toString());
        }
        obj.name = temp.toString();
        return JSON.stringify(obj);
    },

    type: 'UserDetails'
});