0

I need to write a trigger on Case object where I need to delete the placeholder records and relate the case to existing records. I need to prepare the list of placeholder in before context and delete the same list in after context. What would be best approach for this? Can I use static list and delete it in after context? Please advise.

Ex. I have a related lookup filled out with a placeholder record. when the case is closed as it turns out the case should be related to an existing record, so I need to delete the placeholder record. As a best practice, I cannot delete the related placeholder in before context but I need to update the case in before. Let me know if this clarifies the question.

Example - When Case 1 is inserted, Case 1 is related to RecordA(Placeholder)

When Closed after review Case 1 need to be related to RecordB(Existing record)

Before Context Update Case 1 => RecordB Make a tobeDeleted list with recordA -- I made this list static

After Context If tobeDeleted != Empty, delete tobeDeleted

Is this the correct approach? Or is there any better way?

4
  • It's not very clear as what you are trying to achieve. Can you clarify further? Why would you need to create a list and then delete it in different contexts?
    – Jayant Das
    Commented Oct 16, 2018 at 16:57
  • Ex. I have a related lookup filled out with a placeholder record. when the case is closed as it turns out the case should be related to an existing record, so I need to delete the placeholder record. As a best practice, I cannot delete the related placeholder in before context but I need to update the case in before. Let me know if this clarifies the question.
    – P P
    Commented Oct 16, 2018 at 16:59
  • When Case 1 is inserted, Case 1 is related to RecordA(Placeholder) When Closed after review Case 1 need to be related to RecordB(Existing record) Before Context Update Case 1 => RecordB Make a tobeDeleted list with recordA -- I made this list static After Context If tobeDeleted != Empty, delete tobeDeleted Is this the correct approach? Or is there any better way?
    – P P
    Commented Oct 16, 2018 at 17:04
  • 1
    Can you provide all additional context by editing the question.
    – Jayant Das
    Commented Oct 16, 2018 at 17:05

2 Answers 2

0

Based on what I get that you are trying to achieve is that whenever your Case (say Case A) is moved to Closed, you want to link another Placeholder record (say Placeholder B) to that Case, and then delete the Placeholder record (say Placeholder A) which was initially associated to Case A.

You can do everything in your after update trigger.

Because your Case record is getting updated, you will always have the Trigger.Old populated with the OLD values on the Case record. That way, when you get the value of the lookup field for Placeholder, and get Placeholder A. Once you get that, you can just go ahead deleting that Placeholder A record, associate the new Placeholder B to Case and make all updates right there or update the Case in before and just utilize after for deleting Placeholder record.

2
  • If I use after trigger, I need to use one more update DML on case.
    – P P
    Commented Oct 16, 2018 at 17:31
  • You can update the case in before and then just get the old placeholder in after using trigger.old and delete it.
    – Jayant Das
    Commented Oct 16, 2018 at 17:32
0

Unless there's a specific reason you need to delete the placeholders in an after trigger, you can just do so in the before trigger. In fact, using a static variable can cause additional complications in the presence of partial retries, which may additionally complicate your code. If you choose to split the DML operations across the trigger before/after event boundary, make sure you test partial update failures in your unit tests.

2
  • The intention of using after context for deleting recordA was to make sure if in case of any exception the case is not updated and gets rolled back, the placeholder recordA should not be deleted.
    – P P
    Commented Oct 16, 2018 at 17:30
  • @MP If an exception/error occurs, the deletion will also be rolled back.
    – sfdcfox
    Commented Oct 16, 2018 at 17:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .