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
Eledra NguyenEledra Nguyen 

Upsert reference object

Hi all,

 

I'm a beginner with Salesforce platform, i have a small question with upsert object data:

 

Let's say i have 2 custom objects A and B. They have many - to - many relationship.

 

So i created a reference ojbect: ref_A_B which provides master-detail relationship to A and B.

 

I is very easy when i want to delete A or B, ref_A_B will be automatically deleted.

 

However, when i want to update A by adding or removing some B object inside it, i have to delete all of ref_A_B relating to A and B and then re-create new ones.

 

I know when we query database, we can use sub query to get ref object, can i apply the same method for update ?

 

Many thanks,

Best Answer chosen by Admin (Salesforce Developers) 
ClintLeeClintLee

I don't believe you can change the master record of a child record once it's been created.  So, once your junction object has been saved you cannot change which A or B it looks up to.

 

Here is an excerpt from the docs:

 

Two types of relationship field types can be defined. Here is a description of each:

  • Lookup Relationship - Creates a relationship that links one object to another object. The relationship field allows you to navigate from records in one object to the related records in another object (both visually, and programmatically).
  • Master-Detail Relationship - Creates a special type of relationship between two objects (the child, or "detail") and another object (the parent, or "master"). Among the additional details a) The relationship field is required on all detail records, b) Once the value of the relationship field has been saved, it cannot be changed, c) Deletes are cascaded from the master to the detail 

 

Hope that helps!

 

~ Clint