Friday, June 14, 2013

Sneak Peek: Salesforce Polymorphic keys

What?
Polymorphic key is a relationship field which can relate to more than one type of object as parent. Unlike Lookup and Master-detail relationships which are related to a single object, Polymorphic keys can refer to multiple objects.

Where?
As of date, Polymorphic keys cannot be created by developers. They are available for specific objects out of the box for e.g. WHOID and WHATID in Task and Event objects.

Why?
Sneak Peak: Salesforce Polymorphic Keys
As we know, Salesforce’s data model is strictly relationship based, and the type of objects at both the ends of the relationship is fixed. So, if an object is to be related with multiple objects, then a separate relationship field has to be created for each relationship. Polymorphic keys are required to maintain relationship with multiple types, for e.g. task/ event might be related with various type of records viz. Account, Contact etc. So, a polymorphic key helps in establishing unique access to such disparate relationships.

How?
Polymorphic keys are hard to handle!! As they are polymorphic the queries/ data manipulation can be tricky, as the related object can be anything, and hence the operations might or might not succeed. For e.g. functionality to update a field on Account may not work with Contact.

In order to implement such functionality, the type of the object is to be determined and hence the operations are to be applied. Further, the objects would need to be retrieved separately.

With Summer ’13 release, Polymorphic keys have become more friendly. They can be used in SOQL queries to filter out specific types, based on type specific filter criteria. Following are few ways of handling polymorphic keys in SOQL:-

SOQL TYPEOF Clause
List = [SELECT TYPEOF What WHEN Account THEN Phone WHEN Opportunity THEN Amount END FROM Event];
This would conditionally return value of field "Phone" if event is related to Account and value of field "Amount" if event is related to Opportunity


Note: TYPEOF is currently available as a Developer Preview as part of the SOQL Polymorphism feature.

Filter results by Type
List = [SELECT Description FROM Event WHERE What.Type IN ('Account', 'Opportunity')];
This would return only those events which are related with an Account or Opportunity.


For more details refer http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_relationships.htm?SearchType=Stem&Highlight=whatId#soql_polymorph_keys

8 comments:

  1. Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
    mobile application development course | mobile app development training | mobile application development training online
    "web designing classes in chennai | Web Designing courses in Chennai "
    Web Designing Training and Placement | Best Institute for Web Designing
    Web Designing and Development Course | Web Designing Training in Chennai
    mobile application development course | mobile app development training

    ReplyDelete
  2. Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
    Web Designing and Development Course | Web Designing Training in Chennai

    ReplyDelete
  3. I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
    web designer courses in chennai | best institute for web designing Classes in Chennai
    web designing courses in chennai | web designing institute in chennai | web designing training institute in chennai
    web designing training in chennai | web design and development institute

    ReplyDelete
  4. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.
    Cyber Security Projects for Final Year

    JavaScript Training in Chennai

    Project Centers in Chennai

    JavaScript Training in Chennai

    ReplyDelete

Popular Posts