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
meeeeeeeeeeeeemeeeeeeeeeeeee 

Trigger to insert and Delete at the same time.Please help me.

Hi,

My requirement is like that...

I want to delete records from a custom object based on a criteria (if a checkbox is checked). This record is also holding some attachments. But before delete the Same record should be inserted into another Custom object  along with the attachments.(this object is also configured like this.)

 

I don't know why my code is not working. I 'm new to the world of trigger. Please help me.

 

 

trigger DeleteInvoicePopulatePSR on Invoice_holder__c (after Insert)
 {
  List <Invoice_holder__c> selectedInvoice= new List<Invoice_holder__c>([select Id,Amount_Open__c,name,Credits__c,Debits__c,Doc_Number__c,Due_Date__c,Order_Date__c,Paid_date__c,Payment_Referance__c,Purchase_Order__c,Sales_Order_No__c,Transaction_Type__c, (Select Id From Attachments)
   from Invoice_holder__c where ID IN:Trigger.newMap.keySet()and Seven_Years_Old__c=true]);
  List <PSR_clone__c> psrInsert=new List<PSR_clone__c>();
  List <Attachment> attlist=new List<Attachment>();
  List <Invoice_holder__c> ToBeDeleted=new List<Invoice_holder__c>();
  
   for(Invoice_holder__c i:selectedInvoice)
       {
        PSR_clone__c psrCloneInsert=new PSR_clone__c();
            psrCloneInsert.Amount_Open__c=i.Amount_Open__c;
            psrCloneInsert.Credits__c=i.Credits__c;
            psrCloneInsert.Debits__c=i.Debits__c;
            psrCloneInsert.Doc_Number__c=i.Doc_Number__c;
            psrCloneInsert.Due_Date__c=i.Due_Date__c;
            psrCloneInsert.Paid_date__c=i.Paid_date__c;
            psrCloneInsert.Transaction_Type__c=i.Transaction_Type__c;
           psrInsert.add(psrCloneInsert);
          for(Attachment att:i.Attachments )
           {
             att.parentId=psrCloneInsert.ID;

             attlist.add(att);
            }
         
         
   }
    Insert psrInsert;
    Insert attlist;
    Delete selectedInvoice;
   }

SivarajanSivarajan

Hi,

 

You should write the trigger for Before Delete Event. In that trigger, First insert those records into that custom object. no need to delete those records. It will be deleted automatically when the trigger get fired

meeeeeeeeeeeeemeeeeeeeeeeeee

Hi,

Thanks for your help. But my requirement is a bit different. It should be Before update. Because As per my requirement my records will marked (by a workflow) if it's 7 yrs old. then my trigger should fire if the checkbox is checked and fetch the record and insert into another object and the old record should be deleted.

 

Imran MohammedImran Mohammed

Are you getting any exception while doing this.

If its not throwing any error message, are the records inserted into the clone object.

 

One more question, how will your trigger get fired.

Does it get fired when Checkbox is checked.

Are you updating the checkbox related field in Invoice_Header__c object after its checked.

if you are updating then the trigger should be "after update" and not "after insert".

meeeeeeeeeeeeemeeeeeeeeeeeee

Yes my trigger will fire only when the Checkbox is checked. (Another workflow is taking care of this).

I have tried with "After Update" also but it's not working. Some time the trigger is firing but no record is inserting and sometime I'm getting error "The record is read only"...

Am I missing some required fields. As per logic written in the trigger it should fire. But I have another point which I would like to highlight..

I'm using Cast-Iron as a integration tool to fetch data.. Is it creating any probs?????