- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 08:20 AM
Hi all,
I am trying to create a field map using a script to return a true or false value. I previously had this working in an onAfter script as you can see below. However we had to remove this as it was causing issues and creating (empty) users due to a conflict with another transform script.
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
if (source.u_extensionattribute6.indexOf('DaaS') > -1){
target.u_daas_user = true;
}
if (source.u_extensionattribute6.indexOf('DaaS') == -1){
target.u_daas_user = false;
}
target.update();
})(source, map, log, target);
So instead I have tried to take the above script and put it into a field map as below to try and return either a true or false result depending on if the extensionattribute6 field contains 'DaaS' or not.
answer = (function transformEntry(source) {
if (source.u_extensionattribute6.indexOf('DaaS') > -1){
return true;
}
if (source.u_extensionattribute6.indexOf('DaaS') == -1){
return false;
}
})(source);
Unfortunately it doesn't seem to be working in this format. It should be setting a true|false field on the user table to either true or false depending on DaaS being present in extensionattribute6 (true - ticked box, or false - untick box if ticked).
Clearly I've got the format of the script all wrong. Any help would be greatly appreciated!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 08:37 AM
This looks like it should work
Keep in mind, your 'if should return a true of false, and you're essentially checking the same thing twice. This will return a true or false:
source.u_extensionattribute6.indexOf('DaaS') > -1
Try returning a string instead:
if (source.u_extensionattribute6.indexOf('DaaS') > -1){
return 'true';
}
Seems lame, however ServiceNow generally likes to work with strings.
Also, can you validate what's happening using log statements?
answer = (function transformEntry(source) {
gs.info('Logging inside script');
if (source.u_extensionattribute6.indexOf('DaaS') > -1){
gs.info('We have true');
return true;
}
if (source.u_extensionattribute6.indexOf('DaaS') == -1){
gs.info('We have false');
return false;
}
})(source);
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 08:37 AM
This looks like it should work
Keep in mind, your 'if should return a true of false, and you're essentially checking the same thing twice. This will return a true or false:
source.u_extensionattribute6.indexOf('DaaS') > -1
Try returning a string instead:
if (source.u_extensionattribute6.indexOf('DaaS') > -1){
return 'true';
}
Seems lame, however ServiceNow generally likes to work with strings.
Also, can you validate what's happening using log statements?
answer = (function transformEntry(source) {
gs.info('Logging inside script');
if (source.u_extensionattribute6.indexOf('DaaS') > -1){
gs.info('We have true');
return true;
}
if (source.u_extensionattribute6.indexOf('DaaS') == -1){
gs.info('We have false');
return false;
}
})(source);
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2022 02:57 AM
Thanks timmo, returning them as string values has done the trick!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 08:38 AM
Hi,
try to return 'true' and 'false' as string values.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader