Expand my Community achievements bar.

Join us for Adobe Campaign Community Q&A Coffee Break on Thursday, March 7th at 8am PT with Eric Perrin, Gael Mouello, Chakravarthy Kalva, Bruce Swann and Sandra Hausmann.
SOLVED

Remove Leading 0's from Alphanumeric String - Javascript

Avatar

Level 3

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
Community Advisor

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

5 Replies

Avatar

Correct answer by
Community Advisor

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 3

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 3

@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

Community Advisor

@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 3

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.