+ Start a Discussion
smagee13smagee13 

Casting from double to integer error

Hoping somebody can help me here. Im trying to assign a value to a variable in an Apex Class from a field in a custom object.  I'll use this variable later on in the class to shift some due dates.

 

My problem is that the field is defined as a double, and I need to cast it to an integer in order to do my calculations.

 

I have the following code, that gives me this error when attempting to save it.

 

Illegal Assignment from Schema.SObjectField to Double

 

Code snippet

 

private final ApexPages.StandardController controller; public CustomController(ApexPages.StandardController controller){ this.controller = controller; myProject = (Project_KA__c)controller.getRecord(); Double y = [select ShiftByDays__c from Project_KA__c where Id = :myProject.Id]; Integer shiftby = y.intValue(); }

 

 Any ideas as to why the invValue is not casting this, or why Im getting the Illeagle Assignmet error?

 

Best Answer chosen by Admin (Salesforce Developers) 
smagee13smagee13

Richie D, Thanks that worked perfectly.

 

Just for my own knowledge, why did you need to add the .ShiftByDays__c to the end of the statementI hate just cutting and pasting code without understanding why its working.

 

 

Double y = [select ShiftByDays__c from Project_KA__c where Id = :myProject.Id].ShiftByDays__c;

All Answers

MukulMukul

Hi There,

 

Did you try this:

 

Integer shiftby = Integer.valueOf(y);

Richie DRichie D

Hi,

 

You may need to change your code to this:-

private final ApexPages.StandardController controller; public CustomController(ApexPages.StandardController controller){ this.controller = controller; myProject = (Project_KA__c)controller.getRecord(); Double y = [select ShiftByDays__c from Project_KA__c where Id = :myProject.Id].ShiftByDays__c; Integer shiftby = y.intValue(); }

R.

smagee13smagee13

Richie D, Thanks that worked perfectly.

 

Just for my own knowledge, why did you need to add the .ShiftByDays__c to the end of the statementI hate just cutting and pasting code without understanding why its working.

 

 

Double y = [select ShiftByDays__c from Project_KA__c where Id = :myProject.Id].ShiftByDays__c;
This was selected as the best answer
Richie DRichie D

Your original statement returned an object of type Project_KA__c which contained 1 field ShiftByDays__c. You just needed to get the field out and use that rather than the object itself.

 

Really I'm not sure why SFDC didn't complain that you were setting a double (y) to be an SObject(Project_KA__c)...