16 votes
Accepted

Obtaining Single Record - SOQL vs. Instantiating New Object

Do not use SOQL unless you must. If all the data you need to update comes from the child, simply use it sans query. Save governor limits wherever possible in your code. Opportunity master = new ...
  • 147k
15 votes
Accepted

Is it possible to convert a Master-Detail relationship to a Lookup?

From the Salesforce documentation: Considerations for Relationships Converting Relationships You can convert a master-detail relationship to a lookup relationship as long as no roll-up ...
  • 22.7k
12 votes
Accepted

"Cannot set sharingModel to ControlledByParent on a CustomObject without a MasterDetail" on an "sfdx force:source:push"

Based on the workaround from Cannot set sharingModel to ControlledByParent on a CustomObject without a MasterDetail relationship field: Workaround: Add the master-detail lookup field to the ...
  • 133k
12 votes

SOQL query to display number of opportunities, number of contacts, from account?

You can approach this in two ways. One, you can perform the parent-child subquery and check the size() of the returned list in Apex: List<Account> accounts = [SELECT Id, Name, (SELECT id FROM ...
  • 90.1k
9 votes
Accepted

Cannot set sharingModel to ReadWrite on a CustomObject with a MasterDetail relationship field

Okay this is how I resolved it. Based on the Metadata Field Types I set the sharingModel and the externalSharingModel both to ControlledByParent and everything built correctly! EDIT When I tried ...
  • 4,621
8 votes

Is it possible to convert a Master-Detail relationship to a Lookup?

Yes, this is a known issue. Mark Pond is correct, our information in that unit on Trailhead is inaccurate. You can convert as long as there are no RSF fields. https://help.salesforce.com/HTViewHelpDoc?...
8 votes
Accepted

Roll-up summary implications and behind the scenes calculations

There are following issues in your design. Account data skew: If an Account record has more than 10,000 child records then it is called Account data skew. It can also be broadly called as Parent-...
8 votes

The number of Lookup relationship that an object can have are as in today?

By default, the system is limited to 40 lookup fields per object, up to two of which may be master-detail relationships if it is a custom object. You can request more than the 40 relationship per ...
  • 462k
8 votes
Accepted

Illegal assignment from sObject to Id

You should be using the relationship name instead of the object name. Replace below code which is giving the error Revenue_Pipeline__c = new Revenue_Pipeline__c(External_Id__c = ext)); With ...
  • 6,466
7 votes

Convert SQL query into SOQL query

Also you can directly query in child object using relationship fields, [select ISOOffice__Merchant_Location__c,Account__r.Name from ISOOffice__Merchant_Opportunity__c where ...
  • 3,571
7 votes
Accepted

Any reason not to use Lookups instead of Master-Details for Junction object?

I see two reasons if we use look-up instead of master-detail type relationship: When you delete parent records, its child records will not be deleted. In case you delete both parent records, that ...
  • 22.4k
7 votes

Why can't Leads or Users be Masters in a Master-Detail relationship

This currently is not a functionality in Salesforce. Check out the below excerpt from the Relationships Among Objects developer page. You can define master-detail relationships between custom ...
  • 1,037
7 votes

Reading and writing child relationships of not-yet persisted SObjects?

You can do it via serialization if you want to go that far: class Proxy { final List<Child__c> records; Proxy(List<Child__c> children) { records = children; } Integer totalSize ...
  • 147k
7 votes
Accepted

Does Lookup Skew affect only master-detail relationships or also lookup ones?

Skew and lock contention are generally two different issues. About skew In a nutshell, skew is a matter of probability distribution. A good database index is one that has a uniform-ish distribution. ...
  • 54.7k
6 votes

Visibility of records in a junction object - AND based or relationshipOrder based?

If you followed the link from the page you referenced to Custom Object Security Overview you'd have found this: View a record: “Read” permission and read access to the related master record. ...
  • 40.7k
6 votes

Look Up relation Vs Master detail relation

Primary points in choosing MDR over lookup should be: Does it make sense? Think about structure. What do the objects represent? When you insert a cObj2 does it depends on the related cObj1? Roll-up ...
6 votes
Accepted

Do calculations on child records properties for master record

Just create a custom field on the parent object using the rollup summary data type. The field creation wizard will walk you though it. Create a formula field on the child "Total Amount" (quantity * ...
  • 462k
6 votes

Can We assign a null value to an Master Detail relationship?

If you would read the description of a Master-Detail field, you would notice the following: Creates a special type of parent-child relationship between this object (the child, or "detail") and ...
  • 5,176
6 votes
Accepted

Trigger to prevent edit on parent if there are no related child records

You could also prevent edits by rolling up a count of child records and place a validation rule based on that rollup.
  • 10.5k
6 votes

Changing Master-Detail to Lookup, who becomes Owner?

The OwnerId value from the Master record is used to set the value of the OwnerId on the child records when the relationship is changed from Master-Detail to a Lookup. Additionally, the Org-Wide ...
  • 22.7k
5 votes
Accepted

Creating custom object for class roster that would pull a list from contacts

For the above problem, I would suggest creating new Custom object, that will act as a Junction object between Roaster and Contacts. It will have look-up fields to Contact object and Roaster object. ...
5 votes
Accepted

How many master-detail relationships per object?

There is only 2 Master Detail Relationship allowed per object and it can have upto three custom detail levels. You can read more about it here, Considerations for Relationships
  • 673
5 votes

Can Many to Many relationship be achieved using lookup relationship?

Yes you can. A downside is that unlike when you use master-detail relationships, the junction object will not be automatically deleted when one or both of the objects it is relating are deleted. You ...
  • 133k
5 votes
Accepted

Inner SOQL child to parent query

For parent field references the extra "select" keyword is not used: List<detail__c> detailObj = [select id, master__r.Id from detail__c]; See the examples in the Using Relationship Queries ...
  • 133k
5 votes
Accepted

Unable to query child record through inner query

In setup click go to the child object and then click on the master-detail relationship field to Project and you will see the child-relationship name that should be used in the subquery. Example from ...
  • 10.5k
5 votes
Accepted

Unable to create Master Detail Relationship

As per Salesforce documented here: You cannot create a master detail relationship with a object which is a junction object. Please find the screenshot below: Custom junction objects can't have ...
5 votes
Accepted

When using FOR UPDATE does Salesforce also lock the child records in a master-detail relationship?

No. You must independently lock child records that you're interested in. Also see Avoiding Deadlocks, where they mention: Apex has the possibility of deadlocks, as does any other procedural logic ...
  • 462k
5 votes
Accepted

Can a master-detail relationship be blank?

Master-Detail fields are a special kind of required fields, it has special considerations beyond just whether a value is required or not. When you set up a child as a master-detail, you're saying that ...
  • 2,090
5 votes

SOQL query to display number of opportunities, number of contacts, from account?

If you are talking about total no. of Opportunity associated with all the accounts in your org:- SELECT COUNT(Id), Count(Accountid) From Opportunity the following query returns the number of ...
  • 14.3k
5 votes
Accepted

Master-Detail relationship questions

Record Sharing and CRUD/FLS access are very different things. You are correct that the detail will inherit the sharing from the master. CRUD and FLS are controlled by Profiles and Permission Sets. See ...
  • 1,451

Only top scored, non community-wiki answers of a minimum length are eligible