+ Start a Discussion
BennettTranBennettTran 

Trigger to delete custom object record

Hello,

 

I am new to Apex triggers and need help on creating one.  My organization has a custom object called product subscriptions.  I would like to run a trigger to checks if a field called quantity on my custom object is equal to 0.  If the quantity is equal to 0 I would like the trigger to delete that record.  Below is the trigger I have started(it may be completely wrong):

 

trigger DeleteZeroQtySubscriptions on Product_Subscription__c (after update, after insert) {
//This trigger deletes any product subscription that has a quantity of 0
//Created by Bennett 1082013

Product_Subscription__c[] toBeDeleted = new list<Product_Subscriptions__c>();

for (Product_Subscription__c sub : Trigger.Old)
{
if (sub.Quantity__c == '0')
{
toBeDeleted.add(sub);
}
}
delete toBeDeleted;
}

 

Also, how can I test this trigger?

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
testrest97testrest97

see if this works...or i guess you need to delete using future class

 

trigger DeleteZeroQtySubscriptions on Product_Subscription__c (after update, after insert) {
//This trigger deletes any product subscription that has a quantity of 0
//Created by Bennett 1082013

Product_Subscription__c[] toBeDeleted = new list<Product_Subscriptions__c>();

Set<id> idsset=new Set<id>();

for (Product_Subscription__c sub : Trigger.New)
{
if (sub.Quantity__c == '0')
{
//toBeDeleted.add(sub);

idsset.add(sub.id);
}
}

//if(toBeDeleted.size()>0)
//delete toBeDeleted;

if(idsset.size()>0){

List<product_subscription__c> sublists=[select id from product_subscription__c where id in:idsset];

delete sublists

}
}

All Answers

testrest97testrest97

looks good, just change trigger.old to trigger.new......test class should be very simple just create one product subscriptuon record and update Quantity to 0

 

trigger DeleteZeroQtySubscriptions on Product_Subscription__c (after update, after insert) {
//This trigger deletes any product subscription that has a quantity of 0
//Created by Bennett 1082013

Product_Subscription__c[] toBeDeleted = new list<Product_Subscriptions__c>();

for (Product_Subscription__c sub : Trigger.new)
{
if (sub.Quantity__c == '0')
{
toBeDeleted.add(sub);
}
}
delete toBeDeleted;
}

VPrakashVPrakash

Your trigger code is mostly right, I did some tweaks/best practices.

 

trigger DeleteZeroQtySubscriptions on Product_Subscription__c (after update, after insert) {
//This trigger deletes any product subscription that has a quantity of 0
//Created by Bennett 1082013

Product_Subscription__c[] toBeDeleted = new list<Product_Subscriptions__c>();

for (Product_Subscription__c sub : Trigger.New)
{
if (sub.Quantity__c == '0')
{
toBeDeleted.add(sub);
}
}

if(toBeDeleted.size()>0)
delete toBeDeleted;
}

 

You can write a simple test class with single method which inserts the product subsciption and then update the record which will invoke the above trigger to delete you record.

 

use following link as your reference --> http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

 

BennettTranBennettTran

I am getting the following error when trying to save:

 

Error: Compile Error: Invalid type: Product_Subscriptions__c at line 5 column 54

testrest97testrest97

Product_Subscription__c

 

Product_Subscription__c[] toBeDeleted = new list<Product_Subscription__c>();

BennettTranBennettTran

When I try to test the trigger by taking the quantity of a subscription to zero I get the following error:

 

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

 

 

BennettTranBennettTran

Whoops I didn't see that extra s.  Thanks for catching that!

testrest97testrest97

see if this works...or i guess you need to delete using future class

 

trigger DeleteZeroQtySubscriptions on Product_Subscription__c (after update, after insert) {
//This trigger deletes any product subscription that has a quantity of 0
//Created by Bennett 1082013

Product_Subscription__c[] toBeDeleted = new list<Product_Subscriptions__c>();

Set<id> idsset=new Set<id>();

for (Product_Subscription__c sub : Trigger.New)
{
if (sub.Quantity__c == '0')
{
//toBeDeleted.add(sub);

idsset.add(sub.id);
}
}

//if(toBeDeleted.size()>0)
//delete toBeDeleted;

if(idsset.size()>0){

List<product_subscription__c> sublists=[select id from product_subscription__c where id in:idsset];

delete sublists

}
}

This was selected as the best answer
BennettTranBennettTran

That worked!  Could you possibly explain to me why it worked?

 

Thanks!

testrest97testrest97

DML's are not allowed withing the scope of request in update/insert trigger....Technically what we were trying to do is delete a record before the update was completed......now what we did after was delete the record after the update was completed..

 

 

Also mark it as solved if the solution worked as it might help others