Transform Map / Transform Script

MStritt
Tera Guru

I am creating a transform script on a source field, and I'm wondering if I can add multiple values/entries in the script. Instead of creating multiple lines for each color, add them in one line. If so, what the syntax would be.

 

For example: 

if (source.u_color == 'Black' 'Blue') {
return 'White';

 

Code:

answer = (function transformEntry(source) {

if (source.u_color == 'Black') {
return 'White';
}
if (source.u_color == 'Blue') {
return 'White';
}

})(source);

 

5 REPLIES 5

DrewW
Mega Sage
Mega Sage

Assuming that u_color is just a string field you could just use indexOf to check the string for the value and then do an OR for your if.

var color = ('' + source.u_color);
if (color.indexOf('Black') != -1 || color.indexOf('Blue') != -1) {
   return 'White';
}

 

Not sure you are going to get any better than that.

Hi Drew,

 

It's a choice field.

Then you can skip the indexOf part since a choice field is a text field with specific values in it.  @AnubhavRitolia posted the code you could use.  I thought you where going to pass multiple values at a time.

AnubhavRitolia
Mega Sage
Mega Sage

Hi @MStritt 

 

Can try like below:

 

if (source.color == 'Black' || source.color == 'Blue') {
   return 'White';
}

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023