[SalesForce] Variable does not exist: Decimal

I have the following code:

fieldList = (List<Object>)JSON.deserializeUntyped('['+ JSONValueRights +']');

for(Object fld : fieldList){    
    Map<String,Object> data = (Map<String,Object>)fld;
    RightsTrackerListValue.Add(new Rights_Tracker__c(Category__c = (String)data.get('category'), Product__c = (String)data.get('product'), Details__c = (String)data.get('details'), Agreement__c = Decimal.valueOf(data.get('agreement'))));
}

I'm trying to get the agreement to be converted to a decimal.

Looking at the following page I can see that decimal has a method called .valueOf where I can pass a string and get it converted to decimal.

However I get the error 'decimal does not exist'. What am I doing wrong?

Best Answer

You should convert the Object to String first. And then String to Decimal:

Object o = '2.52';
String s = String.valueOf(o);
Decimal d = Decimal.valueOf(s);

In the Decimal docs: the valueOf(...) method can take as parameter:

  • Double
  • String
  • Long

but not the Object!

So in your code use:

Decimal.valueOf(String.valueOf(data.get('agreement')))
Related Topic