- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 01:00 PM
Hello friends,
We are importing computers and related information from Intune and recently we found out that some computers are being transferred to other folks, for example, when user John Doe leaves the company, his computer is then refreshed and assigned to another user.
When the support team forgets to wipe the old information from Intune, this causes two records for same serail number to be imported into the CMDB, which has only one record.
We want to prevent this situation by allowing the import set to update only the most recent discovery.
I tried below onBefore script in the transform map, but it didn't work.
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
if(source.u_lastsyncdatetime >= 'javascript:gs.beginningOfLast7Days()')
ignore = true;
})(source, map, log, target);
What should I do to allow a record update only if the source field u_lastsyncdatetime is from the last 7 days?
Thanks in advance,
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 01:25 PM
Hi,
You will first have to convert the u_lastsyncdatetime into a GlideDatetime object (it's probably a string when imported), and then compare the value with a date that is seven days old.
Try something like this, and see if it works for you.
var u_lastsyncdatetime = '2024-06-20';
var sevenDaysAgoGDT = new GlideDateTime();
sevenDaysAgoGDT.addDaysUTC(-7);
var importedDate = new GlideDateTime(u_lastsyncdatetime);
if (importedDate.before(sevenDaysAgoGDT)){
gs.info('Imported date is too old, will be skipped');
// set the ignore = true here
}
else{
// might not be needed, just in case you want some other logic happening with valid dates
gs.info('imported date is good');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 01:25 PM
Hi,
You will first have to convert the u_lastsyncdatetime into a GlideDatetime object (it's probably a string when imported), and then compare the value with a date that is seven days old.
Try something like this, and see if it works for you.
var u_lastsyncdatetime = '2024-06-20';
var sevenDaysAgoGDT = new GlideDateTime();
sevenDaysAgoGDT.addDaysUTC(-7);
var importedDate = new GlideDateTime(u_lastsyncdatetime);
if (importedDate.before(sevenDaysAgoGDT)){
gs.info('Imported date is too old, will be skipped');
// set the ignore = true here
}
else{
// might not be needed, just in case you want some other logic happening with valid dates
gs.info('imported date is good');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 01:41 PM - edited 07-03-2024 01:59 PM
Hi @OlaN
Thanks for quick response.
Indeed, I forgot to mention, the Last Sync Date is being handled in the transform field map.
answer = (function transformEntry(source) {
// Add your code here
var source_iso = source.u_lastsyncdatetime; // user source.u_coulmn to set the value here
if (source_iso.indexOf(".") == -1) {
source_iso = source_iso.replace("Z", ".000Z");
}
//Built In methods
var d = new Date(source_iso);
var dateTime = new GlideDateTime(source_iso);
var month = dateTime.getMonthUTC();
if (month <= 9) {
month = "0" + month;
}
var day = d.getUTCDate();
var year = d.getUTCFullYear();
var hours = d.getUTCHours();
var minutes = d.getUTCMinutes();
var seconds = d.getUTCSeconds();
// You may wanna change the below line to your specific Date tme format, here i am using YYY-MM-dd HH:mm:ss
var userDateFormat = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
return userDateFormat; // return the value to be put into the target field
})(source);
The script above was created by someone else.
I'll give a try on your example and update here.
EDIT:
Running onBefore didn't work, it ignored all records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 02:04 PM
Hi @OlaN ,
Thanks again, man!
I joined your script with the one that updates the field map for that Last Sync Date and worked fine!
Here is the updated onBefore script:
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
var source_iso = source.u_lastsyncdatetime; // user source.u_coulmn to set the value here
if (source_iso.indexOf(".") == -1) {
source_iso = source_iso.replace("Z", ".000Z");
}
//Built In methods
var d = new Date(source_iso);
var dateTime = new GlideDateTime(source_iso);
var month = dateTime.getMonthUTC();
if (month <= 9) {
month = "0" + month;
}
var day = d.getUTCDate();
var year = d.getUTCFullYear();
var hours = d.getUTCHours();
var minutes = d.getUTCMinutes();
var seconds = d.getUTCSeconds();
// You may wanna change the below line to your specific Date tme format, here i am using YYY-MM-dd HH:mm:ss
var userDateFormat = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
//return userDateFormat; // return the value to be put into the target field
//var u_lastsyncdatetime = '2024-06-20';
var sevenDaysAgoGDT = new GlideDateTime();
sevenDaysAgoGDT.addDaysUTC(-7);
var importedDate = new GlideDateTime(userDateFormat);
if (importedDate.before(sevenDaysAgoGDT)) {
gs.info('Imported date is too old, will be skipped');
ignore = true;
} else {
// might not be needed, just in case you want some other logic happening with valid dates
gs.info('imported date is good');
update = true;
}
})(source, map, log, target);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 12:46 AM
Great that you got it working.
Just don't forget to comment out, or remove the gs.info() lines I added.
They will only fill the syslog, and are not needed.