+ Start a Discussion
noob123noob123 

Date Error

I am trying to update a Date field (Destination) with the value from another Date field (Source). I am calling the Source field in my page using an inputHidden tag. I'm setting the date fields like so:

 

destinationObj.Start_Date__c = sourceObj.Start_Date__c;

 

 

When I do this, I get the following error below:

 

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_TYPE_ON_FIELD_IN_RECORD, Start Date: value not of required type: Tue Feb 24 00:00:00 GMT 2009: [Start_Date__c]

Class.MyController.completeMove: line 77, column 9
Class.MyController.Move: line 98, column 9
External entry point

 

However, when I change it to to an inputField tag it works perfectly.  Any reason why it wouldn't work when I hide that field? I don't want the user to have the ability to change this field as it is supposed to be set automatically behind the scenes.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
SuriSuri

Oh. Ok. My Curreny Field shown here above is a Custom Field (of DataType: Currency) with:

 

Length: 10

Decimal Places: 5

 

and the {!ROUND(CurrencyField, 2)} works perfectly fine for me.

All Answers

Ron HessRon Hess

inputField knows the data type must be a Date, but inputHidden does not, and your date gets converted to a string

at some point, causing the DML error ( the string is no longer a valid date)

 

Can you use inputField by hiding it inside a DIV that is not visible to the user?

 

Or your controller can copy the date from the source to destination?

JakesterJakester

I don't want to hijack this thread, but I've done some searching and checked the Dev Guide and am stuck on a date question as well. I'd like to display a date from a date field (not a date/time field, if that matters) in a visualforce email. It's displaying right now like so:

 

Fri Oct 31 00:00:00 GMT 2008

 

Is there some way to format it to something like mm/dd/yyyy? This must be pretty easy to do, but I searched the dev guide for date functions and only found Now() and Today().

 

Thanks!

 

-Jake

Ron HessRon Hess

You can do this in Apex, however your email template has no controller, if i recall.  

 

So, you must resort to Javascript

 

here is a crash course :

 

 

<apex:page standardController="Account"> <br /> {!account.SLAExpirationDate__c} <br /> <apex:outputPanel id='formatDate' ></apex:outputPanel> <script> var dd = Date.parse('{!account.SLAExpirationDate__c}'); var date = new Date(dd); document.getElementById('{!$Component.formatDate}').innerHTML = (date.getMonth()+1) + '/' + date.getDate() + '/' +date.getFullYear() ; </script> </apex:page>

 

 

this works because the input to Date.parse() is the same as the output from a date field formated by visualforce.

 

 

 

JakesterJakester

Thank you so much for your response - you're always so helpful!!

 

I'm not sure if I'm doing something wrong, but I'm putting this code in my vf email template, and it's not working. Here's my code:

 

 

<messaging:emailTemplate recipientType="Contact" relatedToType="Asset" subject="Lost Product: {!relatedTo.Account.name} account's {!relatedTo.name} has been marked as inactive" replyTo="jake.harris@aisle7.net" > <messaging:htmlEmailBody > <html> <body> <STYLE type="text/css"> TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1; text-align: center } TD {font-size: 11px; font-face: verdana } TABLE {border: solid #CCCCCC; border-width: 1} TR {border: solid #CCCCCC; border-width: 1} </STYLE> <font face="arial" size="2"> <p><b>{!relatedTo.Account.name}</b> account's Product Owned named <b>{!relatedTo.name}</b> has been marked as <b>{!relatedTo.status}</b> with the reason of <b> {!relatedTo.Inactive_Reason__c}</b>. <br/>Exp Date: <br /> {!relatedTo.Expiration_Date__c} <br /> <apex:outputPanel id='formatDate' ></apex:outputPanel> <script> var dd = Date.parse('{!relatedTo.Expiration_Date__c}'); var date = new Date(dd); document.getElementById('{!$Component.formatDate}').innerHTML = (date.getMonth()+1) + '/' + date.getDate() + '/' +date.getFullYear() ; </script> <br/>Quantity: {!ROUND(relatedTo.Quantity,0)} <br/>Renewal Price: {!relatedTo.Renewal_Price__c} <br/>Total Renewal Price: {!relatedTo.Renewal_Price_Total__c} <br/>Account Owner: {!relatedTo.account.owner.name} </p> <p /> </font> </body> </html> </messaging:htmlEmailBody> <messaging:plainTextEmailBody > Please read the html version </messaging:plainTextEmailBody> </messaging:emailTemplate>

 

 

And here's my output:

 

 

 

 

Ron HessRon Hess

well, I now see how it could be that there is no javascript environment to run the script when the email template is composed on the servers.

 

I have another idea, which i hope will work better for you.

 

Create a formula field on your object, of type text. 

In that field you construct the date as a string using the formulas available for this. 

Then in your email template page, you reference the new "date as a string" field instead of the actual date field.

 

 

JakesterJakester

Thanks so much, Ron- that did the trick!

 

A couple of follow-up questions, if I may. The first is that I have a related problem. The Renewal Price in the screenshot above only returns one decimal place, which looks really funny for a currency, which is the type of field I've got there. I noticed that there's a Round() function, but it doesn't seem to like working with currencies. I'm guessing the only solution will be to create another formula to convert the currency to a number and then use Round(), right?

 

That brings me to my more general question, which sort of has two parts. The first is, how would I have known about Round() if it wasn't used in the example code I got from the AppExchange? I can't find any reference to that function elsewhere. And the second part is, are there plans to offer more functions in VF Emails? It seems quite limited right now.

 

Thank you!!

Ron HessRon Hess

sorry, yes Round() would not work with currencies. another formula i guess

 

Round should be documented in the Apex Code documentation.

JakesterJakester
Interesting! Does that mean that all of the functions here (warning - PDF) would work? If so, does that mean I could use format() for the date problem?
SuriSuri

(@ Jakester):

 

To display the date in your specified format in visualforce, the following will work:

 

{!month(relatedTo.Expiration_Date__c)} / {!day(relatedTo.Expiration_Date__c)} / {!year(relatedTo.Expiration_Date__c)}

 

 

SuriSuri

And to display the Rounded Currency field in visualforce:

 

{!ROUND(acc.CurrencyField__c, 2)}

 

worked for me. This displays the currency field rounded upto two decimal places.

 

In general, I guess all of the Operators given in:  https://na6.salesforce.com/help/doc/en/customize_functions.htm

 

can be used in visualforce.

JakesterJakester

Thanks so much, Suri!

 

The date format trick worked great and I'm able to delete that text-formatted date field now. Sweet!

 

The Round() trick didn't do anything for me - it's still returning a single decimal point :-(

SuriSuri

Oh. Ok. My Curreny Field shown here above is a Custom Field (of DataType: Currency) with:

 

Length: 10

Decimal Places: 5

 

and the {!ROUND(CurrencyField, 2)} works perfectly fine for me.

This was selected as the best answer