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

Sorry I thought this worked but turned out the last logged on user was already populated on my tested assets 

I have tried to run it as a before and after but its still not populating the field in the CMDB

Is the "u_last_logged_on_user" field a reference field?

Yes â€ƒ

Try the below updated 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;
    }
}
var targetUser = '';
var userName = firstName + ' ' + surname;
var getUserID = new GlideRecord('sys_user');
getUserID.addEncodedQuery('name='+userName);
getUserID.query();
if(getUserID.next()){
    targetUser = getUserID.getUniqueValue();
}
target.u_last_logged_on_user = targetUser;