passing value from server script to client/html?

Prem13
Tera Contributor

Unable to retrieve the value which is being passed from server script

Server script:-

data.name = '';

data.email = '';

// get current user email
var uemail; 
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id', gs.getUserID());
usr.query();
if(usr.next())
{
data.email = usr.email;
}

client script:-

var temp = c.data.email;
	alert(JSON.stringify(c.data.email));

 

Alert returns empty { }

 

1 ACCEPTED SOLUTION

Assumption - you are using Service Portal. If this is standard lists/forms, please clarify. The approach is totally different.

Note: Always assume (because it's true) that fields are objects. If you want the value, use getValue() or getDisplayValue().

Server script:

data.email = '';

var usr = new GlideRecord('sys_user');
if (usr.get(gs.getUserID()) {
  data.email = usr.getValue('email');
  if (data.email == '') {
    gs.error('This user has no email');
  }
} else {
  gs.error('Unable to read current user record');
}

Client script:

console.log('Email=' + c.data.email);

View solution in original post

6 REPLIES 6

ChrisBurks
Mega Sage

Try usr.getDisplayValue('email'). 

If not in a scoped application try using usr.getValue('email').

Or using the string method on it: usr.email.toString();

 

i was able to get the value in console.log for the server script

but in client controller its showing empty value

Assumption - you are using Service Portal. If this is standard lists/forms, please clarify. The approach is totally different.

Note: Always assume (because it's true) that fields are objects. If you want the value, use getValue() or getDisplayValue().

Server script:

data.email = '';

var usr = new GlideRecord('sys_user');
if (usr.get(gs.getUserID()) {
  data.email = usr.getValue('email');
  if (data.email == '') {
    gs.error('This user has no email');
  }
} else {
  gs.error('Unable to read current user record');
}

Client script:

console.log('Email=' + c.data.email);

Sagar Pagar
Tera Patron

Hi,

 

Try this,

 Script Include:

    getDetails: function() {
        var user_sys_id = this.getParameter('sysparm_userid');
        var user = new GlideRecord('sys_user');
        var result = {
                email:""
        };

        if (user.get(user_sys_id)) {
            result.email = user.email;
        }
        return JSON.stringify(result);
    },

 OnLoad client script:

    var ga=new GlideAjax('scriptIncludeName');
    ga.addParam('sysparm_name','getDetails');
    ga.addParam('sysparm_userid',gs.getUserID());
    ga.getXML(responseParse);
    
      function responseParse(response){
      var answer= response.responseXML.documentElement.getAttribute("answer"); 
        var res = JSON.parse(answer);

        g_form.setValue('field_name',res.email); 

   }

 

Regards,

Sagar Pagar

The world works with ServiceNow