Help with a client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-04-2024 01:36 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-04-2024 02:16 PM
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-05-2024 11:31 PM
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 .
If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.
Thanks,
Subhashis Ratna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-06-2024 05:59 AM
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