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
snehal waghmare 3snehal waghmare 3 

hi exports i need help to write a trigger facing issue

on Lead creation and updation if lead status is qualified or unqualified want current date . i have written below trigger but its not working

trigger LeadTrigger on Lead (before insert,Before update) {
   if(trigger.isBefore && trigger.isUpdate){
LeadTriggerHandler.befUpdt(trigger.new , trigger.oldMap);
   }

}
------------------------------------------------------------------------------------
public class LeadTriggerHandler {
    
    public static void befUpdt(List<Lead> newLd, map<Id,Lead> oldMapLd){
        
        for(Lead ld : newLd){
           if (oldMapLd != Null && ld.status != oldMapLd.get(ld.Id).status ){
                if(ld.status == 'qualified' && ld.status ==' unqualified'){
                    ld.CreatedDate = system.now() ;
                }
            }
        }
        
    }

}

thank you,
snehal
SOURAV GUHA 9SOURAV GUHA 9
Created Date and Last Modified Date fields in Salesforce objects are audit fields and we can't edit or change these field directly or through code.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Snehal,

Yes CreatedDate is a audit field and you cannot update it directly or with code but you can raise a case wuth salesforce support for getting the permission to edit these fields.

Please find the below article (https://blog.webnersolutions.com/salesforce-how-to-edit-salesforce-created-date-and-last-modified-date-field-values/) for the same.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 
Abdul KhatriAbdul Khatri
Hi Snehal,

I guess your requirment is to tag the DateTime informtion on the lead when it was moved to status qualified or unqualified. I am not why you want ot manipulate the CreatedDate field which is already mentioned to you is controlled by Salesforce and you cannot make any updates there.

Please confirm.