What could be the cause of the issue in the following case? A - ProProfs Discuss
Topics
Products
Follow Us:

What could be the cause of the issue in the following case? A developer writes the following Apex trigger so that when a Case is closed, a single Survey record is created for that Case. The issue is that multiple Survey-c records are being created per Case trigger : CaseTrigger on Case (after insert, after update)List createSurveys = new List();for (Case c : trigger.new)if (c.IsClosed && (trigger.isInsert lltrigger.isUpdate && trigger.oldMap.get(c.ID).Is Closed == false)){createSurveys.add(new Survey__c(Case__c = c.ID));insert create Surveys;



A. User is creating the record as Closed.
B. A workflow rule is firing with a Create Task action.
C. A workflow rule is firing with a Field Update action.
D. A user is editing the record multiple times.

This question is part of PD2
Asked by Pal, Last updated: Mar 31, 2020

+ Answer
Request

4 Answers

justanotherdev inthewild

im prepping for cets

Justanotherdev inthewild, Dev, bachelor, India

Answered Mar 23, 2019

A user updating the record multiple times does not look like the answer if you think about it. The trigger checks if the closed checkbox was previously unchecked. Now if a user was to uncheck the checkbox -> save, then check => save, there would be another survey created as per design. This is correct since in the last save, the case was closed from a not-closed status, even if its a second close of the case. Remember that they are 2 independent transactions.

A workflow rule with field update is a better reason since according to docs-

"When a record is updated and subsequently triggers a workflow rule field update,Trigger.oldin the last update trigger doesn’t contain the version of the object immediately before the workflow update, but the object before the initial update was made. For example, suppose that an existing record has a number field with an initial value of 1. A user updates this field to 10, and a workflow rule field update fires and increments it to 11. In the update trigger that fires after the workflow field update, the field value of the object obtained fromTrigger.oldis the original value of 1, rather than 10, as would typically be the case."

So a workflow field update would run the after update trigger in the SAME transaction and trigger.old would have the same value, thereby causing another survey to be inserted. Phew!

1
 

zhengtianbing

Zhengtianbing

Answered Jul 24, 2019

C. A workflow rule is firing with a Field Update action.

 

devilzP

DevilzP

Answered Apr 05, 2018

A. User is creating the record as Closed. .. not sure

 

Pal

Pal

Answered Nov 20, 2017

A user is editing the record multiple times.
 

Search for Google images
Select a recommended image
Upload from your computer
Search for Google images
Select a recommended image
Upload from your computer
Search for Google images
Select a recommended image
Upload from your computer

Email Sent
We have sent an email to your address "" with instructions to reset your password.