glideajax code not working

suresh kaliappa
Tera Expert

Hi all,

 

My requirement is in the catalog item if requested for user is selected then the location updated in the requested for user record should populate in location variable. So i was written on change client script and script include to populate. but it was not working. So i am sharing the code i was written below hence kindly someone review the code and help me if i am anywhere wrong. 

 

Script include:

GetActiveLocation: function()
{

    var userRecord = new GlideRecord('sys_user');
    userRecord.addQuery('sys_id', this.getParameter('sysparm_user'));
    userRecord.query();
    if(userRecord.next())
    {
        var loc = new GlideRecord('cmn_location');
        loc.addQuery('sys_id',userRecord.location);
        loc.query();
        if(loc.next())
        {
        if(loc.u_active !='')
        {
            return loc.full_name;
        }
        }
    }
 
Client script :
 
 var ga = new GlideAjax('GetUserLocation');
   ga.addParam('sysparm_name','GetActiveLocation');
   ga.addParam('sysparm_user', g_form.getValue('requested_for'));
   ga.getXML(CallActiveLocation);

   function CallActiveLocation(response)
   {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('location',answer);

   }

 

 

 

1 ACCEPTED SOLUTION

@suresh kaliappa If the location is a reference variable then you should return loc.getValue('sys_id'); from script include, instead of loc.full_name;

 

Please try with the following script include.

 

var GetUserLocation = Class.create();
GetUserLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    GetActiveLocation: function() {
        var userId = this.getParameter('sysparm_user');
        var userRecord = new GlideRecord('sys_user');
        userRecord.get(userId);

        if (userRecord.location) {
            var loc = new GlideRecord('cmn_location');
            loc.get(userRecord.location);

            if (loc.u_active == 'true') { // Ensure the custom active field is checked
                return loc.getValue('sys_id');
            }
        }
        return ''; // Return empty if no active location found
    },

    type: 'GetUserLocation'
});

Hope this helps.

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@suresh kaliappa Please check if your script include is a client callable script include. Also is location on your form a string field or a reference field?

Hi Sandeep , 

 

location variable is a reference variable. 

@suresh kaliappa If the location is a reference variable then you should return loc.getValue('sys_id'); from script include, instead of loc.full_name;

 

Please try with the following script include.

 

var GetUserLocation = Class.create();
GetUserLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    GetActiveLocation: function() {
        var userId = this.getParameter('sysparm_user');
        var userRecord = new GlideRecord('sys_user');
        userRecord.get(userId);

        if (userRecord.location) {
            var loc = new GlideRecord('cmn_location');
            loc.get(userRecord.location);

            if (loc.u_active == 'true') { // Ensure the custom active field is checked
                return loc.getValue('sys_id');
            }
        }
        return ''; // Return empty if no active location found
    },

    type: 'GetUserLocation'
});

Hope this helps.

Yashsvi
Kilo Sage

Hi @suresh kaliappa,

Script Include:

var GetUserLocation = Class.create();
GetUserLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    GetActiveLocation: function() {
        var userId = this.getParameter('sysparm_user');
        var userRecord = new GlideRecord('sys_user');
        userRecord.get(userId);

        if (userRecord.location) {
            var loc = new GlideRecord('cmn_location');
            loc.get(userRecord.location);

            if (loc.u_active == 'true') { // Ensure the custom active field is checked
                return loc.full_name;
            }
        }
        return ''; // Return empty if no active location found
    },

    type: 'GetUserLocation'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var ga = new GlideAjax('GetUserLocation');
    ga.addParam('sysparm_name', 'GetActiveLocation');
    ga.addParam('sysparm_user', g_form.getValue('requested_for'));
    ga.getXMLAnswer(function(response) {
        var location = response;
        g_form.setValue('location', location);
    });
}

Thank you, please make helpful if you accept the solution.