- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2024 04:57 PM
Hello,
This is the Transform Map onBefore script. The code compares each user in the user table against the source to determine if they have a ServiceNow account. If not, it skips creating an incident and sends an email to the support team for those users who do not have an account in SN. Currently, it sends separate emails for each ignored user.
Given my limited experience in JavaScript, could someone please assist me in modifying it to send just one email listing all the users without ServiceNow accounts? Thanks to all.
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var ignoredUsers = [];
// Query the source table
var gr_source = new GlideRecord('u_import_data_incident');
gr_source.query();
if (gr_source.next()) {
var senderName = getUserByEmail(source.u_email);
if (senderName && source.u_email == senderName) { // Check if the email matches the senderName
target.caller_id = senderName;
target.description += "USER NAME: " + source.u_caller_id;
target.assignment_group = '57b33d19db1f14107c425638dc9619fd';
} else {
ignore = true; // Ignore the record creation
ignoredUsers.push(gr_source.u_email); //If no match is found, adds it to the array
gs.log("Email does not match the sender name. Ignoring record creation: " + source.u_email);
if (ignoredUsers.length > 0) {
// Prepare email body
var notFoundUser = 'User Name: ' + source.u_caller_id + '\n' + source.u_email;
// Send email notification
sendEmailNotification('CyberSecurity@ABC.com', notFoundUser);
}
}
}
})(source, map, log, target);
// Function to get user ID by email from the sys_user table
function getUserByEmail(email) {
var userGR = new GlideRecord('sys_user');
if (userGR.get('email', email)) {
return userGR.getValue('user_name'); // Assuming 'user_name' is the sender name
} else {
gs.info("Unable to find user with email: " + email);
return null;
}
}
// Function to send an email notification
function sendEmailNotification(email, notFoundUser) {
gs.eventQueue('import.data.user.not.found', source, email, notFoundUser);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2024 06:17 PM
Hi, I don't think there is a lot of complicated javascript involved, and now might be the ideal time to improve your skills; AQlso if you break it down into small pieces it instantly becomes way less complicated.
Start by adding an onStart script that instantiates an array based on the details in the linked threads. IE
this.myArray = [];
If this saves without error, then in the start script push any value into it.
this.myArray.push('test');
Then see if you can read\log the results in a before script eg
log.info('myArray | before ' + this.myArray[0].toString()); // this might be gs.info and not log.info
Hopefully at this point when you import and transform a record, you see 'myArray | before test' in your system log for each row in your import.
If you do, then add an onComplete script
log.info('myArray | complete ' + this.myArray[0].toString()); // this might be gs.info and not log.info
and try again, hopefully you see 1 'compete' log entry?
if yes, go back to your before script and try pushing a value into element 1 IE
this.myArray.push('Element1');
(if importing more than 1 row this would add a new element to the array for each row)
Then update the onComplete script to see if element 1 is populated
log.info('myArray | complete ' + this.myArray[1].toString());
At this point you have hopefully proved that you can create an array in the start script, populate it in the before script and read it in the complete script.
Then all you need do is update your before script so that you push the invalid user details into the array.
And finally update your complete script so that it creates the sysevent add populates the list of impacted email addresses and you will get a comma separated list using this.myArray.toString();
Give it a try, and see how you get on
Any issues or questions you can update this thread and the community can assist\advise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2024 06:17 PM
Hi, I don't think there is a lot of complicated javascript involved, and now might be the ideal time to improve your skills; AQlso if you break it down into small pieces it instantly becomes way less complicated.
Start by adding an onStart script that instantiates an array based on the details in the linked threads. IE
this.myArray = [];
If this saves without error, then in the start script push any value into it.
this.myArray.push('test');
Then see if you can read\log the results in a before script eg
log.info('myArray | before ' + this.myArray[0].toString()); // this might be gs.info and not log.info
Hopefully at this point when you import and transform a record, you see 'myArray | before test' in your system log for each row in your import.
If you do, then add an onComplete script
log.info('myArray | complete ' + this.myArray[0].toString()); // this might be gs.info and not log.info
and try again, hopefully you see 1 'compete' log entry?
if yes, go back to your before script and try pushing a value into element 1 IE
this.myArray.push('Element1');
(if importing more than 1 row this would add a new element to the array for each row)
Then update the onComplete script to see if element 1 is populated
log.info('myArray | complete ' + this.myArray[1].toString());
At this point you have hopefully proved that you can create an array in the start script, populate it in the before script and read it in the complete script.
Then all you need do is update your before script so that you push the invalid user details into the array.
And finally update your complete script so that it creates the sysevent add populates the list of impacted email addresses and you will get a comma separated list using this.myArray.toString();
Give it a try, and see how you get on
Any issues or questions you can update this thread and the community can assist\advise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2024 11:59 PM
Hello @Tony Chatfield1
Thank you greatly for your encouragement and support. I took the plunge and gave it a shot. Currently, I'm still receiving two emails: one containing details about two ignored users, and the other featuring information about one ignored user. Please help review when you have a chance. Thank you again.
Emails Log:
onStart:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
this.ignoredUsers = [];
// this.ignoredUsers.push('test');
gs.info('myArray | before ' + this.ignoredUsers[0].toString());
})(source, map, log, target);
onBefore:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Query the user table
var myUser = source.u_email;
var userCheck = new GlideRecord('sys_user');
if (userCheck.get('email', myUser)) {
//Do something becuase there is a match
target.caller_id = userCheck.sys_id;
target.description += "USER NAME: " + userCheck.name; //should this be userCheck.name ?
target.assignment_group = '57b33d19db1f14107c425638dc9619fd';
} else {
//Do something else as there is not a match.
ignore = true;
// Prepare email body
var notFoundUser = 'User Name: ' + source.u_caller_id;
this.ignoredUsers.push(notFoundUser);
// Send email notification
if (this.ignoredUsers.length > 0) {
// Prepare email body
var emailBody = "";
emailBody += this.ignoredUsers.join('<br/>');
// Send email notification
gs.eventQueue('import.data.user.not.found', source, 'ABC@yahoo.com', emailBody);
}
}
})(source, map, log, target);
onCompete:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
gs.info('myArray | complete ' + this.ignoredUsers[0].toString());
})(source, map, log, target);
Log:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2024 02:38 PM
Hello @Tony Chatfield1
Thank you for your clear instructions; they helped me fulfill the requirement. I need to transfer some code from the "onbefore" section to the "onComplete" section, as illustrated in the screenshot below.
To be honest, it would have taken me some time to figure it out without your guidance. I'm grateful that our community has professional programmers like you who are willing to teach and share their experiences. It makes a significant difference to the community.
I have one last question. Currently, the email is being sent to the support team at "ABC@yahoo.com". Is it possible to send it to the login user who triggered the Transform Map instead? This is just for educational purposes.
if (this.ignoredUsers.length > 0) {
// Prepare email body
var emailBody = "";
emailBody += this.ignoredUsers.join('<br/>');
// Send email notification
gs.eventQueue('import.data.user.not.found', source, 'ABC@yahoo.com', emailBody);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2024 01:35 PM
Hi @Meera_P it's excellent news to hear that you have a working solution.
Regarding your email question, there is a getEmail() method that will return the 'email' address of your logged in user
GlideUser | ServiceNow Developers
So something like this should work.
var myEmail = gs.getUser().getEmail();
// Send email notification
gs.eventQueue('import.data.user.not.found', source, myEmail, emailBody);
Again, well done and great job.
Too often I see community posts that are nothing more than a request for the community to write someone's code for them; and I think it's awesome that you made an effort, worked through your requirements and ended up with a functional result.