Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

How to fill the variable on the basis of requested for details?

developersn
Tera Contributor

Hi Developers,

 

I have a problem :

In the catalog item, there is a checkbox(If the logged in user is a partner); this checkbox should be checked if the requested_for user's career level is Manager or consultant. Career level is a drop down field with multiple designations(Analyst, Manager, Senior manager,...)

 

Kindly help with the script.

 

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Hi @developersn ,

 

In that case please create an onChange script on requested for field as below

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {

        return;

    }

 

    // Use GlideAjax to get the career level of the requested_for user

    var ga = new GlideAjax('CheckCareerLevel');

    ga.addParam('sysparm_name', 'getCareerLevel');

    ga.addParam('sysparm_user', g_form.getValue('requested_for')); // Assuming 'requested_for' is the variable name for the user reference field

    ga.getXML(parseResponse);

}

 

function parseResponse(response) {

    var answer = response.responseXML.documentElement.getAttribute('answer');

    if (answer === 'Manager' || answer === 'Consultant') {

        g_form.setValue('variables.partner_checkbox', true); // Check the checkbox if career level is Manager or Consultant

    } else {

        g_form.setValue('variables.partner_checkbox', false); // Uncheck the checkbox for other care

er levels

    }

}

 

Script Include:

Client Callable should be true

 

var CheckCareerLevel = Class.create();

CheckCareerLevel.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCareerLevel: function() {

        var userId = this.getParameter('sysparm_user');

        var gr = new GlideRecord('sys_user'); // Assuming 'sys_user' is the table name for the User table

        if (gr.get(userId)) {

            return gr.getValue('career_level');

        }

        return '';

    },

    type: 'CheckCareer

Level'

});

 

Thanks,

Danish

 

View solution in original post

5 REPLIES 5

Hi @developersn ,

 

In that case please create an onChange script on requested for field as below

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {

        return;

    }

 

    // Use GlideAjax to get the career level of the requested_for user

    var ga = new GlideAjax('CheckCareerLevel');

    ga.addParam('sysparm_name', 'getCareerLevel');

    ga.addParam('sysparm_user', g_form.getValue('requested_for')); // Assuming 'requested_for' is the variable name for the user reference field

    ga.getXML(parseResponse);

}

 

function parseResponse(response) {

    var answer = response.responseXML.documentElement.getAttribute('answer');

    if (answer === 'Manager' || answer === 'Consultant') {

        g_form.setValue('variables.partner_checkbox', true); // Check the checkbox if career level is Manager or Consultant

    } else {

        g_form.setValue('variables.partner_checkbox', false); // Uncheck the checkbox for other care

er levels

    }

}

 

Script Include:

Client Callable should be true

 

var CheckCareerLevel = Class.create();

CheckCareerLevel.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCareerLevel: function() {

        var userId = this.getParameter('sysparm_user');

        var gr = new GlideRecord('sys_user'); // Assuming 'sys_user' is the table name for the User table

        if (gr.get(userId)) {

            return gr.getValue('career_level');

        }

        return '';

    },

    type: 'CheckCareer

Level'

});

 

Thanks,

Danish