How to remove the last characters from the field and put it into the another field in Transform Map script

Mahak2
Kilo Guru

Hello guys,

 

I have a requirement according to which i need to change the field to Uppercase and then remove some characters from the String field after the certain character and put it into the different field .

For Ex :

Input:

Name : abcde.pqr@stu

Output:

Name:ABCDE

Dom:PQR@STU

In short the characters after " . " should be removed from the first field.

I have written a code in transform field map script to change the string to UpperCAse and remove the characters after fullstop.But it is not working.

answer = (function transformEntry(source) {

// Add your code here

var t = source.name.toString();
var s = t.toUpperCase();
var aa = s.substring(0, s.indexOf(' . '));


return aa; // return the value to be put into the target field

})(source);

Can anyone help in this?

 

Regards,

Mahak

1 ACCEPTED SOLUTION

Hi,

So the target field is choice type and hence you are using if else statements

you should do this in field map script and not onbefore

Field Map Script:

answer = (function transformEntry(source) {

	// Add your code here
var tr = source.u_operation_system.toString();

if(tr == 'Red Hat Enterprise Linux Server')
{
return 'Red Hat Ent Linux';
}
else if(tr == 'Oracle Linux Server')
{
return 'Oracle Linux 4/5/6 (64-bit)';
}
else if(tr == 'SUSE Linux Enterprise Server')
{
return 'SUSE Linux Enterprise 11 (64-bit)';
}

})(source);

if you want transform script then use this

(function runTransformScript(source, map, log /*undefined onStart*/ ) {

var tr = source.u_operation_system.toString();

if( tr =='Red Hat Enterprise Linux Server')
{
target.os = 'Red Hat Ent Linux';
}
else if( tr =='Oracle Linux Server')
{
target.os = 'Oracle Linux 4/5/6 (64-bit)';
}
else if(tr =='SUSE Linux Enterprise Server')
{
target.os = 'SUSE Linux Enterprise 11 (64-bit)';
}
else
{
ignore = true;
}

})(source, map, log, target);

Regards
Ankur

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

View solution in original post

10 REPLIES 10

Jaspal Singh
Mega Patron
Mega Patron

Hi Mahak,

 

Can you try replacing

var aa = s.substring(0, s.indexOf(' . '));

with

var aa = s.split('.')[0]; //should give you text before '.'

var aa = s.substring(0, s.indexOf(' . ')); should also work I guess extra spaces are causing an issue.

Replace

var aa = s.substring(0, s.indexOf(' . '));

with

var aa = s.substring(0, s.indexOf('.')); //Removed extra spaces

Hello Jaspal,

Thank you for the reply.

Name is now populating.

I am also running a onbefore transform script to update the name of the Operating system in the target table so that it doesn't create new options due to difference in Naming Convention.

But still it is using the data which is in the source table not setting the one I have set in the script:

(function runTransformScript(source, map, log /*undefined onStart*/ ) {

var tr = source.u_operation_system.toString();

if( tr =='Red Hat Enterprise Linux Server')
{
target.os = 'Red Hat Ent Linux';
ignore = false;
}
else if( tr =='Oracle Linux Server')
{
target.os = 'Oracle Linux 4/5/6 (64-bit)';
ignore = false;
}
else if(tr =='SUSE Linux Enterprise Server')
{
target.os = 'SUSE Linux Enterprise 11 (64-bit)';
ignore = false;
}

else
{
ignore = true;
}

})(source, map, log, target);

Can you please tell me the mistake I am making in this?

Regards,

Mahak

Hi Mahak,

 

In that case you need to use Field map script.

answer = (function transformEntry(source) {

if( tr =='Red Hat Enterprise Linux Server')
{
return 'Red Hat Ent Linux';
}
else if( tr =='Oracle Linux Server')
{
return 'Oracle Linux 4/5/6 (64-bit)';
}
else if(tr =='SUSE Linux Enterprise Server')
{
return 'SUSE Linux Enterprise 11 (64-bit)';
}

else
{
return '';
}

})(source);