Loading string value from field to a reference field

lrossy31
Tera Expert

Hello everyone,

 

I have to load the value of a string field that contains data migrated (name loaded last name then first name) to invert the name and load it into a reference field that references the user table. I have tried split method but when trying to Display it to reference field I cannot get the value.

I will greatly appreciate if anyone can help me with this.

5 REPLIES 5

Riya Verma
Kilo Sage
Kilo Sage

HI @lrossy31 ,

 

Hope you are doing great.

 

  • Create a business rule that triggers on the insertion of the record.
  • In the script, use JavaScript to split the string into first name and last name.
  • Then, construct a query to find the corresponding user in the user table.
  • Finally, set the reference field to the found user.

 

Reference script for same :

(function executeRule(current, previous) {
    // Assuming 'name' is the string field and 'assigned_to' is the reference field.
    var fullName = current.name; // Assuming the format is "Last Name First Name".
    var nameParts = fullName.split(' '); // Splitting the full name.
    
    var gr = new GlideRecord('sys_user'); // Creating a GlideRecord object for the user table.
    gr.addQuery('first_name', nameParts[1]); // Assuming first name is at index 1.
    gr.addQuery('last_name', nameParts[0]); // Assuming last name is at index 0.
    gr.query(); // Executing the query.

    if (gr.next()) {
        current.assigned_to = gr.sys_id; // Assuming 'assigned_to' is the reference field.
    }
})(current, previous);
 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma