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
Ron08Ron08 

Delete records in Trigger

Can anyone help with the below code please, I am able to create the new lead and task however not able to delete the case 
trigger TriggerOnCase on Case (after insert ) {

    list<string> emailsInCase = new List<string>();
    list<lead> leadstoInsert = new List<lead>();
    list<task> TaskToInsert = new list<task>();
    list<case> casetodelete = new List<case>();
    for(case c : trigger.new){
        emailsInCase.add(c.SuppliedEmail);
    }
    
    list<lead> HasLead = [select id, email from lead where email in : emailsInCase];
    //list<contact> Hascontact = [select id, email from contact where email in : emailsInCase];
   
    for(case c1 : trigger.new){
        if((HasLead.size()==0 ) ){
            system.debug('lead'+HasLead.size());
            system.debug('emailsInCase'+emailsInCase);
            system.debug('leadstoInsert'+leadstoInsert);
            lead l = new lead();
            l.Company = 'ggg';
            l.email = c1.suppliedEmail;
            l.LastName = c1.SuppliedName;
            l.OwnerId = '005580000015thg';
            l.Status = 'Open - Not Contacted';
            leadstoInsert.add(l);
            casetodelete.add(c1);
            
        }
        system.debug('lead'+leadstoInsert);
        system.debug('lead'+TaskToInsert);
    }
    insert leadstoInsert;
    for(lead l :leadstoInsert){
        task t = new task();
            t.whoid= l.id;
            t.subject = 'vvv';
            t.status = 'Not started';
            t.Priority = 'Normal';
            t.OwnerId = l.OwnerId;
            TaskToInsert.add(t);
    }
    insert TaskToInsert;
    for(case cas : casetodelete ){ // unable to delete ??
        delete cas;
    }
}

Please help!
Any suggestions on optimisations also welcome!!
Thanks
Kai Herng LauKai Herng Lau
Hi Ron,

This is my suggestion. The problem you have is because you are try to update the Salesforce currently edit list, so the work around is to use others list for DML. Hope this help
 
trigger TriggerOnCase on Case (after insert ) {

    list<string> emailsInCase = new List<string>();
    list<lead> leadstoInsert = new List<lead>();
    list<task> TaskToInsert = new list<task>();
    list<case> casetodelete = new List<case>();
    for(case c : trigger.new){
        emailsInCase.add(c.SuppliedEmail);
    }
    
    list<lead> HasLead = [select id, email from lead where email in : emailsInCase];
    //list<contact> Hascontact = [select id, email from contact where email in : emailsInCase];
   
    for(case c1 : trigger.new){
        if((HasLead.size()==0 ) ){
            system.debug('lead'+HasLead.size());
            system.debug('emailsInCase'+emailsInCase);
            system.debug('leadstoInsert'+leadstoInsert);
            lead l = new lead();
            l.Company = 'ggg';
            l.email = c1.suppliedEmail;
            l.LastName = 'lName2';
            l.OwnerId = '0057F000001e603';
            l.Status = 'Open - Not Contacted';
            leadstoInsert.add(l);
            casetodelete.add(c1);
            
        }
        system.debug('lead'+leadstoInsert);
        system.debug('lead'+TaskToInsert);
    }
    insert leadstoInsert;
    for(lead l :leadstoInsert){
        task t = new task();
            t.whoid= l.id;
            t.subject = 'vvv';
            t.status = 'Not started';
            t.Priority = 'Normal';
            t.OwnerId = l.OwnerId;
            TaskToInsert.add(t);
    }
    insert TaskToInsert;
    
    List<Case> delCases = new List<Case>();
    for(case cas : [Select Id from Case where Id in: casetodelete]){ 
        delCases.add(cas);
    }
    
    delete delCases;
}

 
Ron08Ron08
Hi Kai, thanks for the reply!
I am still getting an error at line 32 : insert leadstoInsert;
The following errors were encountered while processing an incoming email:

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : TriggerOnCase: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Trigger.TriggerOnCase: line 32, column 1

This trigger fires when an case is created via email to case. Basically, it should only create a case if the email address is not found in existing leads, else create a lead and a Task.
Kai Herng LauKai Herng Lau
Hi Ron, 

That is because is your lead record in line 23, the OwnerId is hardcoded. If the record is removed we will hit into the same problem. My suggestion for this is the use current user. Can you please try to update your code in line 23 to:

l.OwnerId = UserInfo.getUserId();