4

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?

0

1 Answer 1

7

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')))
1
  • 1
    Thanks Sergey. I should of noticed that. Much appreciated. Will mark as answer asap.
    – user24634
    Jul 1, 2016 at 9:52

You must log in to answer this question.