Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help with reference field catalog client script or UI Policy

Ed Hefford
Tera Guru

hey all

I have a variable set that contains a reference field (Job Title) that will look up the job title of the Requested For user.

If 1 record is found, this reference auto populates via catalog client script but if the Requested For var has 2 jobs (quite common) they are prompted to select the job.

I'm after a catalog script or UIP that will hide the Job Title if it auto populates as no user interaction is required, but show if more than 1 record is presented as the user will need to select the job title.

Screenshot example attached.

4 REPLIES 4

AnveshKumar M
Tera Sage
Tera Sage

Hi @Ed Hefford 

You might be adding the options using g_form.addOption() in your catalog client script.

 

You can add another logic if the number of job titles are returned from the Ajax call is only one, after adding the options to the Select Box using g_form.addOption() add another block of code like this

 

if(JOB TITLES == 1){

g_form.setValue("FIELD_NAME", "YOUR JOB TITLE VALUE");

}

 

If you want detailed script, let me know from which table you are querying the Job titles.

 

Please mark my answer helpful and accept as solution if it helped you 👍

Thanks,
Anvesh

Tai Vu
Kilo Patron
Kilo Patron

Hi @Ed Hefford 

Let's try this approach.

1. Define a function in an AJAX Script Include.

2. This function will query to get the Job Title with the param Requested for.

3. Return an array of Titles

4. Create an Onchange Client Script to retrieve the data.

Sample below.

 

    var ga = new GlideAjax("<client_callable_script_include>");
    ga.addParam('sysparm_name', '<function_count>');
    ga.addParam('sysparm_requested_for', '<requested_for_id>');
    ga.getXMLAnswer(function(response) {
		if(response.length >= 2){
			g_form.clearValue('job_title');
			g_form.setDisplay('job_title', true);
			g_form.setMandatory('job_title', true);
			return;
		}

		g_form.setValue('job_title', response[0]);
		g_form.setMandatory('job_title', false);
		g_form.setDisplay('job_title', false);
    });

 

 

Let me know if it works for you.

 

Cheers,

Tai Vu

Vishal Birajdar
Giga Sage

Hi @Ed Hefford 

 

How we are defining if user has two job titles or only one job title...??

Do we have specific fields for example, "job_title_1" , "job_title_2" on User record...??

 

If this is the case then you can write script include & onChange client script on Requested for variable

 

Step 1 : Script inlcude

Name - userUtils

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

    getJobTitle: function() {

        var arrayJobTiltle = [];


        /* Get user from client script */
        var user = this.getParameter('sysparm_user');

        /*  glide record on user table and check for job title */
        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('sys_id', user);
        grUser.query();

        if (grUser.next()) {
            if (grUser.getValue('job_title_1')) {
                arrayJobTiltle.push(grUser.getValue('job_title_1'));
            }

			if (grUser.getValue('job_title_2')) {
                arrayJobTiltle.push(grUser.getValue('job_title_2'));
            }

        }

        /* Return the array with job titles */
        return arrayJobTiltle.toString();


    },


    type: 'userUtils'
});

 

Step 2 : onChange client script on "Requested for"

 

/*Get requested for*/
var user = g_form.getValue('requested_for');
var ga = new GlideAjax("userUtils");  //script inlcude name
    ga.addParam('sysparm_name', 'getJobTitle');  //function name
    ga.addParam('sysparm_user', user);
    ga.getXMLAnswer(callBackFun);


function callBackFun(answer) {
		var result = JSON.parse(answer);
		if(result.length != 1){
                  //Show the variable the variable 
			g_form.clearValue('job_title');
			g_form.setDisplay('job_title', true);
			g_form.setMandatory('job_title', true);
		} else {
                       g_form.setValue('job_title', result[0]);
                       g_form.setMandatory('job_title', false);
		       g_form.setDisplay('job_title', false);
              }

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Amit Gujarathi
Giga Sage
Giga Sage

HI @Ed Hefford .
I trust you are doing great.
Please find below scripts for the same.
Script include

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

    getJobTitles: function() {
        var userId = this.getParameter('sysparm_user_id');
        var jobTitles = [];

        var gr = new GlideRecord('job_title_table'); // Replace 'job_title_table' with your actual table name
        gr.addQuery('user', userId);
        gr.query();

        while (gr.next()) {
            jobTitles.push(gr.getValue('title')); // Replace 'title' with your actual field name
        }

        return JSON.stringify(jobTitles);
    },

    type: 'UserJobTitles'
});

 

On change Catalog client script

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

    var ga = new GlideAjax('UserJobTitles');
    ga.addParam('sysparm_name', 'getJobTitles');
    ga.addParam('sysparm_user_id', newValue);
    ga.getXMLAnswer(function(response) {
        var jobTitles = JSON.parse(response);

        if (jobTitles.length === 1) {
            g_form.setValue('job_title', jobTitles[0]);
            g_form.setDisplay('job_title', false);
        } else {
            g_form.setDisplay('job_title', true);
        }
    });
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi