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
Hari.gsHari.gs 

Error while deleting

 

Hi,

 

I have written a trigger

 

trigger duplicateLeadCheck on Lead (after insert, after update) {
    Lead mylead = trigger.new[0];
    String str = mylead.FirstName+'_'+mylead.Email;
    DuplicatLeadObject__c dupLeadObj = new DuplicatLeadObject__c();
    dupLeadObj.Name = str;
    //Long oneDay = 86400000;
    Long oneDay = 180000;
    Datetime currentTime = System.now();
    Long l2 = currentTime.getTime();
    list<Lead> leadList = [select Id, CreatedDate from Lead where Name_Email__c=:str];    
    if(leadList != null && leadList.size() > 0) {
        for(Lead lead : leadList) {
            Datetime createdDate = lead.CreatedDate;            
            Long l1 = createdDate.getTime();            
            if((l1+oneDay) > l2) {                
                dupLeadObj.Duplicate_Lead_Info__c = mylead.Working_Planet_Category_c1__c+'<br>'+mylead.Working_Planet_Source_source__c+'<br>'+mylead.Working_Planet_Keyword_kw__c+'<br>'+mylead.Working_Planet_Ad_cr5__c+'<br>'+mylead.Working_Planet_User_IP__c+'<br>'+mylead.Working_Planet_Timestamp__c+'<br>'+mylead.Company+'<br>'+mylead.Email+'<br>'+mylead.FirstName+'<br>'+mylead.LastName+'<br>'+mylead.Phone+'<br>'+mylead.Title;                
                insert dupLeadObj;
                delete mylead;
                //mylead.addError('Duplicate Lead');
            }
        }        
    }
}

 

while executing this i am getting an error like this

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger duplicateLeadCheck caused an unexpected exception, contact your administrator: duplicateLeadCheck: execution of AfterInsert caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.duplicateLeadCheck: line 18, column 1

 

Please help me.

 

Thanks and Regards

Hari G S

Starz26Starz26

You issue is you cannot delete records using the trigger.new context....

 

To do this you will:

 

trigger duplicateLeadCheck on Lead (after insert, after update) {
    Lead myLead = [ID = trigger.new[0].id];
    String str = mylead.FirstName+'_'+mylead.Email;
    DuplicatLeadObject__c dupLeadObj = new DuplicatLeadObject__c();
    dupLeadObj.Name = str;
    //Long oneDay = 86400000;
    Long oneDay = 180000;
    Datetime currentTime = System.now();
    Long l2 = currentTime.getTime();
    list<Lead> leadList = [select Id, CreatedDate from Lead where Name_Email__c=:str];    
    if(leadList != null && leadList.size() > 0) {
        for(Lead lead : leadList) {
            Datetime createdDate = lead.CreatedDate;            
            Long l1 = createdDate.getTime();            
            if((l1+oneDay) > l2) {                
                dupLeadObj.Duplicate_Lead_Info__c = mylead.Working_Planet_Category_c1__c+'<br>'+mylead.Working_Planet_Source_source__c+'<br>'+mylead.Working_Planet_Keyword_kw__c+'<br>'+mylead.Working_Planet_Ad_cr5__c+'<br>'+mylead.Working_Planet_User_IP__c+'<br>'+mylead.Working_Planet_Timestamp__c+'<br>'+mylead.Company+'<br>'+mylead.Email+'<br>'+mylead.FirstName+'<br>'+mylead.LastName+'<br>'+mylead.Phone+'<br>'+mylead.Title;                
                insert dupLeadObj;
                delete mylead;
                //mylead.addError('Duplicate Lead');
            }
        }        
    }
}

 

 

**Disclaimer, You have other issues with this trigger, mainly that it is not bulkified. I edited it to work with 1 record as you had it laid out***

 

Hari.gsHari.gs

Hi thanks for the reply.

 

But while trying this i am getting an error

 

Save error: SObject constructor must use name=value pairs. How to solve this

 

Thanks and Regards

Hari G S

 

 

Starz26Starz26

what line?

Hari.gsHari.gs

In this line

 

Lead myLead = New Lead([Select ID, FirstName, LastName From Lead Where ID = :trigger.new[0].id);
vishal@forcevishal@force

you don't need to query while initializing an object. 

 

trigger duplicateLeadCheck on Lead (after insert, after update) {
    Lead myLead = New Lead(ID = :trigger.new[0].id);
    String str = mylead.FirstName+'_'+mylead.Email;
    DuplicatLeadObject__c dupLeadObj = new DuplicatLeadObject__c();
    dupLeadObj.Name = str;
    //Long oneDay = 86400000;
    Long oneDay = 180000;
    Datetime currentTime = System.now();
    Long l2 = currentTime.getTime();
    list<Lead> leadList = [select Id, CreatedDate from Lead where Name_Email__c=:str];    
    if(leadList != null && leadList.size() > 0) {
        for(Lead lead : leadList) {
            Datetime createdDate = lead.CreatedDate;            
            Long l1 = createdDate.getTime();            
            if((l1+oneDay) > l2) {                
                dupLeadObj.Duplicate_Lead_Info__c = mylead.Working_Planet_Category_c1__c+'<br>'+mylead.Working_Planet_Source_source__c+'<br>'+mylead.Working_Planet_Keyword_kw__c+'<br>'+mylead.Working_Planet_Ad_cr5__c+'<br>'+mylead.Working_Planet_User_IP__c+'<br>'+mylead.Working_Planet_Timestamp__c+'<br>'+mylead.Company+'<br>'+mylead.Email+'<br>'+mylead.FirstName+'<br>'+mylead.LastName+'<br>'+mylead.Phone+'<br>'+mylead.Title;                
                insert dupLeadObj;
                delete mylead;
                //mylead.addError('Duplicate Lead');
            }
        }        
    }
}

 this should work. Btw it is not bulk enabled. 

Hari.gsHari.gs

Hi,

 

I have tried this but the lead not get deleted ! :(

 

Regards

Hari G S

 

 

vishal@forcevishal@force

Did you get any error for it? OR it just did not get deleted?

vishal@forcevishal@force

Did the duplicateLeadObj record get inserted?

 

If that record is also not getting inserted, then it has to be something where your if condition isn't getting covered, hence the code inside that if scope is not running. 

 

I am referring to this section of the code :

 

  if((l1+oneDay) > l2) {                
                dupLeadObj.Duplicate_Lead_Info__c = mylead.Working_Planet_Category_c1__c+'<br>'+mylead.Working_Planet_Source_source__c+'<br>'+mylead.Working_Planet_Keyword_kw__c+'<br>'+mylead.Working_Planet_Ad_cr5__c+'<br>'+mylead.Working_Planet_User_IP__c+'<br>'+mylead.Working_Planet_Timestamp__c+'<br>'+mylead.Company+'<br>'+mylead.Email+'<br>'+mylead.FirstName+'<br>'+mylead.LastName+'<br>'+mylead.Phone+'<br>'+mylead.Title;                
                insert dupLeadObj;
                delete mylead;
                //mylead.addError('Duplicate Lead');
            }
Hari.gsHari.gs

Hi

 

I am not getting any errors. But the Custom object is not get inserted and the lead object is not get deleted.

 

Why is this?

 

Thanks

Hari G S

craigmhcraigmh

Why aren't you using the addError() method before insert/delete? I feel like I'm missing something...