function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SKT CloudComputingSKT CloudComputing 

In how many ways we can achieve one-to -one relationship in salesforce.

In how many ways we can achieve one-to -one relationship in salesforce.

Can anyone please explain with examples.
suresh sanneboina 4suresh sanneboina 4
hi,
You can do this using 2 ways

Using Workflow and using Trigger.

Approach 1:
Create a text field in Contact object as Account Name Unique
Create a Work Flow on Contact and set the criteria contact:Account Name Is Not Null
Create a Workflow Actions as Field Update and Select the Account Name Unique Field on contact object and Set the value as AccountId on Contact and click on done.
Activate the Work flow.

Approach 2:
create a master detail relationship and than on master create a roll up summary field of child with count. 
then you write validation on rule rollup summary field to check for >1.
now it will give you error if it has more than one record for same master detail relation values..

trigger oneToOne on Opportunity (before insert, before update) {
    Set<Id> accids = new Set<Id>();
    Map<Id, Account> mapAccount = new Map<Id, Account>();
    
    for(Opportunity e : trigger.New) {
        accids.add(e.Account);
    }
    
    List<Account> listInterest = [SELECT Id, Opp_count__c FROM Account WHERE Id =: accids];
    
    for(Account intr : listInterest) {
        mapAccount.put(intr.Id, intr);
    }
    
    for(Opportunity e : trigger.New) {        
        if(mapAccount.get(e.Account).Opp_count__c == 1) {
            e.addError('Already an employee has been associated with this interest');
        }
    }
}
SKT CloudComputingSKT CloudComputing
Thanks for reply suresh.

I have few questions with respect to these Approaches.

Approach1-

There is a standard field already exist with name Account Name?
How can I set condition Account Name Is Not Null?



Approach 2:
  Is this trigger is the part of this approach?


Thanks!


 
suresh sanneboina 4suresh sanneboina 4
Approach 1:
contact:Account Name Is Not Null

Approach 2:
Yes Using trigger you can even do that.