Unable to return true or false with transform map field mapping

Liam Rhodes
Kilo Guru

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!

1 ACCEPTED SOLUTION

Community Alums
Not applicable

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!

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

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!

Thanks timmo, returning them as string values has done the trick!

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try to return 'true' and 'false' as string values.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader