
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2021 06:33 AM
Hi,
I'm working towards implementing an email integration and I am at the stage where I created the transform map, which works fine, however the table that gets the data import (sys_user) also has 2 fields that calculate the value based on other fields (which in turn get their data from the import).
Just to bring it to life:
-> contract start date (field gets populated via import)
-> 2 year tenure (fields gets populated based on the contract start date field value)
The thing is that before creating the data source and transform map I created a client script "onChange" to run on the 2 year tenure field so it would calculate the value based on the contract start date. This does work if I add the value manually, however it does not when the transform map is run.
I would appreciate some help on how to amend this - do I need to change the client script from "onChange" to "onLoad" or something else and can you point me to some scripts that could help me achieve that, as I don't have any experience in this dpt.?
Thank you 🙂
Paula
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2021 04:48 AM
Hi,
small change
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
//update the fields if there are existent values
if(action != "update"){
//empty
}
var Ctype = source.u_contract_type.toString();
var gdt = new GlideDateTime('2199-12-31');
//If any other value apart from Permanent, Contractor, FTC
if (Ctype != "FTC" && Ctype != "Permanent" && Ctype != "Contractor") {
ignore = true;
}
// Set value to 31st Dec 2199 if the contract type is "Permanent"
if (Ctype == "Permanent") {
target.setValue('u_contract_end_date', gdt);
}
else if (Ctype != "Permanent") {
ignore = true;
}
}
)(source, map, log, target);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2021 06:51 AM
Client script will only run from the front-end when somebody changes the value.
Client script won't help for data loaded from transform map
In your case you can use transform map onBefore script to set the 2 year tenure date based on Contract Start date
Sample script for help
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
var contractValue = source.u_contract_start_date;
// do the processing here like add 2 years etc to this date and then set it on the another field
target.2_year_tenure_field = 'Your value here';
})(source, map, log, target);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 08:07 AM
Thank you
I do have 2 follow up questions if that's ok:
-> I imagine that any transform map script would only run if the fields get populated via the import right? Would not work if someone would update the fields manually (i.e. the contract start date would be updated and then it would trigger the tenure field to get updated as well)
-> in the script you provided, what formula would you recommend to use to add those 2 years?
Thank you,
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 08:40 AM
Hi,
That's correct.
If you wish to add 2 years when data is being loaded then you will have to use transform map script
If you wish to make this work from UI when manual change happens you can use before insert/update Business rule
Script for adding 2 years
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
var contractValue = source.u_contract_start_date;
// do the processing here like add 2 years etc to this date and then set it on the another field
var gdt = new GlideDateTime(contractValue);
gdt.addYearsUTC(2);
target.setValue('2_year_tenure_field', gdt);
})(source, map, log, target);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2021 08:54 AM
I think I will create a business rule to have the tenure field updated as I want to make it possible to get updated when someone also updates manually the start date field.
May I ask something on the transform scripts?
-> can we create more than one script of the same type "when" (i.e. onBefore, onStart, etc?) with different order or is it best practice to collate the conditions into 1? I am asking this as I am looking now to apply to my transform map:
-> set Contract end date field as 31/12/2199 if the Contract type is 'Permanent' and
-> a short script to also update the fields for future data sources being ran against the same records (hope this makes sense)
Thanks again for all your help!!