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
Silpi roy 16Silpi roy 16 

Trigger for deletion of record

Hi

My scenario is :
Billing Document:Parent
Billing Item:Child
I need a detion trigger to delete Billing Item with Unit Price=0/Blank and Subsegment=Undefined.

Note:Billing document can also be created from SAP.So if any Billing Item with above mentioed criteria is there it should also get deleted
Thanks!!
Silpi roy 16Silpi roy 16
Also I want to add if the record is Undeleted from recycle bin again it should get deleted.
sfdc freshersfdc fresher
Hi Silpi roy 16,

i think we can just write the trigger operation on the child record, and if the condition met we can perform the dELETE Operation on that particulr record. So only child get deleted.
it wont effect any respective parent record.

I am not clear about your query, could you please elobarate it?


 
Silpi roy 16Silpi roy 16
My mistake...Billing item can be created from SAP and One Billing document may have multiple Billing Item
Ajvad AjuAjvad Aju
Hi Silpi,

Here is the code for the trigger for deletion of a record, I hope this will help you
 
trigger deleteBillingItem on Billing_Item__c (after insert, after update)
{
    List<Billing_Item__c> abc = new List<Billing_Item__c>;
    for(Billing_Item__c bl: Trigger.new){
       if (bl.unit_price__c == 0 && bl.subsegment == 'undefined')
       {	
        abc.add(bl);
       }
    }
        delete abc;
}

Regards
Ajvad Aju​
Silpi roy 16Silpi roy 16
Hi Ajvad Aju,

Getting Error:Apex trigger deleteBillingItem caused an unexpected exception, contact your administrator: deleteBillingItem: execution of AfterUpdate caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Trigger.deleteBillingItem: line 10, column 1
Sibasish PanigrahiSibasish Panigrahi
Hi Silpi,

I changed the code of Ajvad Aju slightly and tried it out, it's working for me.
Below is the changed code 
trigger deleteBillingItem on Billing_Item__c (after insert, after update)
{
    List<Id> abc = new List<Id>;
    for(Billing_Item__c bl: Trigger.new){
       if (bl.unit_price__c == 0 && bl.subsegment == 'undefined')
       {	
        abc.add(bl.Id);
       }
    }
        Database.delete(abc);
}

Hope this solves your issue.

Thanks,
Sibasish