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
SeanSeeSeanSee 

Change CreatedDate from 1/1/1900

I have written a custom Apex Class that I am using to create a Completed Task for our inside sales people to log a call without having to enter any notes.  It works perfectly except for one small problem.  The tasks that are created are all being assigned a CreatedDate or 1/1/1900.

 

I tried adding objTask.CreatedDate = System.Now(); but I am getting an error that says the field is not writeable.  Is there another way to pass the datetime information to the CreatedDate field?

 

The full code is:

 

public with sharing class LogCall {

    public String AccountID;
    
    public Id TaskRecordTypeId;
    
    public LogCall() {
    
        if(ApexPages.currentPage().getParameters().get('id') != null){        
           AccountID = ApexPages.currentPage().getParameters().get('id');           
           for(RecordType rt : [SELECT ID FROM RecordType WHERE sObjectType = 'Task' AND Name = 'Log a call' Limit 1]){
              TaskRecordTypeId = rt.Id;
           }
           
        }
    }
    
    public PageReference CreateNewTask(){      
        Pagereference  pr = null;        
        try{
            
            Task objTask = new Task();
            
            objTask.CreatedDate = System.Now();
            objTask.OwnerId = Userinfo.getUserId();
            objTask.Status = 'Completed';
            objTask.Subject = 'Called and left message with person or voicemail';
            objTask.WhatId = (Id)AccountID;
            objTask.RecordTypeId = TaskRecordTypeId;
            
            insert objTask;         
            pr = new Pagereference('/' + AccountID);            
        }
        catch(Exception ex){        
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));   
        }        
        return pr;
    }
    
}

 

Thanks so much,
Sean

hisrinuhisrinu

Createddate is a system field which is used for audit purpose. I believe you can't change or set the value for this field until and unless salesforce has given you the access to edit the system fields for your instance.

 

In short, you will not be able to edit the createddate and it should always gives you the current date and time. If this is not the case then you can raise a case with salesforce