+ Start a Discussion

Syntax to change one datatype to another

I need help or ideas on how to change the datatype of one field into some other datatype, just inside a code.


I have this issue where:

The field "CreatedDate" in Account object is of datatype DateTime

and The field "StartDate" in ServiceContract object is of datatype Date

I need to assign account.createddate to servicecontract.startdate

It gives me error: Invalid initial expression type for field ServiceContract.StartDate, expecting: Date

How do i convert a datetime field to a date field.


In general i need to know a way to convert one datatype into another.


Please check the documentation for Apex Code. Most types have all functions to convert between various related types. In your specific question, you can use:



// Account a, ServiceContract s
s.StartDate = a.CreatedDate.date();




Thank you so much.


I am actually a new developer in SFDC. This was very helpful. Thank you once again.


similar requirement.


"Length" field in account is of datatype Text

"Term" field in service contract is of datatype Number


I used the same technique which you replied to my last query but it gives me invalid String[Number] assignment.


I have to assign account.lenght=servicecontract.term





this is the error : Compile Error: Method does not exist or incorrect signature: [String].Number()


when i write : servicecontract.term=account.length.Number();


I think the following should achieve this:






it gives me this error:


Compile Error: Illegal assignment from Double to Integer


Ah - I guess you have zero decimal places on your number.


In that case, use an integer method:




Thank you Bob.


It is working perfectly fine.