Help with Transform Script

RobDoyle
Giga Guru

Hi,

I am very new when I comes to writing scripts.

We have implemented Qualys integration into ServiceNow and I am after a script that I can add to the transform scripts that looks at the last logged on user data and transforms it into data ServiceNow can absorb.

The format of the data comes in like this 
PARKDEAN\RobDoyle

ServiceNow of course cannot match this as its not how we display it. I need the script to remove the Parkdean\ and also split the first name and surname 

We have made a custom field to store this called u_last_logged_on_user

Thanks 
 

1 ACCEPTED SOLUTION

Hello @RobDoyle ,

Try the below code.

var inputString = source.asset_lastloggedonuser;//"PARKDEAN\RobDoyle";
var name = inputString.substring(8);

var firstName = '';
var surname = '';

// Find the point where the first name ends and surname begins
for (var i = 1; i < name.length; i++) {
    if (name[i] === name[i].toUpperCase()) { // Assuming that the surname starts with an uppercase letter
        firstName = name.substring(0, i);
        surname = name.substring(i);
        break;
    }
}
target.u_last_logged_on_user = firstName + ' ' + surname;

 

If this response resolves your query, kindly mark it as both helpful and correct.

Thanks,

Alka

View solution in original post

8 REPLIES 8

Arun_Manoj
Mega Sage

Hi @RobDoyle ,

 

Please the script to split the domain and first & last name from the return value.

 

(function transformEntry(source, target, map, log, isUpdate) {
// Extract the last logged on user from the source
var lastLoggedOnUser = source.u_last_logged_on_user;

// Check if the field is not empty
if (lastLoggedOnUser) {
// Remove the domain part
var userWithoutDomain = lastLoggedOnUser.split('\\')[1];

// Split the first name and surname
var nameParts = userWithoutDomain.match(/([A-Z][a-z]+)([A-Z][a-z]+)/);

if (nameParts && nameParts.length === 3) {
// Assign the first name and surname to target fields
target.u_first_name = nameParts[1];
target.u_last_name = nameParts[2];
} else {
// Handle the case where the name format is unexpected
log.warn('Unexpected name format: ' + userWithoutDomain);
}
} else {
// Handle the case where the last logged on user field is empty
log.warn('Last logged on user field is empty');
}
})(source, target, map, log, isUpdate);


Please mark it as helpful. if it is works

 

Thanks

Arun

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @RobDoyle ,

 

 

Please try the script below and replace the input string as needed. This script will remove "PARKDEAN\" and extract the first name and surname from the remaining string.

var inputString = source.u_last_logged_on_user;// will store this value "PARKDEAN\RobDoyle";
var name = inputString.substring(8);

var firstName = '';
var surname = '';

// Find the point where the first name ends and surname begins
for (var i = 1; i < name.length; i++) {
    if (name[i] === name[i].toUpperCase()) { // Assuming that the surname starts with an uppercase letter
        firstName = name.substring(0, i);
        surname = name.substring(i);
        break;
    }
}



 

If this response resolves your query, kindly mark it as both helpful and correct.

Thanks,

Alka

RobDoyle
Giga Guru

Apologies for the confusion 

The source field from Qualys is 

 

asset_lastloggedonuser

target field is 

 

u_last_logged_on_user

 

where we want it to display how the name field displays on the user table 

Hello @RobDoyle ,

Try the below code.

var inputString = source.asset_lastloggedonuser;//"PARKDEAN\RobDoyle";
var name = inputString.substring(8);

var firstName = '';
var surname = '';

// Find the point where the first name ends and surname begins
for (var i = 1; i < name.length; i++) {
    if (name[i] === name[i].toUpperCase()) { // Assuming that the surname starts with an uppercase letter
        firstName = name.substring(0, i);
        surname = name.substring(i);
        break;
    }
}
target.u_last_logged_on_user = firstName + ' ' + surname;

 

If this response resolves your query, kindly mark it as both helpful and correct.

Thanks,

Alka