+ Start a Discussion

Function for primitive type conversion?

I need to do primitive type conversions dynamically. I will not know the type the field is nor will i know the type it needs to be converted to. Is there a built in function to convert any primitive type to another primitive type? ex:


Double d = SFDC.convert('10.30', 'Double');

String s   = SFDC.convert(10.30, 'String');


Alternatively i could create a function (which just walks through every permutation, which i would rather not) if i could return a AnyType field type form a function. Put although the 'AnyType' field is in the APEX documentation force.com says that type doesn't exist. 


Any ideas on either of those? Thanks! 


have you checked the System static methods for these primitives, e.g. valueof?

I think that is what you are looking for.


Check the Apex docu for Primitive methods under "Reference"




Message Edited by ahab1372 on 08-27-2009 10:09 AM

Thanks for the comment but you are referring to using the Static Methods Double.valueOf, Integer.valueOf and this is dynamic programming.


Please remember WE DO NOT know what the primitive type is nor do we know what type it must be converted to at development time. We will only know this at run time. Therefore we need to specify what is being converted and what to convert to dynamically. Example (assume this function exists and is called System.convert(<AnyType>, <String>) where param 2 indicates what you ant to convert to):


// The follwoing is only known at runtime, but hard hard code for this ex. 

String fieldType = 'Double' 

String field1 = 'AnnualRevenue';

String value1 = '100.2' 


Contact c = new Contact();

// Since we d not know what the field is, we do not know the type, and

// we do not know the type of the incoming value is either. Therefore we must have 

// something dynamic which can handle each permutation 

c.put(field1, System.convert(value1, fieldType));



This is why i'm asking for either

1. Is there a built in function that does this?

2. If not is there a way to specify a AnyType (any primitive type) data type from a function (in apex documentation but you can't seem to use it)? That was we could build every permutation in a gigantic "if then else" statement (String to Double, String to Integer, Double to String, Double to Integer, etc) and return it. This way it can be reused in code.  






You can use object to pass around unknown primitive types.


The following (trivial) code creates an Account with Annual Revenue of 10,000.0





Account acc = new Account(Name='Something');

object objAnnualRevenue; double dAnnualRevenue = 10000;


// Put the double directly into the object...

objAnnualRevenue= dAnnualRevenue;


// pass the object into the put() method

acc.put('AnnualRevenue', objAnnualRevenue); insert acc;



HTH, Ian :)


got it. Sounds interesting.

Have you considered converting everything into a string first (valueOf for string accepts virtually everything without specifying a data type) and then convert from this string to whatever is needed? Most primitives have a valueOf method that accepts strings.

Haven't tried this, just a thought.