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")}}') 
         set @TrackingNumber = TreatAsContent('{{TrackingNumber}}')   EndIF

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

  • Thank you again, it fits. May 10 '19 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 May 20 '19 at 13:08

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.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.

  • 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 May 10 '19 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.