+ Start a Discussion
Ryan-HaireRyan-Haire 

Convert CreatedDate to Date

This is probably a very basic question. I'm new to programming and have spend 2 hours on this.
I'm trying to do a cross object field update trigger stamping one object with the CreatedDate of the other object.
The update field on the object I'm trying to update is of type Date.
trigger UpdateQSOCreatedDate on FRLS_Round__c (after insert) {
  	List<ID> QSOTargetIds = New List<ID>();

  for(FRLS_Round__c r : Trigger.new){
    if(r.QSO_Target__c!=null){
      QSOTargetIds.add(r.QSO_Target__c);
    }
  }

  List<QSO_Target__c> QSOTargetList = [SELECT id, Date_Last_Round_Created__c FROM QSO_Target__c WHERE id in :QSOTargetIds];
  for(integer i = 0 ; i < QSOTargetList.size(); i++){
  QSOTargetList[i].Date_Last_Round_Created__c = FRLS_Round__c.CreatedDate;
  }
  update QSOTargetList;
}


How do you convert CreatedDate to a Date field?
Jason Curtis NBSFDGJason Curtis NBSFDG

Hi, Ryan, here is a list of the Date/time methods for Apex: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

date()
Returns the Date component of a Datetime in the local time zone of the context user.
Signature

public Date date()
Return Value

Type: Date

Best,
 

Jason

Ryan-HaireRyan-Haire
I tried FRLS_Round__c.CreatedDate.date() and the system returned Method does not exist or incorrect signature. I also tried Datetime.date(FRLS_Round__c.CreatedDate) and the system returned variable does not exist. Am I using this incorrectly? Thanks- Ryan
SgtKillerSgtKiller
Try using DATEVALUE(Field) or 

Date myDate = date.newinstance(CreatedDate.year(), CreatedDate.month(), CreatedDate.day());