Help with a client script

Sam10
Tera Expert

Hi All,


Very new to scripting and been struggling with this requirement, can someone help me out?


I have a script include named XUserDetailsAjax(); which has a function called getCompany, which can extract the company of the requested for user (this comes from a variable within a variable set, variable = "requested_for_set" and variable set name = "requested for").

Using this script, I need to check requested_for's company (if its "xyz") and set value "abcd" to the field "abcd_group".


Any help is appreciated!

3 REPLIES 3

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Sam10,

 

Script include :

var getUserPropertiesAjax = Class.create();
getUserPropertiesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	get_comp : function() {
		var grUser = new GlideRecord('sys_user');
		if(grUser.get(this.getParameter('sysparm_user'))) {
			return grUser.getValue('company');
		}
	},
	
    type: 'getUserPropertiesAjax'
	
});


Client Script

Type: onChange
Field name: your req_for field

Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {

	if(isLoading) {
		return;
	}

	if(newValue === '') {
		g_form.clearValue('u_company');
	}

	var gaPhone = new GlideAjax('getUserPropertiesAjax');
	gaPhone.addParam('sysparm_name', 'get_comp ');
	gaPhone.addParam('sysparm_user', newValue);
	gaPhone.getXMLAnswer(handleResponse);

	function handleResponse(response) {
		var answer = response;
		g_form.setValue('u_company', answer);
	}

}

ā˜‘ļø Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Subhashis Ratna
Tera Guru

Hi @Sam10 

You can call your Script Include code in your catalog client script like this.

Catalog CS code : 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    g_form.clearValue('u_company');
    var gaPhone = new GlideAjax('getUserPropertiesAjax');
    gaPhone.addParam('sysparm_name', 'get_comp ');
    gaPhone.addParam('sysparm_user', newValue);
    gaPhone.getXMLAnswer(handleResponse);

    function handleResponse(response) {
        var answer = response;
        g_form.setValue('u_company', answer);
    }

}


SS : You can choose your variable set and variable name as per your requirement .

SubhashisRatna_0-1712384951723.png


If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

Thanks,
Subhashis Ratna

Maddysunil
Kilo Sage

@Sam10 

Client Script:

 

var ajax = new GlideAjax('XUserDetailsAjax');
ajax.addParam('sysparm_name', 'getCompany');
ajax.addParam('sysparm_requested_for', current.requested_for); // Assuming requested_for is the sys_id of the user record
ajax.getXMLAnswer(function(response) {
    var company = response.trim(); // Trim any whitespace from the response

    // Check if the company is "xyz"
    if (company === 'xyz') {
        // Set the value "abcd" to the field "abcd_group"
        current.abcd_group = 'abcd';
    }

 

Script Include:

 

 getCompany: function() {
        var requestedFor = this.getParameter('sysparm_requested_for'); // Get the sys_id of the requested for user
        var company = '';

        // Check if the requested for user exists
        if (requestedFor) {
            var userGr = new GlideRecord('sys_user');
            if (userGr.get(requestedFor)) {
                company = userGr.company.getDisplayValue(); // Get the company of the user
            }
        }

        return company; // Return the company name
    },

 

Please Mark āœ…Correct if this solves your query and also mark šŸ‘Helpful if you find my response worthy based on the impact.

 

Thanks