PDI · Question #27
A developer created a custom order management app that uses an Apex class. The order is represented by an Order object and an Orderltem object that has a master-detail relationship to Order. During…
The correct answer is C. Create a junction object between Orderltem and Order. To allow existing OrderItem records to be associated with multiple Orders, such as when an order is split, a many-to-many relationship using a junction object between OrderItem and Order is necessary.
Question
A developer created a custom order management app that uses an Apex class. The order is represented by an Order object and an Orderltem object that has a master-detail relationship to Order. During order processing, an order may be split into multiple orders. What should a developer do to allow their code to move some existing Orderltem records to a new Order record?
Options
- AChange the master-detail relationship to an external lookup relationship.
- BAdd without sharing to the Apex class declaration.
- CCreate a junction object between Orderltem and Order.
- DSelect the Allow reparenting option on the master-detail relationship.
How the community answered
(62 responses)- A2% (1)
- B10% (6)
- C82% (51)
- D6% (4)
Why each option
To allow existing OrderItem records to be associated with multiple Orders, such as when an order is split, a many-to-many relationship using a junction object between OrderItem and Order is necessary.
Changing to an external lookup relationship would still maintain a one-to-many relationship and does not inherently allow an OrderItem to relate to multiple Orders, nor does it typically relate to 'moving' internal records.
Adding `without sharing` to an Apex class declaration impacts security and visibility, but it does not change the underlying data model or relationship types to allow records to be moved or associated with multiple parents.
Creating a junction object between OrderItem and Order converts the relationship to many-to-many, allowing a single OrderItem record to be flexibly associated with multiple Order records, which is crucial when an order is split into multiple new orders and items might relate to more than one resulting order or are shared.
Selecting 'Allow reparenting' on a master-detail relationship enables a child record to change its single parent, but it does not allow a single child OrderItem to be simultaneously associated with multiple Order records.
Concept tested: Many-to-Many Relationships with Junction Objects
Source: https://help.salesforce.com/s/articleView?id=sf.overview_of_custom_object_relationships.htm&type=5
Topics
Community Discussion
No community discussion yet for this question.