Saturday, October 20, 2018

Relations in Salesforce

Relations:

1. Relations are used to establish connection between two or more objects.
2. Salesforce provides different types of relations
    i) Master-Detail Relations
    ii) LookUp Relations
    iii) Many - to - Many Relation(junction object)
    iv) Hierarchial Relations
    v) External Lookup relationship
    vi) Self Relationship
3. Master-Detail Relations :
    1. It is one to many relationship between two objects
    2. Master - detail field can be created only on custom objects
    3. Master - detail field can be created only on those custom objects on which their are no records in it.
    4. Master - details field is a required field
    5. In one to many relation ship on many side of the relation object, Master - Detail field is created.
    6. Object on which master - Detail field is created that object we call it as child object/ detail object/ Related object.
    7. In one to many relation of master - detail, object to which data is referred in the master-detail field is called as master object/parent object.
    8. Which ever the record that master-detail field is referring, that master - record recordid is stored in the master-detail field.
    9. If the master record is deleted, corresponding child records are also deleted, but deleted child record will not move to recycle bin.
   10. If we undelete the master record corresponding child records are also undeleted.
   11. Object in which master-detail field is created, that object will not have any owner field in it.
   12. Who ever is the owner of the master record, he will be the owner of corresponding child records.
   13. sharing setting
         This will specify the minimum access level required on the master record to create, edit, or delete related child records:
    a. Read only : If you choose this option, allows users who has atleast read access to he master record can not perform create, edit, or delete operations on corresponding child records.
    b. Read/Write : If you choose thi option, allows users who has atleast read/write access to the master record can perform create, edit, or delete operations on corresponding child records.
  14. Allow Re-parenting: If this option is enabled, child can change it's master record after creation of child record.
  15. An object can be a master to any no of objects.
       1. A                  2.          A                        3.                       A
            |    yes                       |          yes                                     |             yes
           B                         -----------                              -------------------------
                                       |             |                              |              |                |
                                      B           C                            B            C              D


 16. On an object we can create only two master - detail fields.
       1. A             2.   A             B                    3.    A            B            C
            |   yes            |              |    yes                     |              |               |     not allowed
           B                   ------------                              -------------------------
                                       |                                                      |
                                      C                                                    D

17. Child object can be parent to any other object upto three levels
      1.   A         2.   A         3.  A         4.  A
             |   yes        |   yes       |    yes       |   No
            B              B              B               B
                             |                |                 |
                            C              C               C
                                              |                |
                                             D              D

18. If an object has 2 parents on it, it can not be parent to any other object.
                    A           B
                    |              |
                    ------------
                          |
                         C
                          |   Not Allowed
                         D                       

19. Maximum structure what we can form using master-detail
                  A           X
                   |             |
                  B           Y
                  |              |
                 C            Z
                  |             |
                 ------------
                       |
                      D
4. Junction Object:
     1. Junction object is a custom object.
     2. Junction object has two master-detail fields on it.
     3. Junction object maintains Many to Many relation.
     4. First master-detail field created on the junction object is called primary master.
     5. Second master-detail field created on the junction object is called secondary master.
     6. Look and feel and ownership is inherited from the primary master.
     7. If you delete any master record, corresponding child records are deleted.
     8. If any master record has more than 200 child records in the junction object, then we cannot delete that master record.

UseCase:
1. Create two custom objects
    a. Course
         SNO        Field Label              Field Name     DataType      Options
    ---------------------------------------------------------------------------------------
            1.          Course Name            Name              Text                   -
            2.          Fee                            Fee                  Currency           -
            3.          Status                        Status              Picklist         Active, Inactive
   b. Branch
          SNO       Field Label             Field Name       Data Tyep       Options
   -----------------------------------------------------------------------------------------
            1.          Branch Name            Name                 Text                  -
            2.          Phone                        Phone                 Phone               -
            3.          Email                         Email                 Email              -
2. Create custom object student using schema builder
           SNO         Field Lable         Field Name       Data Tyep       Options
    -----------------------------------------------------------------------------------------
            1.             StudentID             Name                Auto Number     -
            2.             First Name          First Name            Text                 -
            3.              Last Name          Last Name            Text                 -
            4.              Course                 Course             Master-Detail(course)
            5.              Branch                 Branch             Master-Detail(branch)
3. Goto student detail page and add the fields to the pagelayout.
4. Create new application collegeapp.
5. Create new custom object tabs for course, branch, student and add them to collegeapp application.

5. Lookup Relation:
    1. It is one to many relationship.
    2. It can be created on both standard and custom object.
    3. It can be created on both objects which contain data or which doesn't contain data.
    4. If you delete master record, corresponding child records will not be deleted.
    5. Owner of the parent record and child record can be same or different.
    6. We can create 40 lookup fields on an object.
    7. It is an optional field.
    8. We can create self lookup on an object.
    9. Don't allow deletion of lookup record which is a part of lookup relation, if we enable this option parent record which gas child records using lookup cannot be deleted.

6. Hirerarchical Relation:
    1. This can be created only on user object.
    2. It is one to one relationship.
    3. This is used to create a relation between user to user.
    4. Steps to create hirerarchical relation
          Setup
             |-----Build
                        |------Customize
                                        |---------User
                                                        |-----Fields
                                                                    |------Custom fields and relations
                                                                                                             |----------New
           Step 1: Choose datatype as hierarchical relation
           Step 2:  ENter field name and details
           Step 3:  Add field level security
           Step 4:  Add this to the page layout.

7. Rollup- Summary:
1. Rollup-summary fields can be created only on master object in master-detail relation.
2. Rollup summary is used for applying aggregate functions on corresponding child records.
3. Count(): This will return count of no of child records participating in the rollup operation.
4. Sum(Number/Currency/Percent): This is used to make the summarized value of given child records participating in the rollup.
5. Max(Number/Currency/Percent/Date/DateTime): This is used to return the maximum value from the corresponding the child records participating in the rollup operation.
6. Min(Number/currency/percent/data/datetime):This will return the minimum value from the corresponding child record which are participating the rollup operation.
7. We can create the filter condition to specify which child records should participate in the rollup operation.
  a. allow all the child records: if we choose this all the child records of the master record will participate in the relation.
  b. allow only those child records which are meeting the filter condition: if we choose this, only those child records which are meeting the filter condition can participate in the relation.
8. We can create 25 rollup summary fields on a object.
--------------------------------------------------------------------------------------------------------------------------
1. ISBLANK(FieldName):
    1. This will return true if the given field value is blank.
    2. This can be used for both text and numerical fields.
Example:
     ISBLANK(lastname__c)
     ISBLANK(Email)

2. ISNULL(FieldName):
    1. This will return true if the given field value is null.
    2. This can be used for only numerical fields.
    Note : Text fields are never considered as null.
    Example :
         ISNULL(Salary__c)
         ISNULL(AnnualRevenue)

3. AND(cond1, cond2, .....)
    1. This is used to test more than one condition
    2. This will return true if all the conditions are true
    Example:
        AND(Age__c > 30, Salary__c < 30000)
4. OR(Cond1, cond2,...)
    1. This will used to check more than one condition
    2. This will return true if any one of the condition is true
example:
OR(city__c == 'HYD', Branch == 'MADHAPUR')
5. ISPICKVAL(Picklist, Literal) : (Fieldname, Option)
    1. This is applied only on picklist field
    2. This will return true if the option what we have selected in the field is same as the literal value what we have given.
example:
ISPICKVAL(Mode__c, 'online')
6. IF(condition, stmt1, stmt2) :
    1. If the given condition is true, stmt1 will be returned
    2. If the given condition is false, stmt2 will be returned.
example:
    IF(salary__c > 50000, 'selected', 'not selected')
    IF(ISPICKVAL(Industry,'education'), 30000, 50000)
7. CASE(expression, 
value1, result1, 
value2, result2, 
valuen, resultn, 
defaultvalue)
example:
CASE(city__c,
'HYD','TG',
'Ban', 'KA',
'Che','TN',
'India')

  8. Image(Url, defaulterrormessage):
    1. This is used to print images on the detail page.
8. Date and Time Functions:
    1. Today() : this will return today's date
    2. Now() : This will return current date and time
    3. Date(YYYY,MM,DD) : this is used to create a instance of a date.
        Ex: DATE(2016,2,23)
    4. DATEVALUE(DateTime) : This will take datatime value and convert into date.
        example: DATEVALUE(NOW())
                        DATEVALUE(2-2-2015) : DATE(2015,2,2)
    5. DAY(DATE) : This will return the day in the month
        ex: DAY(TODAY()) : 3
              DAY(DATE(2015,2,23)) : 23
   6. MONTH(DATE) : This will return the month in the given date
   7. YEAR(DATE) : This will return the year in the date.
   8. ISCHANGED(Fieldname) : This function returns true if the value in given field is changed.
   9. PRIORVALUE(Fieldname): This function returns old version of modified value.
   10. ISNEW() : This function returns true if the validation is fired due to new record.
9. Cross-Object Formula:
   1. If you want to refer to data from parent record while defining the formula fields on child object, then we call it as cross-reference formula or cross-object formula.
   2. if two objects are connected by a lookup/master detail, If field was created by salesforce then we can refer to the parent record data from child object by using RFieldname.PField.
  3. If two objects are connected by a lookup/master detail, if relation field is created by user, we can refer to the parent record data from child object.

10. Regex() Function:
      1. This function is used to validate the user input with the specified format.
      2. It returns true, if the user input in the same format. else it returns false.
      syntax: regex(userinput, 'expression format')
      Note: we need to prepare the regular expression format by using "Wild Card Characters".
      [ ] --> describes a collection of characters
      [A - Z] --> It describes a collection of characters falls between A - Z.
      [a - z] --> Describes the collection of characters falls between a - z
      [0 - 9]] --> Describes the collection of digits falls between 0 -9
      { number } --> Describes the number of characters.
      [ a p r] --> describes a collection of characters whch contains a /p / r.
      [A - Z, a - z] --> It accepts a collectio of charcters falls between A-Z and a-z.

Validation Rules:

1. There will whether the format of the data that given the field is compatible with your business requirement or not.
2. There validation rules are verified when we create a new record or update the existing record.
3. There are two types of validation rules
    1. System validation
    2. Custom validation
System Validation Rules:
------------------------------
There are created by the salesforce which is to verify
1. All required fields are entered or not
2. DataType of the field is compatible with the format of the given value

Custom Validation Rules:
-------------------------------
There are created by the user to meet the business requirement
ex: age should not be less than 18
ex: salary should be greater than 50000

Note: validation rules are verified when a record is created or when a existing record is modified.
validation rules checks the conditions is true or not, if it is true then it prints the custom error message defined by the user.




   



















No comments:

Post a Comment