Skip to main content

Polymorphic Relationship & SOQL Polymorphism

Problem:-

In Event object, WhatId field can refer to 'Account', 'Opportunity', 'Campaign', custom objects etc. I want to write a SOQL query which will return different set of fields based upon which object 'WhatId' is referring to.

Solution:-

Before Winter'13 release,  you have to do extra work when making queries that involve polymorphic relationships. Since you don’t know in advance what type of object is being referenced, you might have to get the ID of a Name object instead, process this information in some logic code, and then do a second query using information from that Name object to get to the content of the referenced object.

With Winter'13, salesforce has introduced the concept of Polymorphic SOQL.

Polymorphic Relationship:- 

A polymorphic relationship is a relationship where the referenced objects can be one of several different object types. For example, the What relationship field in an Event can reference an Account, or a Campaign, or an Opportunity. The LastModifiedBy relationship field in an Event, however, can only reference a User and is not polymorphic.

With SOQL polymorphism you can provide instructions directly in the SOQL query on what to do for each possible type of object. You do this via the new TYPEOF clause. Something like below

SELECT Subject,
  TYPEOF What
    WHEN Account THEN Phone, NumberOfEmployees
    WHEN Opportunity THEN Amount, CloseDate
  END
FROM Event

You may have to contact saleforce.com to enable this feature for your organization.

Comments

  1. i really like this article please keep it up.
    Insurance

    ReplyDelete
  2. Hi,

    Do you know which objects (other than Event and Task) in Salesforce have Polymorphic relationships?

    Thanks.

    ReplyDelete

Post a Comment

Popular posts from this blog

Mashup Integration in Salesforce

During preparation for TA certification exam, I came across a word Mashup for integration a number of times. I explored about it and below is description:- Mashups, sometimes called “composites,” are hybrid applications created by bringing together several data sources and Web services to create a new application or to add value to an existing application. Behind the scenes, mashups may require different levels of integration, depending on whether the mashed-up data is only meant to be viewed, whether it can be edited, and whether data is actually transferred between systems. There are three types of mashup:- Client Presentation Mashup - In this type of mashup the integration takes place strictly at the visual level. It makes possible to view data from two or more applications in a browser,  without actually moving data between the applications. Example - Google Maps. Client Service Mashup - As mashups evolve, they are becoming more complex and sophisticated. Client...

Make Calls to PageReference.getContent() in Asynchronous Apex - Winter16

You can now make calls to the getContent() and getContentAsPdf() methods of the PageReference class from within asynchronous Apex such as Batch Apex, Schedulable and Queueable classes, and @future methods. This allows you to design much more flexible and scalable services that, for example, render Visualforce pages as PDF files.

Hierarchical Relationship

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 field. They didn't want update of Manager, but on the basis of some business use case. First impression to me was it will be straight forward to create a lookup on user and then in trigger will check for business use case and update Manager field accordingly. However, when I proceed with creating a look on User object, I couldn't find an option ' Lookup Relationship ' on first screen of 'New Custom Field'.  In fact, I saw only available option ' Hierarchical Relationship ' for creating any sort of relationship. I know most of us know about it. But you can't defer on my opinion that we tend to forget these small stuff. Since, on user you can't have any direct relationship, but only user. Salesforce gives you special relationship data t...