3

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

10
  • Can you clearly explain your variable name and type again? Mar 28, 2014 at 10:02
  • Without_credit_time_off_amount__c >>> currency type | Days__c >>> formula field, number type | Salary__c >>> currency type
    – LoveLace
    Mar 28, 2014 at 10:03
  • Can you post your casting expression? Mar 28, 2014 at 10:04
  • here you are : obj.Without_credit_time_off_amount__c = ((Decimal)(User.Salary__c)/30)*obj.Days__c; But currency type is already known as a Decimal type on Apex
    – LoveLace
    Mar 28, 2014 at 10:07
  • I think the problem is in the arithmetic operator / , x / Y. Divides x, an Integer or Double, by y, another Integer or Double. Note that if a double is used, the result is a Double.
    – LoveLace
    Mar 28, 2014 at 10:09

2 Answers 2

3

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;
1
  • Awsomly working :) thank you Keith ! So you are telling me to manipulate my user fields I should use UserInfo, didnt know that! thank you again :)
    – LoveLace
    Mar 28, 2014 at 11:06
0

I figured out something, is that I cant manipulate a custom field on user without inisialised it !!!! is there any other way to manipulate it without inisialised it?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .