Вы находитесь на странице: 1из 26

Salesforce Training

By Gayatri Sharma
• Relationships associate objects with other objects.
• For example, a relationship can link a custom object to standard
object in a related list, such as linking a custom object called Bugs to
cases to track product defects associated with customer cases.
• For example, the account has a lookup on himself, called Parent
Sobject Relationship Types
• Master Detail
• Look up
• Self
• Hierarchical
What is “Master-Detail Relationship”?
• Master Detail relationship is the Parent child relationship.
• In which Master represents Parent and detail represents Child.
• If Parent is deleted then Child also gets deleted.
• Rollup summary fields can only be created on Master records which will
calculate the SUM, AVG, MIN of the Child records.
• Up to 2 allowed to object.
• Parent field on child is required.
• Access to parent determines access to children.
• Deleting parent automatically deletes child.
• A child of one master detail relationship cannot be the parent of another.
• Lookup field on page layout is required.
Master Detail
• Q: Can we create master detail relationship on existing records?
• A: "You cannot create a new Master-Detail relationship on
an existing custom object if records already exist. You must
first create a Lookup relationship, populate the lookup field with data
in all records, and then change the relationship type to Master-Detail
• Q: How many master detail relationships can you have on an object in
• A: There is only 2 Master Detail Relationship allowed per object and
it can have upto three custom detail levels. Each object is allowed to
have one or two masters, or up to 8 details.
What is a roll up summary field in Salesforce?
• Roll up summary field can only be defined on the master object.
While your formula fields calculate values using fields within a single
record, roll-up summary fields calculate values from a set of related
records, such as those in a related list.
What can a cross object formula reference?
• Cross-object formulas are formulas that span two
related objects and reference merge fields on those objects. Cross-
object formulas can reference merge fields from a master
(“parent”) object if an object is on the detail side of a master-detail
relationship. Cross-object formulas also work with lookup
What is a “Lookup Relationship”?
• On a standard or custom object, a lookup relationship creates
a field that allows users to click a lookup icon and select another
record from a popup window. On an external object,
the lookup relationship field references 18-character SalesforceIDs
that are stored in an external data source.
What is a “Lookup Relationship”?
• Up to 25 allowed for object.
• Parent is not a required field.
• No impact on a security and access.
• No impact on deletion.
• Can be multiple layers deep.
• Lookup field is not required.
If you have any of these 4 requirements, then
go with master detail (otherwise lookup):
• When the parent record is deleted all the child records attached
needs to be deleted.
• All the users who can access a specific parent record will also has
access to all the related child records (parent has private OWD setting
and has sharing rules on top of it)
• wants to create rollup summary fields on parent
• activities tagged to child records has to be shown on parent account
Junction object
• the sole purpose of a junction object is to simply relate two objects, so it
often makes sense to give the junction object a name that indicates the
association or relationship it creates. For example, if you wanted to use a
junction object to create a many-to-many relationship between bugs and
cases, you could name the junction object BugCaseAssociation.
• Let's look at a typical scenario. There are open positions for a Project
Manager and a Sr. Developer. The Project Manager position is only posted
on Monster.com, but the Sr. Developer position is more difficult to fill, so
it's posted on both Monster.com and Naukri. Every time a position is
posted, a job posting record tracks the post. As you can see in the following
diagram, one position can be posted many times, and both positions can
be posted to the same employment website.
Entity Relationship Diagram for the Position,
Job Posting, and Employment Website Objects

Consequently, in order to define a many-to-many relationship

between the Position and Employment Website objects, we'll
need to create a Job Posting object with the following fields:
A Position master-detail relationship
An Employment Website master-detail relationship
Exercise 1:
1.) Create a custom App
2.) Create a custom sobject1 with fields status(picklist), dependent picklist
fields, text field and long text field
3.) Create a custom sobject2 with fields status(picklist), dependent picklist
multi select picklist fields, text area field and long rich text field
4.) Create a custom sobject3 with fields currency, number with decimals,
encrypted field, text area field and long rich text field
5.) Create a Master detail field on custom object1 related to custom sobject3
6.) Create a Master detail field on custom object2 related to custom sobject3
Exercise 2:
7.) Create a custom sobject4
8.) Create a custom sobject5
9.) Create a custom sobject6
10.) Create a look up field on custom object4 related to custom
11.) Create a look up field on custom object5 related to custom
12.) Create records for all custom sobject1,2,3,4,5,6.
Exercise 3:
• On master object create a roll up summary field to show count of
detail/child records
• Create formula fields
• Convert Master detail to Look up
• Convert Look up to Master detail
• Convert text field to Picklist
• Convert picklist field to Text
• Convert text to text area. Convert text long area to text
• Perform all field data type conversion POC
• When an object has a lookup with itself, it is a self-relationship. A self
relationship creates a tree diagram of the objects.
• Salesforce does not force any special behaviour when doing this self
• For example, the account has a lookup on himself, called Parent Account.
• A self lookup is a hierarchical relationship. Using the self lookup, you can
have an object that has a child object (of the same kind), which in turn has
its own child object (of the same kind), making that record a grandchild of
the original object.
• On the user object a special self-relationship called the hierarchical
relationship helps create superior roles such as supervisor, manager on
user object. The hierarchical relationship is a self-relationship of the user
What is Hierarchical relationship? What is its
• This type of relationship is a special lookup relationship available only
for the user object. It allows users to use a lookup field to associate
one user with another that does not directly or indirectly refer to
itself. For example, you can create a custom hierarchical relationship
field to store each user's direct manager.
• A lookup relationship on the user object to the user object
• Go to setup--- customize--- user -- fields--- create new custom field --
there you will find hierarchy relationship instead --Master -detail and
With Hierarchical type of relationship are easy
ways to get:
• a user's complete upstream hierarchy (List)
• a user's complete downstream hierarchy (tree structure and/or Set)
• Determine if a user is in another user's upstream and distance
• Determine if a user is in another user's downstream and distance

When creating a hierarchical field in Personal, Contact Manager, Group,

and Professional Editions, you can select the Restricted Field checkbox
so that only users with the “Manage Users” permission can edit it. In
Enterprise, Unlimited, and Developer Edition, use field-level
security instead.
Hierarchical Relationship Example :1
When developing an Approval Process, a Custom Hierarchical Relationship
field on the user object enables you to specify, for a given user, who is the
next person up in a custom hierarchy. This is useful if the standard Manager
field (which is populated based on the applicable Role Hierarchy) is not
appropriate for the given approval process that you are developing. So, for
example, if there was a committee in a company that had people across
various departments, and they wanted to implement an approval process for
various proposals that they were considering, then the standard Role
Hierarchy would not apply. Instead, you could define a custom hierarchy of
approvers using a custom hierarchical relationship field and use that field to
designate a specific "higher-up" for each user in the hierarchy. Then you
could set up the approval process to use the value in that field to determine
who is the next approver for a given step.
• Recently, I came across one of requirement where client wanted to
have a custom lookup field on User object. This lookup will be on
User. Based upon certain criteria, value of this field will go to Manager
• On user object create a new field of type 'Hierarchical Relationship'
for creating any sort of relationship. It Creates a hierarchical lookup
relationship between users.
Roll Up Summary Limitations
• Roll-ups are easy to create on Master-Detail relationships as they are available as
standard field type in Salesforce. In certain situations we need to write apex code
to roll-up the child information. Here are some limitations we have:
• Only 25 roll-up summary fields are allowed per object on Master-Detail
• We cannot add lists or set of values in a criteria.
• Native roll-up summary fields are not available on a LOOKUP relationship.
• The following items are the supported function in a Lookup Rollup:
• SUM ():Returns the summation of numeric field.
• MIN (): Return the minimum value of field.
• MAX (): Return the maximum value of field.
• AVG (): Return the average value of numeric field.
Example 4: How to provide Roll-up Summary
features to a lookup relationship.
• Please follow the simple instructions below to setup this feature:
• Use the "Code Snippet" link (given above) to download the class “RollUpSummaryUtility”
• Let’s consider Application__c is child object of Student__c, And we want to roll-up 'Fee__c' from
'Application__c' object to 'TotalFee__c' of 'Student__c' object
• The relationship between Student__c and Application__c: One student can have multiple
Trigger ApplicationTrigger on Application__c (after insert, after update, after delete, after undelete) {
List<Application__c> applications = new List<Application__c>();
if(Trigger.isInsert || Trigger.isUndelete){
applications = Trigger.new;
}else if(Trigger.isUpdate){
for(Application__c app : Trigger.new){
if(app.Fee__c != Trigger.oldMap.get(app.id).Fee__c){
} else if(Trigger.isDelete){
applications = trigger.old;
list<RollUpSummaryUtility.fieldDefinition> fieldDefinitions1 =

new list<RollUpSummaryUtility.fieldDefinition> {

new RollUpSummaryUtility.fieldDefinition('SUM', 'Fee__c',



RollUpSummaryUtility.ObjectDefination objDef =
new RollUpSummaryUtility.ObjectDefination('Application__c','StudentId__c','Student__c','',null);

new RollUpSummaryUtility().rollUp(fieldDefinitions1, objDef , applications);

A. "FieldDefinition" is inner class in "RollUpSummaryUtility". To construct FieldDefinition object we need to pass 3
Operation : Operation to be performed like Sum, Max, Min, Avg. In our case it is SUM
childField : Source field on child object for rolling up data. In our case it is Fee__c field of Application__c
ParentField : Destination field where data will be rolled up. In our case it is TotalFee__c field of Student__c
B. ObjectDefinition is another inner class in a RollUpSummaryUtility. To construct ObjectDefinition object we
need to pass 5 parameters.
Child Object Name : Name of child object in lookup relationship
Look up Field Name : Lookup field name on child object
Parent Object Name : Name of parent object in lookup relationship
Additional Filters : If you want to pass any filters like stage = ‘Closed won’ etc
List filter: If you want pass any list filters. Please follow the following syntax
Ex :
List<String> strlist = new List<String>();
new RollUpSummaryUtility.ObjectDefination('JobApplication__c','StudentId__c','Student__c',' and Name IN :
XYZ', strlist);
(Please note that XYZ is internally used in dynamic query so make sure that you pass it.)
C. Finally call the rollup() method of RollUpSummaryUtility class which accepts 3 parameters:List of
FieldDefinition instances
ObjectDefinition instance
List of child records to process in our case it's applications
Practice all examples minimum two times 
Thank You !!