not working function onChange(control, oldValue, newValue, isLoading, isTemplate) { if…
- User
not working
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } g_form.addInfoMessage("I am in "); g_form.addErrorMessage (" i am here "); function concatenateCallerIdAndStorename(callerID) { // Perform an asynchronous GlideAjax call to fetch user details var ga = new GlideAjax('GetUserDetails'); ga.addParam('sysparm_name', 'getDetails'); // Correct function name in script include ga.addParam('sys_id', callerID); ga.getXMLAnswer(function(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); if (answer) { var userDetails = JSON.parse(answer); var concatenatedValue = userDetails.user_id + " - " + userDetails.u_store_name;
// Set the concatenated value in a custom field (e.g., u_caller_details) g_form.setValue('u_caller_details', concatenatedValue); } }); }
// Call the function to concatenate caller ID and department concatenateCallerIdAndStorename(newValue); }
Script Include: var GetUserDetails = Class.create(); GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() { var sys_id = this.getParameter('sys_id'); var user = new GlideRecord('sys_user'); user.get(sys_id);
var details = { user_id: user.user_name.toString(), u_store_name : user.u_store_name.getDisplayValue().toString() };
return JSON.stringify(details); }
});
- Assistant
Your current implementation contains several issues that might prevent it from functioning correctly. I'll address a few key points and provide a revised version.
Issues and Solutions:
- Misuse of
getDisplayValue()on a string field: The methodgetDisplayValue()is generally used for reference fields, not string fields or custom fields. It's safer to use thegetValue()method unless you are specifically dealing with reference data. - Ensure proper error handling: Always check for the existence of the record before using its fields.
- Make sure
sys_idparameter is passed correctly. - Debugging: Add some debug messages to help identify any issue during execution.
Revised Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } g_form.addInfoMessage("Starting the process to concatenate caller ID and store name."); function concatenateCallerIdAndStorename(callerID) { var ga = new GlideAjax('GetUserDetails'); ga.addParam('sysparm_name', 'getDetails'); ga.addParam('sys_id', callerID); ga.getXMLAnswer(function(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); if (!answer) { g_form.addErrorMessage("Error: Unable to retrieve user details."); return; } var userDetails = JSON.parse(answer); var concatenatedValue = userDetails.user_id + " - " + userDetails.u_store_name; g_form.setValue('u_caller_details', concatenatedValue); g_form.addInfoMessage("Successfully updated caller details."); }); } concatenateCallerIdAndStorename(newValue); }Revised Script Include:
var GetUserDetails = Class.create(); GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, { getDetails: function() { var sys_id = this.getParameter('sys_id'); var user = new GlideRecord('sys_user'); if (user.get(sys_id)) { var details = { user_id: user.getValue('user_name'), u_store_name: user.getValue('u_store_name') }; return JSON.stringify(details); } else { return JSON.stringify({ error: 'User not found' }); } } });Tips for Debugging:
- Make sure the parameter names are spelled correctly and match across the client script and Script Include.
- Use
g_form.addInfoMessageandg_form.addErrorMessageto show status and errors during the script execution.
Additional Advice:
- Ensure that the field names (like
u_store_name) exist on thesys_usertable or whichever table you're querying. - Confirm the client script is correctly configured to run on the appropriate field change in the form.
By carefully diagnosing each step and using debug messages, you can identify where the script might be failing and verify the program flow.
- Misuse of