function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
benwrigleybenwrigley 

Getting the value from a field Object

Hi There,

 

I am writing a trigger to test if certain Opportunity field values have been changed by the user. Some fields are allowed and some are not.

 

I have a List of field names that I iterate through with the code below

 

 

for (String f : notAllowed){
			
    Schema.Sobjectfield field = newOpp.getSObjectType().getDescribe().fields.getMap().get(f);
			
    if (field.getDescribe().getType().Name() == 'Decimal'){
	Decimal newVal = newOpp.get(f);
	Decimal oldVal = orig.get(f);
	if (Math.round(newVal) != Math.round(oldVal)){
		return false;
	} 
    }
    else if (newOpp.get(f) != orig.get(f)){
	return false;
    }
}

return true;

 

 

I had to add the decimal check as I found that Decimal values that were not being edited can be different by 10 decimal places. 

 

The decimal check is where it fails. According to the docs, newOpp.get(f) returns an Object, what kind of Object is unclear to me and how I get the decimal value out of it is just as unclear.

 

Can anyone help?

 

TIA

 

 

Best Answer chosen by Admin (Salesforce Developers) 
benwrigleybenwrigley

Ok, I have found a rubbish solution, so if anyone finds a better one please let me know.

 

So the object returned by sObject.get() method can be cast to a String which can then be cast to a decimal

 

Decimal newVal = Decimal.valueOf(String.valueOf(newOpp.get(f)));

 

Nice! yuk!