script to fill "assigned to" based on email string

VernYerem
Tera Expert

We have some assets that have email addresses in a text field "assignmentPlaceholder". I'm trying to create a script to loop though these assets, search the text email in sys_user.email, and apply the user record to "assigned to". I've tried the following script, which seems to not query the sys_user table, as it is applying the text "sys_user" as the assigned to:


var asset = new GlideRecord('alm_hardware');

//Query for specific asset for testing

asset.addQuery('serial_number','123456');
asset.query();
while(asset.next()){
if (asset.assigned_to.nil()){
var user = new GlideRecord('sys_user');
user.addQuery('email', asset.assignmentPlaceholder);
user.query();
asset.assigned_to = user;
asset.update();
}
}

 

The issue seems to be how/when I'm searching for the user. Using 'user.sys_id' fails completely and doesn't update the record, and using 'user' applied the text "sys_user" to assigned to:

VernYerem_0-1727797555980.png

 

2 ACCEPTED SOLUTIONS

debendudas
Mega Sage

Update the script as below and check if it works:

 

var asset = new GlideRecord("alm_hardware");

//Query for specific asset for testing

asset.addQuery("serial_number", "123456");
asset.query();
while (asset.next()) {
  if (asset.assigned_to.nil()) {
    var user = new GlideRecord("sys_user");
    user.addQuery("email", asset.assignmentPlaceholder);
    user.query();
    if (user.next()) {
      asset.assigned_to = user.getUniqueValue();
      asset.update();
    }
  }
}

 

View solution in original post

Brad Bowman
Kilo Patron
Kilo Patron

The assigned_to assignment value should definitely be 

asset.assigned_to = user.sys_id;

You are not returning a record in the user GlideRecord, so after user.query() you should have

if (user.next()) {
    asset.assigned_to = user.sys_id;
    asset.update();
}
}
}

You may also still not be returning the intended user record.  assignmentPlaceholder is not the name of an out of the box field on the alm_hardware table.  If you have added this field in your environment, check the column name - perhaps it is u_assignmentplaceholder?  If a GlideRecord addQuery cannot resolve, it will be ignored, so a random user record will be returned instead of the one with a matching email. 

View solution in original post

3 REPLIES 3

debendudas
Mega Sage

Update the script as below and check if it works:

 

var asset = new GlideRecord("alm_hardware");

//Query for specific asset for testing

asset.addQuery("serial_number", "123456");
asset.query();
while (asset.next()) {
  if (asset.assigned_to.nil()) {
    var user = new GlideRecord("sys_user");
    user.addQuery("email", asset.assignmentPlaceholder);
    user.query();
    if (user.next()) {
      asset.assigned_to = user.getUniqueValue();
      asset.update();
    }
  }
}

 

Brad Bowman
Kilo Patron
Kilo Patron

The assigned_to assignment value should definitely be 

asset.assigned_to = user.sys_id;

You are not returning a record in the user GlideRecord, so after user.query() you should have

if (user.next()) {
    asset.assigned_to = user.sys_id;
    asset.update();
}
}
}

You may also still not be returning the intended user record.  assignmentPlaceholder is not the name of an out of the box field on the alm_hardware table.  If you have added this field in your environment, check the column name - perhaps it is u_assignmentplaceholder?  If a GlideRecord addQuery cannot resolve, it will be ignored, so a random user record will be returned instead of the one with a matching email. 

Ah okay, I assumed since there would only be a single record, it would have worked. Make sense because how would it know one vs many was the result. 

 

Also, you're correct with the field name. "assignedPlaceholder" isn't the actual name of the field, and the real one does start with "u_". What our field was named was confusing, so I used a clearer name in this example.