Osama's Weblog

The place where you get updated!
  • rss
  • Home
  • Who am I?
  • RSS Feeds

Enforcing 1:1 relationship in Salesforce.com

Osama | June 3, 2010

I was wondering what if I have to create a one to one relationship between two objects. There is no declarative way where we can specify a one to one relationship between two objects.

We can write a trigger to enforce a one to one relationship between two objects. The trigger can be written on the child object that validates if there is any other record with same Parent record ID. If found then instead of inserting the record, it gives an alert.

The trigger will generate a list of child objects with specified parent ID. If the list’s length that is retrieved is greater than one, it will generate an alert.

List<ChildObject> co = [select name from ChildObject where parentId = ParentId]
If  (co.Size() < 2)
{
//insert
}
else
//generate alert

  • Share/Bookmark
Categories
APEX, salesforce
Tags
APEX, salesforce, trigger
Comments rss
Comments rss
Trackback
Trackback

« How to: Retrieve a list of sObjects in Apex Visualforce enhancements »

3 Responses to “Enforcing 1:1 relationship in Salesforce.com”

  1. marco says:
    July 6, 2010 at 2:38 pm

    you can create a 1 to 1 relationship without code with a lookup.
    you need a text field and tick the uniqueness checkbox.
    Then a workflow will update this field by the id of the main object.
    Du to uniqueness of it you will get an error message if the ‘main’ object is already linked with another :-)

  2. Osama says:
    July 6, 2010 at 8:15 pm

    That’s the way that SF has told. I was trying to think something out of the box. cheers!

  3. Marc says:
    July 30, 2010 at 8:01 pm

    If you have a master-detail relationship you can also do a roll-up (COUNT) over the child on the master object and a validation rule that disallows this count to exceed 1.

Leave a Reply

Click here to cancel reply.

My status

Categories

  • .NET
  • APEX
  • consulting
  • General
  • Oracle CRM on Demand
  • salesforce
  • Uncategorized
  • VisualForce

MM Did You Know?

The number of possible ways of playing the first four moves per side in a game of chess is 318,979,564,000.
Plugin by mmilan

Its all about cloud

Salesforce

.NET .NET 4.0 beta actionFunction actionSupport administrator android APEX Apex variable API button certification cloud computing consulting controller custom DoDirectPayment force.com GET goggles google html images input inputField JavaScript jquery master-detail matcher Parallel Programming parameters pattern paypal query string REGEX regular expression salesforce search url variable Visual Force VisualForce Visual Studio vmware web service XML PARSING DOM SAX STAX

WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

My Tweets

Error: Please make sure the Twitter account is public.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox