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
TinkuTinku 

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.

sfdcfoxsfdcfox

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();

 

 

TinkuTinku

Thank you so much.

 

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

TinkuTinku

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

 

 

 

TinkuTinku

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

 

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

bob_buzzardbob_buzzard

I think the following should achieve this:

 

servicecontract.term=Double.valueOf(account.length);

TinkuTinku

@bob_buzzard

 

it gives me this error:

 

Compile Error: Illegal assignment from Double to Integer

bob_buzzardbob_buzzard

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

 

In that case, use an integer method:

 

servicecontract.term=Integer.valueOf(account.length);

TinkuTinku

Thank you Bob.

 

It is working perfectly fine.