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
Nidhi Sharma 17Nidhi Sharma 17 

One to One relationship between objects (a standard and a custom)

The one solution which worked for me was..

"Create a lookup field on the child
Create a unique field on the child, and hide this field from all page-layouts
Write a workflow, for any change of the lookup field, to copy that value from the lookup field into the unique field

This process has many overheads:
Extra field is required
Unique criteria is utilized (we only get 3 unique fields per object)
Workflow is used"


The only problem is the error message that gets displayed whenever the rule gets broken is out of line because it still refers to the hidden field which seems absurd.

Is there any way to change the pre-defined error message??

PS: I am looking for a one to one relationship between standard object (Users) and a custom object.

Best Answer chosen by Nidhi Sharma 17
Himanshu ParasharHimanshu Parashar
Hi Nidhi,

You can write a trigger in that case on your custom object.
 
Trigger Triggername on Custom__c (Before update)
{
    set<id> userids = new set<id>();
    set<id> existuserids = new set<id>();
    for(Custom__c obj : Trigger.new)
       userids.add(obj.user__c); //custom user lookup field name.
    
    for(Custom__c obj : [select user__c from custom__c where user__c in : userids])
       existuserids.add(obj.user__c);

    for(Custom__c obj : Trigger.new)
    {
       if(useids.contains(obj.user__c)
       {
            obj.adderror('This User is already exist with another record');
        }
   }

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 

All Answers

Himanshu ParasharHimanshu Parashar
Hi Nidhi,

You can write a trigger in that case on your custom object.
 
Trigger Triggername on Custom__c (Before update)
{
    set<id> userids = new set<id>();
    set<id> existuserids = new set<id>();
    for(Custom__c obj : Trigger.new)
       userids.add(obj.user__c); //custom user lookup field name.
    
    for(Custom__c obj : [select user__c from custom__c where user__c in : userids])
       existuserids.add(obj.user__c);

    for(Custom__c obj : Trigger.new)
    {
       if(useids.contains(obj.user__c)
       {
            obj.adderror('This User is already exist with another record');
        }
   }

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 
This was selected as the best answer
ShotShot

Himanshu Parashar, i guess you wanted to write this (existUserIds instead of userIds):
for(Custom__c obj : Trigger.new)
    {
        if(existuserids.contains(obj.user__c)
        {
            obj.adderror('This User is already exist with another record');
        }
    }

 
Nidhi Sharma 17Nidhi Sharma 17

Thank you Bogdan for the correction in Himanshu's code.
Thank you Himanshu. It worked!

Himanshu ParasharHimanshu Parashar
Thanks Bogdan for the correction :)

I am glad it worked Nidhi. :)


Thanks,
Himanshu
Nidhi Sharma 17Nidhi Sharma 17

Hi,

Is there a way that I can set focus on user__c when the trigger initiates?

I tried doing it using focus() but seems it does'nt work.