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
PerGeertPerGeert 

Trigger which delete current object returns warning

I have a trigger on a custom object OpenInvoice__c: trigger OpenInvoiceTrigger on OpenInvoice__c (after insert, after update) { if (Trigger.isAfter) { if (Trigger.isUpdate || Trigger.isInsert) { // After insert and update, check if invoice balance is zero. // If so, delete the record List PaidInvoices= new OpenInvoice__c[0]; for (OpenInvoice__c i : Trigger.new) { if(i.Balance__c == 0) { PaidInvoices.add(new OpenInvoice__c(id = i.id)); } } delete PaidInvoices; } } } The idea being, when an OpenInvoice is created/updated and the balance is zero, simply delete the record. (This happens normally from an interface from an ERP system). When I test this I get this warning/message: Record deleted The record you attempted to access has been deleted. The user who deleted this record can recover it from the Recycle Bin. Deleted data is stored in the Recycle Bin for 30 days. The message probably is caused by the fact that the trigger is executing in another user context than the user entering the data. The record is deleted OK. Any ideas on how to avoid this message and bring the user back e.g. to the object owning the OpenInvoice (the Account)?
Ron HessRon Hess
apex code cannot direct the user interface to any specific destination.

could you instead block the creation of the record?
PerGeertPerGeert
Yes, I can block the creation. However, most often it is the update that is of interest: Scenario: - Interface from ERP creates an OpenInvoice with a balance >0 - Interface at some later point changes the balance to 0 What we are trying to do is only to show sales reps open invoices. History is of no interest to them. Of cause we can handle this in the interface, but I just thought I would be nice to the interface people, so they didn't have to worry about the delete event. I still haven't released the code to the interface people, so maybe it will work for them, i.e. they do not have a UI issue, but are doing everything though the API.
Ron HessRon Hess
i guess if it's just visibility, i'd try something like, change the owner to a user in a different role, allowing sharing to hide the record from the reps.  Sharing is a very powerful feature, use it if you can given your specific case.

in the past i've also changed the record type on a record to lock or hide it from reports and dashboards.