Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Remove Leading 0's from Alphanumeric String - Javascript

Avatar

Level 2

Hello, 

 

I wrote the following JS so I can remove leading zeroes from an Alphanumeric field. However when I run the code - the field is not dropping the leading zeroes. 

 

Instead I get the same value. I feel like I'm missing something. The field I'm loading is a string value, but it looks like the replace is being ignored. 

 

montezh2001_0-1645126179179.png

montezh2001_1-1645126263746.png

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hello @montezh2001 

 

the type of your ''res'' variable is xml and the replace function works only on string variables. You have to convert your xml variable to a string one like this :

var res = results[0].@image1.toXMLString();

 

Amine_Abedour_0-1645130209750.png

result : 

Amine_Abedour_1-1645130285678.png

 

Br,

 

Amine

 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 3

Hello @montezh2001 

 

the type of your ''res'' variable is xml and the replace function works only on string variables. You have to convert your xml variable to a string one like this :

var res = results[0].@image1.toXMLString();

 

Amine_Abedour_0-1645130209750.png

result : 

Amine_Abedour_1-1645130285678.png

 

Br,

 

Amine

 

Avatar

Level 2

This worked. Thank you. I knew I was missing something small. 

Thank you so much! 

 

I also ran it this way based on a recommendation, and this worked as well. 

 

var res = new String(results[0].@image1) 

 

 

Avatar

Level 2

@Amine_Abedour Ok..last question. When I create the instance.var and write it out in an Enrichment, I'm only seeing it output the last record only and appending it to every record. How do I get it to write out all the records? 

Avatar

Level 3

@montezh2001,

 

Something like this would work for you :

var query = xtk.queryDef.create(
              <queryDef schema={vars.targetSchema} operation="select">
                <select>
                  <node expr="@id"/> //or any identifier of the workflow's temporary table
                  <node expr="@image1"/>
                </select>
                </queryDef>);
var results = query.ExecuteQuery();


for each( var tmp in results){
  var res = tmp.@image1.toString();
  res = res.replace(/^0+/,'');
  xtk.session.Write(<{vars.targetSchema.split(':')[1]} _operation="update" _key="@id" xtkschema={vars.targetSchema} id={tmp.@id} image1={res}/>); // update each row of your temp schema with the right res
}

No need for Enrichment after that.

 

Br,

 

Amine

Avatar

Level 2

This worked PERFECTLY!!! Thank you again. I'm new to coding in JS and knew I was missing something. Thank you again for your help.