fields are not auto-filling in the mobile agent

Rhonda9
Tera Expert

Hello, 

 

I am having an issue getting the incident location and phone field to auto-fill with the affected user's details in the mobile agent on incident form.  I am also trying to get the affected user  be the same as caller when the caller field is changed .  All of this must happen before save.    I created a client script with script include and it is not working.   Please help.

Client Script

function onChange(control, oldValue, newValue, isLoading) {
// Skip if the form is loading
if (isLoading || !newValue) {
return;
}

// Create a new GlideAjax object for the Script Include
var ga = new GlideAjax('GetUserDetails');
ga.addParam('sysparm_name', 'getUserLocationAndPhone');
ga.addParam('sys_id', newValue); // Pass the affected user's sys_id
ga.getXMLAnswer(function(response) {
var userDetails = JSON.parse(response); // Parse the JSON response

// Check if the response has values
if (userDetails) {
g_form.setValue('location', userDetails.location); // Set the location field
g_form.setValue('u_phone', userDetails.phone); // Set the custom phone field
}
});
}

Script Include

var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserLocationAndPhone: function() {
        var userId = this.getParameter('sys_id'); // Get the Sys ID from the client script
        var result = {};

        if (userId) {
            var userGR = new GlideRecord('sys_user'); // Query the sys_user table
            if (userGR.get(userId)) {
                result.location = userGR.location.getDisplayValue(); // Use display value for location field
                result.phone = userGR.u_phone; // Ensure 'u_phone' is the correct field name
            }
        }
        return JSON.stringify(result); // Return the result as a JSON string
    },

    type: 'GetUserDetails' // Ensure this matches the Script Include name
});


 

4 REPLIES 4

Bhavya11
Kilo Patron

Hi @Rhonda9 ,

 

These is issue in script  like you need to pass the value as "sysparm_sys_id" not ony "sys_id" make these changes both in client script and script include

please try below one

Client script

 

function onChange(control, oldValue, newValue, isLoading) {
// Skip if the form is loading
if (isLoading || !newValue) {
return;
}

// Create a new GlideAjax object for the Script Include
var ga = new GlideAjax('GetUserDetails');
ga.addParam('sysparm_name', 'getUserLocationAndPhone');
ga.addParam('sysparm_sys_id', newValue); // Pass the affected user's sys_id -//sysparm_sys_id
ga.getXMLAnswer(function(response) {
var userDetails = JSON.parse(response); // Parse the JSON response

// Check if the response has values
if (userDetails) {
g_form.setValue('location', userDetails.location); // Set the location field
g_form.setValue('u_phone', userDetails.phone); // Set the custom phone field
}
});
}

 

 

Script include :

 

var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserLocationAndPhone: function() {
        var userId = this.getParameter('sysparm_sys_id'); // Get the sysparm_sys_id from the client script
        var result = {};

        if (userId) {
            var userGR = new GlideRecord('sys_user'); // Query the sys_user table
            if (userGR.get(userId)) {
                result.location = userGR.location.getDisplayValue(); // Use display value for location field
                result.phone = userGR.getValue('u_phone'); // Ensure 'u_phone' is the correct field name
            }
        }
        return JSON.stringify(result); // Return the result as a JSON string
    },

 

 

Please mark helpful & correct answer if it's really worthy for you.

 

 

Thanks,

BK

 

Thank you, I adjusted the scripts and it still is not working....

Hi @Rhonda9 ,

 

ui type is all right for client script ?

 

Bhavya11_0-1730883760216.png

 

I tried all and mobile.