2

Need to check a type of variable in ampscript, in order to use FormatNumber function(that can only work with integer variables). I tried the Handlebars function {{isString()}}, but can't make it worked with AmpScript or GTL, in code snippet block

SET @X  =  TreatAsContent('{{=isString(TrackingNumber )}}')     
     If @X == False THEN
         set @TrackingNumber = TreatAsContent('{{=FormatNumber(TrackingNumber, "d6")}}') 
     Else 
         set @TrackingNumber = TreatAsContent('{{TrackingNumber}}')   EndIF

3 Answers 3

2

I think you can also accomplish your use case with format() as opposed to formatnumber()

I believe this fits your use case

%%[ 
 set @tn1 = '012345'
 set @tn2 = '01234a'
 set @tn3 = '123456'
]%%

%%=Format(@tn1, "0#####")=%%<br>
%%=Format(@tn2, "0#####")=%%<br>
%%=Format(@tn3, "0#####")=%%<br>

Provides the following values

012345
01234a
123456
2
  • Thank you again, it fits. Commented May 10, 2019 at 9:07
  • 1
    I just see that this function works with a pre-specified number of value ("0#####" here works for 6 digits). Are there a format pattern that can integrates formatting for n digits. Can't find the doc with these formats. ampscript.guide/formatnumber gives a limit format patterns Commented May 20, 2019 at 13:08
0

You aren't going to have much luck checking the type with ampscript (or converting string to int without a hacky workaround if at all). Can you just pass that ampscript variable into SSJS to determine type and format the value there (or pass back to ampscript to format there)?

You can get and set the value with:

Platform.Variable.GetValue("@tracking_number");
Platform.Variable.SetValue("@formatted_tracking_number", var_you_format_in_ssjs);

That being said, I would highly advise that you just format your JSON string at the beginning of the email to convert the data type for all objects that contain a string value for this key. It would be much more efficient than checking the type of every tracking number when you call it in the email, and would be much easier to maintain.

2
  • Yes, SSJS can do it easily, but im parsing my json with GTL, then using ampscript is the the only way to retrieve values from the gtl. {{=format(TrackingNumber, '0#####')}} is the best solution Commented May 10, 2019 at 9:11
  • @mahmoudGueye I tried but somehow I always ends up with two zeros. So in my data I have something like this: 012345 and using {{=format(TrackingNumber, '0#####')}} gives me 00123456. Do I miss something?
    – Martijn e
    Commented Feb 27 at 12:53
0

One way is to use RegExMatch:

if trim(RegExMatch(trim(concat(" ", @variable)), '([0-9]+)', 1)) != "" then 
    /* your code */
endif

The regular expression "([0-9]+)" matches all numeric characters, requiring at least one. Using the brackets and the third parameter "1" makes sure the number is returned. The trim(concat(" ", @variable)) nesting makes sure you get a string to perform the RegEx on, even if your variable is null (unset). Trimming what comes back from RegExMatch is to make sure that no trailing spaces added by AMPscript methods gonna cause us headache (which I had experienced in the past).

If there is no match on your variable, the test will fail because you will get an empty string. If there is at least one numeric digit, the test will succeed, because it retrns a non empty string.

To convert the string to a numeric variable, you can use

Add(0,RegExMatch(trim(concat("0", @variable)), '([0-9]+)', 1))

By performing a mathematical operation on string variable that contains only numeric digits (add it to 0), the returned variable must be numeric. But be careful, this works on plain integers. If the number in @variable can be decimal/floating (like 1.5) point or otherwise formatted (like 1'000), you need to make sure to replace those characters or add them to the RegEx. Use Format/NumberFormat for that purpose.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .