Get multiple user attibutes in sys_user table by parsing email body text

Joe Taylor
Giga Guru

I'd like to parse an inbound email for a user employee number and use it to retrieve multiple other attributes in the record to populate a form.

 

My sys_user table has an attribute called "u_employee_number"

 

In the email body I'm user this variable pair

requestor:123456

 

In my inbound action script I'm trying 

var userid = new GlideRecord('sys_user');
userid.addQuery('u_employee_number',requestor);
userid.query();
if(userid.next()){
var id = userid.sys_id();

}

 

Then I want use the sys_id in my catalog item:

 

if(requestor!=undefined)
hddcart.setVariable(item,"requestor_name",id);

 

This isn't working.

 

Can you help?

1 ACCEPTED SOLUTION

Hi @Joe Taylor 

 

You can fetch multiple attributes using the same object:

 

 if (email.body.requestor != undefined){
    var userid = new GlideRecord('sys_user');
    userid.addQuery('u_employee_number', email.body.requestor);
    userid.query();
    if(userid.next()){
    var id = userid.getUniqueValue();
    hddcart.setVariable(item,"requestor_name",id);
    hddcart.setVariable(item,"variable1_name",userid.field_name.getValue());
    hddcart.setVariable(item,"variable2_name",userid.field_name.getValue());
    }
}
 
Example:
hddcart.setVariable(item,"variable_name",userid.name.getValue());  //name
hddcart.setVariable(item,"variable_name",userid.title.getValue());   //title
hddcart.setVariable(item,"variable_name",userid.department.getValue());   //department
 
Thanks!
 
Mark it as helpful and correct if it works ✔️👍

View solution in original post

5 REPLIES 5

Sagar Pagar
Tera Patron

Hello @Joe Taylor,

 

Try this updated scripts -

 

 

if (email.body.requestor != undefined){
var userid = new GlideRecord('sys_user');
userid.addQuery('u_employee_number', email.body.requestor);
userid.query();
if(userid.next()){
var id = userid.getUniqueValue();
}
}

 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

Yay!  

This worked to get the sys_id of the requestor using the employee number.

    if (email.body.requestor != undefined){
    var userid = new GlideRecord('sys_user');
    userid.addQuery('u_employee_number', email.body.requestor);
    userid.query();
    if(userid.next()){
    var id = userid.getUniqueValue();
    hddcart.setVariable(item,"requestor_name",id);
    }
}

 

 

Now how do get multiple attributes for this user from the sys_user table to use in my forms?

 

Say I want to get:

sys_id  (This is working now!)

name

title

dept

etc.

Hi @Joe Taylor 

 

You can fetch multiple attributes using the same object:

 

 if (email.body.requestor != undefined){
    var userid = new GlideRecord('sys_user');
    userid.addQuery('u_employee_number', email.body.requestor);
    userid.query();
    if(userid.next()){
    var id = userid.getUniqueValue();
    hddcart.setVariable(item,"requestor_name",id);
    hddcart.setVariable(item,"variable1_name",userid.field_name.getValue());
    hddcart.setVariable(item,"variable2_name",userid.field_name.getValue());
    }
}
 
Example:
hddcart.setVariable(item,"variable_name",userid.name.getValue());  //name
hddcart.setVariable(item,"variable_name",userid.title.getValue());   //title
hddcart.setVariable(item,"variable_name",userid.department.getValue());   //department
 
Thanks!
 
Mark it as helpful and correct if it works ✔️👍

Thanks so much!