How to receive 2 variables from a JSON from a Script Include?

Lucas Rodrigues
Tera Contributor

My client script is receiving 1 variable from a script include. But I want that this script include gives me 2 variables. How can I do that?

3 REPLIES 3

Community Alums
Not applicable

Hi @Lucas Rodrigues ,

If you meant to say, How to pass two arguments/parameters in a function of script include?

 

If yes, you can pass arguments one by one and can receive the same in script Include function in the same order.

For example. -

 

 

//calling a script include function with two vairable from different script.
var aaa = 3;
var bbb = 5;
var num1 = new ScriptIncludeName.functionName(aaa, bbb);

//Script include function
function: functionName(aaa, bbb){
   //function body
}

 

Sandeep Rajput
Tera Patron
Tera Patron

@Lucas Rodrigues You have two options to do it.

1. Your script include method should return a key/value pair

OR

2. Your script include returns an array of variables.

 

Here is an example of the first approach.

 

Client script:

 

function onLoad() {
  // Get the sys_id of the current user.
  var userId = g_user.userID;

  // Call the server-side script include to get user details.
  getUserDetails(userId);
}

function getUserDetails(userId) {
  var ga = new GlideAjax('UserDetailsScriptInclude');
  ga.addParam('sysparm_name', 'getUserInfo');
  ga.addParam('sysparm_user_id', userId);
  ga.getXML(userDetailsCallback);
}

function userDetailsCallback(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  var userDetails = JSON.parse(answer);

  if (userDetails) {
    var userName = userDetails.name;
    var userDob = userDetails.dob;

    // Populate form fields or display the data.
    g_form.setValue('user_name', userName); // Replace 'user_name' with your field name
    g_form.setValue('user_dob', userDob);    // Replace 'user_dob' with your field name

    // Optionally display an alert:
    // alert("User Name: " + userName + "\nDate of Birth: " + userDob);
  } else {
    // Optional error message
    //alert("User details not found.");
  }
}

Script Include:

 

var UserDetailsScriptInclude = Class.create();
UserDetailsScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getUserInfo: function() {
    var userId = this.getParameter('sysparm_user_id');
    var user = new GlideRecord('sys_user');

    if (user.get(userId)) {
      var userName = user.name.toString();
      var userDob = user.dob.getDisplayValue();
      var userDetails = {
        name: userName,
        dob: userDob
      };
      return JSON.stringify(userDetails);
    } else {
      return null;
    }
  },

  type: 'UserDetailsScriptInclude'
});

Hope this helps.

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucas Rodrigues 

you can use JSON and return it from script include and then parse in client script

check these links

GlideAjax Example Cheat Sheet 

GlideAjax | How to return multiple values from Script Include to Client Script 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader