Skip to content
The Chat Archive
gpt-4o-2024-05-13· OpenAI WildChat-4.8M (AI2)

not working function onChange(control, oldValue, newValue, isLoading, isTemplate) { if…

  1. 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); }

    });

  2. 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:

    1. Misuse of getDisplayValue() on a string field: The method getDisplayValue() is generally used for reference fields, not string fields or custom fields. It's safer to use the getValue() method unless you are specifically dealing with reference data.
    2. Ensure proper error handling: Always check for the existence of the record before using its fields.
    3. Make sure sys_id parameter is passed correctly.
    4. 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.addInfoMessage and g_form.addErrorMessage to show status and errors during the script execution.

    Additional Advice:

    • Ensure that the field names (like u_store_name) exist on the sys_user table 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.