Wednesday, June 28, 2017

Relationships in Salesforce :

What are the difference between Lookup and Master Detail relationship?

Ans: Difference between Lookup and Master-Detail relationship are

Master Detail
 1. Cascade record deletion 
 2. Child record must have a parent
 3. Cascade record level security
 4. Standard Object can not be on detail side of Master Detail
 5. Roll-up summary fields on parent object
 6. Master Detail relationship field is required on the page layout  of the detail record

Lookup
 1. This is optional, no parent requirement 
 2. No impact on record security
 3. Roll-up summary fields cannot be established
 4. Lookup relationship is not automatically required

What is difference between 15 digit and 18 digit Record id?
In Salesforce, whenever user create any component (Object, field, tab etc...) or record then salesforce will generate an unique id with which user can identify the record or component.
After creating the record, in the URL user can see the id of the record which is of 15 digits length.
15 digit id- It is case sensitive and can be seen in user interface in URLs and Reports
18 digit Id- It is case insensitive. APIs and Apex (SOQL) always return 18 digit Id. Even through apex dataloader, we get 18 digit Id.

First 3 digit of record Id specifies the object to which record belongs. For example account records starts with “001” & contact record start with “003”.

What are different types of relationship in salesforce?
Basically there are 2 types of relationship, lookup and master detail.
Other relationships are:

  • Self-relationship- When object is related to itself. For example a case can be related to parent case.
  • Hierarchical relationship- When user has relationship with user. For example we can have manager field in user object which is related to user object only.
  • Many to many relationship- When an object is having master detail relationship with 2 different objects.  The intermediate object is called as junction object. An object is called junction object only if it has master detail relationship with 2 different objects.
Can a standard object become child in master detail relationship?
No. Standard objects always remain master.

12. What are roll up summary fields?
Rollup-summary fields are supported in master detail relationship. The parent object can use roll-up summary field type to perform operations of sum, maximum, minimum, count among its children records.

13. What is junction object?
Junction object is an object which has master detail relationship with 2 different objects. Many-to-Many relationships are implemented using two master detail objects. One Junction object is used as the child of the objects between which many-to-many relationship needs to be established.

14. What is difference between self-relationship and hierarchical relationship?
Self-relationship is a lookup relationship to itself. For example a case record can be related to parent case record.
When there is self-relationship exist on user object, then it is called hierarchical relationship. For example, an employee's manager is also an employee.

15. Can we change already existing lookup relationship between 2 objects to master detail relationship?
System will allow changing relationship to master-detail only if all child records have value specified for parent as in master detail relationship, master is required.
If any of the child value doesn’t have parent value (means look up field is blank), then system will give validation error while changing the data type.
So if it is required to change relationship, them populate all blank values with some parent value and then change it to master detail.

Is it possible to change master-detail relationship to lookup relationship?
If the parent object doesn’t have Roll up Summary fields for the child object then we can convert.

Is it possible to create the Master – Detail Relationship field for the child object which is having existing records?
No, we cannot create directly. To create first we should create Look up relationship then populate the field value for all the records and then convert the look up relationship to master detail relationship.

What will happen if we undelete junction object and normal object from recycle bin?
Master – Detail Relationship data types will be converted to look up relationship data types.
If we undelete any object from recycle bin, then all master detail relationship will be changed to lookup relationship.

What will happen if we delete child record in case of look up relationship and master detail relationship?
In case of lookup relationship, we can delete parent object or child object. When parent object will be deleted, the value of lookup relationship field in all child records will become blank.
In case of master-detail relationship, if parent object contain roll up summary, then system will not allow you to delete child object.Also you cannot delete parent object in master detail relationship.

Is it possible to filter the records which are coming in look-up window when we click on lookup icon?
Yes, define filter criteria while defining the look up relationship.

one master object A is private and other master object B is public then what is OWD for child object c?

A)Private.

if we have two objects A and B .and we have trigger on A to insert record in b.But user doesn't have access to B object what happens then?

A)Yes insert record ,Because trigger can run in system mode.


Here is the relationship between Opportunity,OpportulityLineItem ,PricebookEntry,Pricebook and Product2 .


Here's a schematic of the relationships involved.
User-added image


1. OpportunitylineItem is junction object between opportunity and pricebookentry.
    As We need PricebookEntry Id and opportunity Id while creating OpportunityLineItems.

2. PricebookEntry is a junction object between Product2 and PriceBook.
    As we need Product2 Id and PriceBook2 Id while creating PricebookEntries.

Currently I do the following to create a one-to-one relation between two objects:

  1. Create a lookup field on the child
  2. Create a unique field on the child, and hide this field from all page-layouts
  3. Write a workflow, for any change of the lookup field, to copy that value from the lookup field into the unique field

This process has many overheads:


  1. Extra field is required
  2. Unique criteria is utilized (we only get 3 unique fields per object)
  3. Workflow is used

 

What are the different types of object relations in salesforce? How can you create them?

No list of Salesforce interview questions is complete without involving relationships between objects in Salesforce. Relationships in Salesforce can be used to establish links between two or more objects.
The different types of object relationships in Salesforce are:
Master-Detail Relationship (1:n):-
It is a parent-child relationship in which the master object controls the behavior of the dependent child object. It is a 1:n relationship, in which there can be only one parent, but many children.
The main concept you need to be know is that, being the controlling object, the master field cannot be empty. If a record/ field in master object is deleted, the corresponding fields in the dependent object are also deleted. This is called a cascade delete. Dependent fields will inherit the owner, sharing and security settings from its master.
You can define master-detail relationships between two custom objects, or between a custom object and standard object as long as the standard object is the master in the relationship.
LookupRelationship(1:n):-
Lookup relationships are used when you want to create a link between two objects, but without the dependency on the parent object. Similar to Master-Detail relationship, you can think of this as a form of parent-child relationship where there is only one parent, but many children i.e. 1:n relationship.
The difference here is that despite being controlling field, deleting a record will not result in automatic deletion of the lookup field in the child object. Thus the records in the child object will not be affected and there is no cascade delete here. Neither will the child fields inherit the owner, sharing or security settings of its parent.
JunctionRelationship(Many-To-Many):-
This kind of a relationship can exist when there is a need to create two master-detail relationships. Two master-detail relationships can be created by linking 3 custom objects. Here, two objects will be master objects and the third object will be dependent on both the objects. In simpler words, it will be a child object for both the master objects.

What happens to detail record when a master record is deleted? What happens to child record when a parent record is deleted?

In a Master-Detail relationship, when a master record is deleted, the detail record is deleted automatically (Cascade delete).
In a Lookup relationship, even if the parent record is deleted, the child record will not be deleted.

Can you have a roll up summary field in case of Master-Detail relationship?

Yes. You can have a roll-up summary in case of a master-detail relationship. But not in case of a lookup relationship.

Salesforce: Master-Detail relationship (in detail)

One of the most advantage using Master-Detail relationship compare to Lookup relationship is Roll-Up Summary. In Master-Detail relationship, we can create roll-up summary field in master record to summarize / count data from child records.

Few items to notice when create roll-up summary field:
Parent record is locked
If you implement approval process with lock record, user will not able to create new child record if Sharing Setting in Master-Detail option is Read/Write.
Solution: change the sharing setting to Read Only.







Validation rule on parent record
If user not able to edit parent record and you have roll-up summary in parent record, user will not able to create or edit child record.
Solution: remove roll-up summary fields.

Can I change field type from Master-Detail to Lookup relationship?
Yes, make sure there is no roll-up summary fields in parent record, also in Deleted Fields (erase from Deleted Fields).

Can I change field type from Lookup to Master-Detail relationship?
Yes, all parent record should be not blank.



Master-Detail relationship



Sometimes new admin confuse what is the different between Master-Detail relationship with Lookup relationship, since both are used to link an object to another object and you can use parent-child relationships in SOQL queries.

But, fundamentally it is different between Master-Detail with Lookup relationship. Few items you need to notice on Master-Detail relationship:

  1. When a record of the master object is deleted, its related detail records are also deleted.
  2. The owner field on the detail object is not available and is automatically set to the owner of its associated master record.
  3.  The detail record inherits the sharing and security settings of its master record.
  4. The master-detail relationship field is required on the page layout of the detail record.
  5. By default, records can’t be reparented in master-detail relationships. Administrators can, however, allow child records in master-detail relationships on custom objects to be reparented to different parent records by selecting the Allow reparenting option in the master-detail relationship definition.
  6. You can define master-detail relationships between custom objects
  7. You also can define master-detail relationships between a custom object and a standard object, but the standard object cannot be on the detail side of a relationship with a custom object. In addition, you cannot create a master-detail relationship in which the User or Leadobjects are the master.
  8. You can display detail object record as custom related list on master object page layouts.
  9. You can use master-detail relationships to model many-to-many relationships between any two objects. Maximum 2 master-detail relationship from a custom object.
  10. If the object is master of 1 or more detail object, you only can have 1 parent object for that object.

Salesforce Opportunity Roll-Up

Roll-Up summary field is a nice feature in Salesforce to summary data from child record to parent within Master-Detail relationship object.

Although, Account and Opportunity is standard Salesforce object, but we can set Roll-Up summary for many purposes.

Date of last won deal
We can easily know and display when closed won opportunity in Account page layout without complex triggers.
Create roll-up summary field in Account with
- Summarized Object = Opportunity
- Field to Aggregate = Opportunity: Close Date
- Filter Criteria = Won EQUALS True
- Summary Type = MAX

Number of Opportunities


Display number of opportunities in Account page layout


Create roll-up summary field in Account with
- Summarized Object = Opportunity
- Filter Criteria =
- Summary Type = COUNT

Number of Won Opportunities
Display number of won opportunities in Account page layout
Create roll-up summary field in Account with
- Summarized Object = Opportunity
- Filter Criteria = Won EQUALS True
- Summary Type = COUNT

Total Amount of Won Opportunities
Display total amount of won opportunities in Account page layout

Create roll-up summary field in Account with
- Summarized Object = Opportunity
- Field to Aggregate = Opportunity: Amount
- Filter Criteria = Won EQUALS True
- Summary Type = SUM

Win Percentage
Display percentage of closed won opportunity for account
Create formula field with return type = Percent, with 2 decimal place
Formula = IF(Count_of_Opportunities__c > 0, Count_Won_Opportunities__c / Count_of_Opportunities__c,0)

No comments:

Post a Comment