Convert between data types by creating a variable of type string
You may have noticed that it is not possible to convert between certain data types in C4C. This is especially bothersome since most data types are declared in multiple namespaces.
Take the example CurrencyCode. Suppose you have two fields of type Amount. One stores the EUR value, the other stores the USD value. If amount1 (EUR) changes you want to update amount2 with the corresponding value in USD.
You could go ahead with a coding similar to this:
import AP.Common.GDT as APC; // I'm doing the declarations here so you know they're the same // namespace. In reality, the fields would not need to be declared, // so imagine they're not initial, but filled var amount1 : APC:Amount; // contains amount in EUR var amount2 : APC:Amount; // contains amount in USD // amount1 has changed => update amount2 by converting // amount1 into the currency of amount2 amount2 = amount1.ConvertCurrency(amount2.currencyCode);
However, this will fail during activate. Why? Because Amount.currencyCode uses the CurrencyCode data type from the BASIS.Global namespace, but Amount.ConvertCurrency expects the CurrencyCode from the AP.Common.GDT namespace.
Okay, you might think, according to the repository explorer their technical details are the same. So just do a:
// use CurrencyCode from AP.Common.GDT var code_apc : APC:CurrencyCode; code_apc = amount1.currencyCode; amount2 = amount1.ConvertCurrency(code_apc);
This will satisfy our ConvertCurrency routine, you will be able to activate, but you’ll get a dump during runtime because of a green little wriggle underneath amount1.currencyCode saying that assignment between CurrencyCode(BASIS.Global) and CurrencyCode (AP.Common.GDT) is not possible.
Assign the BASIS.Global CurrencyCode to a string. And then assign the string to the AP.Common.GDT CurrencyCode.
Note that you cannot explicitly declare a variable of type String.
var code_string : String;
will fail. String only exists in the AP.PDI.ABSL namespace. But this cannot be imported.
You also cannot use another data type that implements string (like Text).
The solution is rather simple. To create a String data type, just do:
var code_string = "";
And in our case above, the solution would be:
// use CurrencyCode from AP.Common.GDT var code_apc : APC:CurrencyCode; var code_string = ""; code_string = amount1.currencyCode; code_apc = code_string; amount2 = amount1.ConvertCurrency(code_apc);
Not all data types can be converted to string just like that. But all that I’ve come across either contained a .content node, which contains the string representation of the content. Or there is a ToSTring() method, which converts the content to string