+ Start a Discussion
bs881026bs881026 

In a lookup relationship

Hi,
I have a scanario where there is a lookup relationship between two objects and the requirement is to not allow more than One Child Object record for a that Master object (In a way to have just 1:1  record ) between the two objects, related with a lookup relationship.
Please help me understand, how do I achieve this.

Thanks
AnupPrakash_AlgoworksAnupPrakash_Algoworks
Hi bs881026,

   Why don't you try it with a trigger and it could be resolved. It wouldn't much of a line of code.


Please Mark as best answer if it solves your query.
SFDC GuestSFDC Guest
Hi bs881026,

Please use the below code for your requirement.
Here,
Account --> Parent Object.
Contact --> Child object


Trigger RestrictMoreThanOneContact on Contact (after insert, after update)
{
    set<id> accountIdSet = new set<id>();
    Map<id, List<Contact>> accContactMap = new Map<id, List<Contact>>();
    for(Contact con:Trigger.new)
    {
        accountIdSet.add(con.AccountId);
    }
    for(Contact con:[select Id, AccountId from Contact where AccountId =:accountIdSet])
    {
        if(accContactMap.containsKey(con.AccountId))
        {
            accContactMap.get(con.AccountId).add(con);
        }
        else
        {
            accContactMap.put(con.AccountId, new List<Contact>());
            accContactMap.get(con.AccountId).add(con);
        }
    }
    for(Contact con:Trigger.new)
    {
        if(accContactMap.containsKey(con.AccountId))
        {
            if(accContactMap.get(con.AccountId).size()>1)
            {
                con.addError('Contact is already existed for this account');
            }
        }
    }
}

Please mark it as best answer if it helps you.

Thank You,
Sohel Mohd
Trupti Dhoka 41Trupti Dhoka 41
Hello,

You can achieve by two ways.

It relation between objects is Master-Detail then try with follwing solution : 
1. On master object create a roll up summary field of child with count. Then write validation rule rollup summary field to check for >1 .. so it will give error if it has more than one record for same master detail relation values.

else you can try with below solution too if relation is lookup 
1. Create a custom field on the detail object which will hold ID of related parent object. Make the field unique.
2. Create a Workflow field update rule on detail which will Update the custom field with the value of the associated parent ID.
In this case whenever you will try to add more child records to same parent it will throw error 'Error: Duplicate value on record:'

Please mark as Best Answer if this solved your query.
Thanks,
Trupti
 
bs881026bs881026
HI Trupti,
Can you please explain more on point # 2. for Lookup relationship.

Thanks.
Trupti Dhoka 41Trupti Dhoka 41
Hi,
Conside a scenario where I have created a lookup relation between Camping Items(Parent) and Expense (Child).Then I have created one (text) field on Expense object named 'Related Campaign Id' with unique option selected at the time of creation of field.
Then I have created one workflow rule on Expense object with Field Update action as below :

User-added image

Now I have created 1 'Expense 1' record and associated it with 'Campaign!' record. Now whenever I try to assosiacte new record (Let's say 'Expense 2') with 'Campaign!' record it will throw error as below : 

User-added image

Let me know if you need more info.
Please mark as Best Answer if this solved your query.


Thanks,
Trupti