Using GlideRecord Query in Transform Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 05:26 PM
Hello,
I'm trying to import records from a CSV file to create RITMs based on the column fields. One of the fields in the CSV is the username that is formatted LastName, FirstName. I'm using the Load Data tool and a Transform Script (onBefore) to read through each row, match the column with the RITM variable, and create a RITM. The script works and creates new RITMs to assign the values to the variable. However, it does not work when I add the line that calls my reformat function.
I want the Requested For and Opened By to be assigned to the user I query using GlideRecord, but for now I am trying to just get the name reformat to output to the Notes field in my RITM. However, it doesn't create any new RITMs when I try the reformat function on its own. That makes me think that something goes wrong when I call that function. I did test my reformat function in a JavaScript console and it returns the correct output there. Any pointers on what I could be doing wrong? If I get the reformat working, will my GlideRecord query work as well?
Thank you! Below is my script:
(function runTransformScript(source, map, log, target /*undefined onStart*/) {
// Add your code here
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('060f3afa3731300054b6a3549dbe5d3e');
cart.setVariable(item, 'division', source.u_division);
cart.setVariable(item, 'amount', source.u_amount);
// This outputs correctly on its own
// cart.setVariable(item, 'notes', source.u_user);
// Output to notes field to test if email is formatting correctly - doesn't create RITMs
var email = reformat(source.u_user);
cart.setVariable(item, 'notes', email);
// cart.setVariable(item, 'requested_for', getUserSysId(email));
var rc = cart.placeOrder();
})(source, map, log, target);
// Username is formatted as LastName, FirstName
function reformat(name) {
// Split the last and first names
let [lastName, firstName] = name.split(', ');
// Return the reformatted name
return firstName + '.' + lastName + '@company.com';
}
function getUserSysId(email) {
var users = new GlideRecord('sys_user');
var sysId;
users.addQuery('email', email);
users.query();
if (users.next()) {
sysId = users.getValue('sys_id');
}
return sysId;
}