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
Laurie CaronLaurie 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!

Best Answer chosen by Laurie Caron
Laurie CaronLaurie Caron

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!

 

trigger ChangeDateTrigger on Lead (before insert) {
//before insert trigger on Lead
for (Lead ct : Trigger.new) {
    if(ct.Datefake__c!=NULL && ct.Date__c ==NULL ) {
            String sDate = ct.Datefake__c; 
        Date dDate = Date.parse(sDate);
            ct.Date__c = dDate ;  
    }
  }


}

All Answers

Deepak GulianDeepak Gulian
In Apex
String sDate = '02/12/2016';
Date dDate = Date.parse(sDate);
Laurie CaronLaurie Caron

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 

 

Michał Zadrużyński 2Michał Zadrużyński 2
try this 
//before insert trigger on Lead
for (Lead ct : Trigger.new) {
	if(ct.LeadSource=='Web2Lead') {	//or whatever value you use on web form as hidden field
          	String sDate = ct.Date__c; 
		Date dDate = Date.parse(sDate);
          	ct.Date__c = dDate ;  
	}
  }

 
Laurie CaronLaurie Caron

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:

 

//before insert trigger on Lead
for (Lead ct : Trigger.new) {
	if(ct.LeadSource=='Web2Lead') {	//or whatever value you use on web form as hidden field
          	String sDate = ct.Datefake__c; 
		Date dDate = Date.parse(sDate);
          	ct.Date__c = dDate ;  
	}
  }
Laurie CaronLaurie Caron

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!

 

trigger ChangeDateTrigger on Lead (before insert) {
//before insert trigger on Lead
for (Lead ct : Trigger.new) {
    if(ct.Datefake__c!=NULL && ct.Date__c ==NULL ) {
            String sDate = ct.Datefake__c; 
        Date dDate = Date.parse(sDate);
            ct.Date__c = dDate ;  
    }
  }


}
This was selected as the best answer