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
icejohn2508icejohn2508 

How would I write a trigger to delete a record upon insert when a field = 0

We upload/create records into our Trading Info custom object tradingInfo__c in Salesforce via the API.  We currently create Trading Info records for each product that a contact has access to trade even when no actual trade has occurred.  I would like to delete all Trading Info records created upon insert if the tradeCount__c =0.  I am new to APEX and would appreciate any assistance.

Thanks,
 
ManojjenaManojjena
Hi iceJohn,

Still your requirment is not clear enogh to provide solution .

You are creatin tradingInfo__c  record via API .tradeCount__c  field is in which object ?
Which record you want to delete and when need to delete ?

Please clarify .
MandarKhojeMandarKhoje
Assuming that tradeCount__c is a field on the tradingInfo__c custom object
 
trigger TradingInfoHandler on tradingInfo__c (after insert){
  List<tradingInfo__c> toBeDeleted = new  List<tradingInfo__c>();
  if(trigger.isAfter && trigger.isInsert){
     for(tradingInfo__c tradingInfo: trigger.old){
          if(tradingInfo.tradeCount__c = 0){
             toBeDeleted.add(tradingInfo);
          }
     }
    delete toBeDeleted;
  }
}

 
icejohn2508icejohn2508
Hello Mandar, Thanks for the reply. Yes. The field tradeCount__c is a field on the tradingInfo__c custom object. Basically the need to remove/delete the Trading Info records that are created and have no trade (i.e. tradeCount__c = 0) is to reduce our data storage numbers. John Barnes | Lead Salesforce Administrator Intercontinental Exchange | ICE 5660 New Northside Dr. NW | 3rd Floor | Atlanta, GA 30328 Tel: +1 770.857.2456 | Fax: +1 770.916.7877 john.barnes@theice.com www.intercontinentalexchange.com [cid:image003.png@01CF83F7.39BD45E0]
MandarKhojeMandarKhoje
then the above trigger I wrote should work, try and let us know! cheers
icejohn2508icejohn2508
Thanks…I will let you know. John Barnes | Lead Salesforce Administrator Intercontinental Exchange | ICE 5660 New Northside Dr. NW | 3rd Floor | Atlanta, GA 30328 Tel: +1 770.857.2456 | Fax: +1 770.916.7877 john.barnes@theice.com www.intercontinentalexchange.com [cid:image003.png@01CF83F7.39BD45E0]