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
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
|improve this answer|||||
  • Thank you again, it fits. – Mahmoud Gueye 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 – Mahmoud Gueye May 20 '19 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.

|improve this answer|||||
  • 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 – Mahmoud Gueye 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.