[SalesForce] arithmetic expression must use numeric argument

This error is driving me crasy

arithmetic expression must use numeric argument

obj.Without_credit_time_off_amount__c =( (User.Salary__c)/30)*obj.Days__c;

Without_credit_time_off_amount__c currency type
Days__c formula field, number type
Salary__c is a currency type

I've tried to cast the salary__c value but in vain

Best Answer

I think zdropic has identified the problem. With no other variables involved:

User.Salary__c

is an SObjectField token and can't be used in an arithmetic expression. (Its purpose is to provide a compile-time checked token that represents a field of an SObject; it can be used with SObject.get and SObject.put and in describe calls.) If you have a variable called User (or user given the Apex compiler's case insensitivity) in scope, that would normally have precedence, but it is clearer to not user variable names that are identical to object type names.

There is no global User object, there is only a global UserInfo object. So I would expect code looking like this to work here:

User u = [select Salary__c from User where Id = :UserInfo.getUserId()];
obj.Without_credit_time_off_amount__c = (u.Salary__c / 30) * obj.Days__c;
Related Topic