Sunday, June 14, 2020

Apex Percent Usage

I wanted to check on how is Apex code counted towards the limit, considering we are only allowed 6,000,000 characters (6 MB ) and we might come across a situation where we may want to increase the aforementioned limit.

Salesforce allows you to increase limit upto 10,000,000 characters (10MB) on logging a case.

Salesforce doc

I have created a few scenarios to check on what all simple things contribute towards this usage.

After creating a dummy class below is apex usage, we would run different scenarios on below class to see how is apex usage changed.

  • Adding redundant new lines (/n) in the code doesn't impact the usage.
  • Adding redundant spaces/tab in between the code increases the usage.
  • Adding redundant spaces/tab even at the end of line increases the usage.
  • Adding spaces/tabs for indentation of code also seems to increase the usage(for demo purposes I have indented class variables only)
  • Adding comments in class don't impact the usage.

  • Creating a test class don't count towards apex usage ie redundant tabs/spaces none of them are counted.

  • Blank lines containing only spaces are also not counted towards the usage.

Note : Managed packages are not counted towards the limit as they belong to different namespace as that of your org.

Thanks for reading.

If you liked the post do support me by buying a pizza for me :D


Saturday, May 30, 2020

Deep dive into Master Detail Relationships


In this post we will talk about things that are there to know related to M-D relationship.
I will try to cover things from basic to corner cases around M-D relationships.
Hopefully you will find something new in below post.
  • Owner field is not available on detail object rather it is set to owner of master record.
  • Sharing rules, manual sharing or queues cant be defined for detail object as these require owner field
  • OWD of detail object is controlled by Parent.
  • We have limit to create 40 relationship on an object and that limit can be achieved in any of below pattern. 
    • 39L 1 M
    • 38L 2 M 
    • 40L
(where L-> Lookup , M-> Master-Detail)
  • When we say an object has limit of 2 M-D its related to how many parents an object can have not how many details a parent can have.  For example Object A could be detail to Object B and Object C making it a junction object. I cant make it detail to Object D now.
  • When it comes to how many detail objects a master can have, as far as I know there is no limit, I have tested till 12 details under same master object. Do try it in your org...
  • Standard objects can't be on the detail side of a M-D relationship ie I cant even create Master detail relationship between two standard objects as that would mean one of them to be on detail side.
Let me know if you have any scenarios where you'd want to create master detail between two standard objects.
Currently there is an idea pending for enabling M-D between standard objects but not sure if it would be allowed as it would impact underlying schema,security,permissions and what not related to  objects.

Link to Idea : https://success.salesforce.com/ideaView?id=08730000000HBN2
  • You can have up to three custom detail levels ie for example  
    • Castle 
      •   Property  (Level 1)
        •   Supply (Level 2)
          •    Treasure (Level 3)
I cant create another relationship pointing to Treasure as it would void above limit.
  • We cant create master-detail relationship if the object already contains data. There are two ways to do it: 
    • We can either create a lookup field and then fill up all the records with a proper value then convert it into lookup.
    • Else we can go ahead and hard delete all the records , soft delete doesn't work as records stay in recycle bin for 15 days.
  • In a multi level relationship we cant create a roll-up field directly on grand child object ie in below scenario.
    • Castle - Parent 
      •   Property - Child 
        •   Supply - Grand child 
I cant create rollup field on Castle object to summarize data from Supply(Grand child) directly, rather I have to first create a roll on Property then create a roll up on Castle using that field on Property.
  • We cant delete an object which is master to any object.But we can delete an object which is on detail side of relationship. Here is a catch though if you at some point of time undelete the object relationship would be converted to lookup.
  • Now lets talk about deleting records in M-D relationship I have added few scenarios below: 
    • If I delete a detail record, master record stays intact and if i undelete detail record it will be linked back to master record.
    • If I delete master record all detail records will be deleted (ie including grand child records) and if I undelete the master record all the child records will also be undeleted and restored with old connections.
    • However if I delete a detail record first then delete a master record. Now if I undelete master record it will be restored back but without detail record and there is no way restore detail record now.
  • We can create cross object workflow in case of master detail relationship ie I can create a workflow on child object to update a field on parent object which is not allowed in case of lookup relationship.
  • You cant create master detail relation between custom object and User,Lead,Product,Pricebook etc you can although create lookup.
FYI : Even cascade delete option which we can get enabled from Salesforce doesn't work on object like User,Lead, Product, Pricebook.
  • A little about Junction objects (objects used to create M:M relationship) : 
    • If any of the master records are deleted junction object record will also be removed, if master record is undeleted junction object record is also restored back.
    • Deleting a junction object record doesn't delete master records.
    • First master detail created on Junction object is considered as primary relationship and if primary relationship is converted into lookup or deleted, other M-D relationship becomes primary.
    • Junction object inherits look and feel from primary relationship moreover junction object inherits value of owner from primary relationship.
    • OWD of junction object is most restrictive of both parents ie if Parent  1 is (Public Read only) and Parent2(Private) OWD of junction object will be private.
    • User must have at-least read access to both the parent records to be able to access junction object records.
    • Junction objects cant have any details objects.
Note : Above points are written assuming junction object is created with 2 M-D relationship.


Thanks for reading...

If you liked the post do support me by buying a pizza for me :D

Sunday, May 17, 2020

Insert custom parent and child record using single DML

Sometime back I came across a scenario where i wanted to insert both parent and child object records together.

There could be two ways to do it :

  • Insert parent record then insert child record using already insert parent record ID. Something similar to below. 
I noticed with this approach we will be using multiple DML statements and If I wanted to insert parent and child in bulk that would be whole another level of effort as I'd have to maintain relationship details.

  • Another way would be to create a reference of parent record and use it to set foreign key reference on child record, once done insert both parent and child in single DML.

Refer to below link for more information.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_foreign_keys.htm

I have created code snippet to do same for custom objects, make sure you read through comments to learn about general errors that might come.


Note above method will not work if you try to reference another record of same object type for example if you try to create both ParentAccount and Child Account in same transaction and try to link them using ParentId field, It will not work. In Self lookup scenarios you can only reference records which are already existing else divide them in different DML's


Thanks for reading...


If you liked the post do support me by buying a pizza for me :D