You need to sign in to do that
Don't have an account?
Laurie Caron
Check if value is date type, if not then convert it to date type
Hello good people.
I don't have skills in apex or anything. I am here unfortunatley because when we are capturing leads from a landing page which sends us date in the format: "02/12/2016". This doesn't go into our date field which is of date type.
We also edit/update dates ourselves.
I ended up on apex and now want a solution that when a lead is created, the date field should be checked, if it is of date type (manual entries) then ignore but if it is text "02/12/2016" then convert it to date type and assign to our date field.
What will the code for that?
Thank you alot!
Actually I solved it.
I made a text field just for the web 2 lead form where I don't show the actual Date__c field. So I check if it's the insert trigger and if the text field has a date and actual date field is empty then I just assign the text field to actual date field after conversion. Thanks everyone!
All Answers
String sDate = '02/12/2016';
Date dDate = Date.parse(sDate);
Thank you.
This will work if the date comes in text form (from the lead capture) but when we enter/edit dates manually using the salesforce calendar, this will not work I guess. Our Date__c is of date type. This is how it looks at the moment:
trigger ChangeDateTrigger on Lead (before insert) {
for (Lead ct : Trigger.new) {
String sDate = ct.Date__c;
Date dDate = Date.parse(sDate);
ct.Date__c = dDate ;
}
}
This doesn't work and gives an error that ct.Date__c is of date type. Is there a way we check if ct.Date__c is of date type then skip it, but if it comes in text form then we parse it?
As of right now when I save the code above, it says: Illegal assignment from Date to String
Thanks Michal.
But there is an error. Error: Compile Error: Illegal assignment from Date to String on line 4.
Maybe created a different field like Datefake__c of string type just to be used on the web2 lead form and then assign it to Date__c like this:
Actually I solved it.
I made a text field just for the web 2 lead form where I don't show the actual Date__c field. So I check if it's the insert trigger and if the text field has a date and actual date field is empty then I just assign the text field to actual date field after conversion. Thanks everyone!