How to bring multiple values from script include to Clint Script??

KM SN
Tera Expert

In incident form if i change a caller field his information i.e department,  mail, manager, phone number should be auto populate in description field.

 

I need to store values/push values in string and them have to get same array to CS and then set description to as string.

 

 

Any inputs? I am unable to store values in string and set same string to description through client script?

1 ACCEPTED SOLUTION

Shraddha Kadam
Mega Sage

Hello @KM SN ,

 

Hope you are well !

 

Please create a Client callable Script Include and Client Script. For your reference please find below script :

 

Script Include :

returnEmployeeDetails: function() {
        var data = '';
        var employee = this.getParameter('sysparm_user');
        var grEmployee = GlideRecord('sys_user');
        grEmployee.addQuery('sys_id', employee);
        grEmployee.query();
        while (grEmployee.next()) {
            data = grEmployee.first_name + ',' + grEmployee.last_name + ',' + grEmployee.email + ',' + grEmployee.title + ',' + grEmployee.location;


            return data;

        }

    },

Client Script :

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

    var employee = g_form.getValue('employee');
    if (employee != '') {
        var gr = new GlideAjax('ICClientCallableScriptInclude');
        gr.addParam('sysparm_name', 'returnEmployeeDetails');
        gr.addParam('sysparm_user', employee);
        gr.getXML(getResponse);
    } else {
        g_form.clearValue('first_name');
        g_form.clearValue('last_name');
        g_form.clearValue('email');
        g_form.clearValue('employee_job_title');
        g_form.clearValue('location');
    }

    function getResponse(response) {
        var values = response.responseXML.documentElement.getAttribute('answer').toString().split(',');
        g_form.setValue('description', values[0] + values[1] + values[2] + values[3] + values[4]);
    }
   
}

 Please mark my answer helpful and accept the solution.

 

Appreciate your help !

 

Thank you.

If my response was helpful, please mark it as correct and helpful.
Thank you.

View solution in original post

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, if you need to do populate this data into the form UI then you would normally use GlideAjax to retrieve the data you need.

GlideAjax | ServiceNow Developers

It is possible to use getReference() (or GlideRecord) but neither of these are recommended best practice.

GlideForm | ServiceNow Developers

GlideRecord | ServiceNow Developers

 

If the data does not need to be visible to the user immediately\onChange and is not used\referenced in the UI form, then another solution would be to use an on before BR to populate the correct values when the record is saved\updated.

Prateek kumar
Mega Sage

Check this out:

https://www.servicenow.com/community/developer-articles/get-user-details-based-on-the-logged-in-user...


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Shraddha Kadam
Mega Sage

Hello @KM SN ,

 

Hope you are well !

 

Please create a Client callable Script Include and Client Script. For your reference please find below script :

 

Script Include :

returnEmployeeDetails: function() {
        var data = '';
        var employee = this.getParameter('sysparm_user');
        var grEmployee = GlideRecord('sys_user');
        grEmployee.addQuery('sys_id', employee);
        grEmployee.query();
        while (grEmployee.next()) {
            data = grEmployee.first_name + ',' + grEmployee.last_name + ',' + grEmployee.email + ',' + grEmployee.title + ',' + grEmployee.location;


            return data;

        }

    },

Client Script :

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

    var employee = g_form.getValue('employee');
    if (employee != '') {
        var gr = new GlideAjax('ICClientCallableScriptInclude');
        gr.addParam('sysparm_name', 'returnEmployeeDetails');
        gr.addParam('sysparm_user', employee);
        gr.getXML(getResponse);
    } else {
        g_form.clearValue('first_name');
        g_form.clearValue('last_name');
        g_form.clearValue('email');
        g_form.clearValue('employee_job_title');
        g_form.clearValue('location');
    }

    function getResponse(response) {
        var values = response.responseXML.documentElement.getAttribute('answer').toString().split(',');
        g_form.setValue('description', values[0] + values[1] + values[2] + values[3] + values[4]);
    }
   
}

 Please mark my answer helpful and accept the solution.

 

Appreciate your help !

 

Thank you.

If my response was helpful, please mark it as correct and helpful.
Thank you.

KM SN
Tera Expert

Any other alternative for  storing values into array and then setting same in description through client script?