The CreatorCon Call for Content is officially open! Get started here.

Transform script to trim unwanted space on the target record

Tamil5
Tera Expert

Hi All,

Currently i'm populating cmn_location table using Scheduled job & transform map.

The incoming data some times having unwanted space at the end (eg, loc1 ,loc 2 ,loc3 like wise).

I just need to trim this blank space & insert the correct value into target table.Though it can be done from Business Rule, I have been asked to specifically do with Transform script.

I have tried the below in 'onBefore' Transform Script but its not working.

if(source.u_site_code != '' && source.u_site_code != null && source.u_site_code != 'n/a')

target.u_site_code = (source.u_site_code).toString().trim();

if(source.u_street != '' && source.u_street != null && source.u_street !='n/a')

target.street = (source.u_street).toString().trim();

if(source.u_city != '' && source.u_city != null   && source.u_city != 'n/a')

target.city = (source.u_city).toString().trim();

Any idea would be appreciated. Thanks in advance

Prici

1 ACCEPTED SOLUTION

Tamil5
Tera Expert

Thank you all for your responses.


Actually am able to achieve the same by writing script on field rather onBefore script.


Just clicked on the source field & checked 'Use source script'



find_real_file.png


Its working as expected.


View solution in original post

5 REPLIES 5

divya mishra
Tera Guru

Hi Prici,



Could you please use the below sample code to remoe unwanted spaces:


var str = "PB 10 CV 2662";


str = str.split(" ") //[ 'PB', '10', 'CV', '2662' ]


str = str.join("");


gs.print(str);



Let me know if this was helpful/correct
Have a great day ahead


vipulsethi
Tera Contributor

Hi Prici,



In your script you are using () on source object , please remove them and then try. Pfb the updated script



if(source.u_site_code != '' && source.u_site_code != null && source.u_site_code != 'n/a')


target.u_site_code = source.u_site_code.toString().trim();


if(source.u_street != '' && source.u_street != null && source.u_street !='n/a')


target.street = source.u_street.toString().trim();


if(source.u_city != '' && source.u_city != null   && source.u_city != 'n/a')


target.city = source.u_city.toString().trim();



Mark this correct, if it suites your requirement


Hi Guys,



Actually the trim operation is working fine regardless of with/without the braces.


But the trimmed value is not getting updated in the target table.


Even I tried giving target.insert or target.update. But with this, duplicate records inserted.



One record contains only this values(3 fields site code,street,city), another one contains value for other fields (state,Region & etc)



If I don't give these statement, i'm not getting duplicates & at the same time not getting trimmed values



Thanks


Prici


vipulsethi
Tera Contributor

Hi Prici,



Have you deleted the manual field mapping for those fields which you were using in transform script, If not then please update the filed mapping using mapping assist.