Create a script include and a client script to show "caller" email and phone number as a field msg

jordanbenbelaid
Tera Contributor

Trying to create a script include and a client script to show the "caller" info (email and phone number) as a field message under caller, that changes dynamically when the "caller is changed in an incident.

 

I have tried about 15 different script includes and client scripts, here are the latest that I have done

 

SCRIPT INCLUDE:

 

// CallerInfoScriptInclude script include
var CallerInfoScriptInclude = Class.create();
CallerInfoScriptInclude.prototype = {
  initialize: function() {},

  getCallerInformation: function(callerID) {
    var caller = new GlideRecord('sys_user');
    if (caller.get(callerID)) {
      var email = caller.email.getDisplayValue();
      var phoneNumber = caller.phone.getDisplayValue();
      return "Email: " + email + "\nPhone Number: " + phoneNumber;
    } else {
      gs.error("CallerInfoScriptInclude - Unable to find user with ID: " + callerID);
      return "Error retrieving caller information.";
    }
  },

  type: 'CallerInfoScriptInclude'
};
 
CLIENT SCRIPT:
function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }

  // Replace 'caller_id' with the actual name of the caller reference field on your Incident table
  var callerField = g_form.getControl('caller_id');

  // Use GlideAjax to call the server-side script include
  var ga = new GlideAjax('CallerInfoScriptInclude');
  ga.addParam('sysparm_name', 'getCallerInformation');
  ga.addParam('sysparm_user_id', newValue);

  ga.getXMLAnswer(function(response) {
    if (response) {
      // Remove existing messages before adding a new one
      g_form.clearMessages();

      // Display the information as a message in the caller field
      g_form.showFieldMsg('caller_id', response);
    }
  });
}
 
Any help would be appreciated, at the moment nothing comes up but before with a slightly different script I was getting a TypeError
1 ACCEPTED SOLUTION

Taking a quick stab at this.

 

Following the recommended change, I just noticed the following lines

 

 

getCallerInformation: function(callerID) {
    var caller = new GlideRecord('sys_user');
    if (caller.get(callerID)) {

 

 

There is no parameter to pass so...

 

 

getCallerInformation: function() {
    var callerID = this.getParameter('sysparm_user_id');
    var caller = new GlideRecord('sys_user');
    if (caller.get(callerID)) {

 

 

 

EDIT: Just wanted to add, that from a programming practice, I would recommend setting the GlideRecord variable to something a lot clearer, so that you know the datatype of the variable, eg. var caller --> var grCaller

 

View solution in original post

14 REPLIES 14

Anurag Tripathi
Mega Patron
Mega Patron

Don't think you have client callable checked don your script include.

-Anurag

I have the scope globally and I have the client callable field ticked.

The first 2 lines of script include should be as below if client callable is true, you need to correct it

var CallerInfoScriptInclude  = Class.create();
CallerInfoScriptInclude .prototype = Object.extendsObject(AbstractAjaxProcessor, {
-Anurag

This actually did help! But I am now getting "Error retrieving caller information." but it is in the right place!