+ Start a Discussion
Devendra SawantDevendra Sawant 

Avoid Duplicates in Master-Detail Relationship

 

Hi,

 

I have two object School and Student.
They are having master detail relationship where School(Master) --> Student(Child)

Each school must contain student name with unique values.

For e.g. School Name : DOn Bosco
        Student Name: Ajay
        Student Name : Kunal
       
    It should not allow another record with student name as 'Ajay', because it is already available under DOn Bosco.

But student Name "Ajay" can allow to add under different school name (say under St. Xaviers), simply because "Ajay" belongs to different school.

How to resolve this problem?

Cheers,
Devendra S    

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

This trigger avoids Insertion of Account with duplicate name.

You have to develop something similar.

You would have to avoid combination of  Student and School Id's or Names respectively depending upon the requirement.

 

trigger AvoidDuplicateAccounts on Account (before insert, before update) 
{
    Set<String> setAccountName = new Set<String>(); 
    for(Account objAccount: [Select Name from Account])
        setAccountName.add(objAccount.Name);
    for(Account objAccount: Trigger.new)
    {
        if(!setAccountName.contains(objAccount.Name))
        {
            setAccountName.add(objAccount.Name);
        }
        else
        {
            objAccount.Name.addError('Account with same name Exists');
        }
    }
}

 Hope it helps :)

All Answers

Rahul SharmaRahul Sharma

I beleive you are using a vf page here to add multiple students to a School.

this can be handled in your save method, where you are inserting the Students record.

Before inserting a record, just check whether it exists in database or not, if not then create it, else display an appropriate page message

 

and the other way of avoiding it is  using triggers,

Devendra SawantDevendra Sawant
Hi, Can you please provide the code to do the same? It would be helpful. Thanks and Regards, Devendra S
Rahul SharmaRahul Sharma

I'm still in confusion about - you want to avoid it using trigger or you want to handle in vf page.

Devendra SawantDevendra Sawant
I want to use a Trigger for it.
Rahul SharmaRahul Sharma

This trigger avoids Insertion of Account with duplicate name.

You have to develop something similar.

You would have to avoid combination of  Student and School Id's or Names respectively depending upon the requirement.

 

trigger AvoidDuplicateAccounts on Account (before insert, before update) 
{
    Set<String> setAccountName = new Set<String>(); 
    for(Account objAccount: [Select Name from Account])
        setAccountName.add(objAccount.Name);
    for(Account objAccount: Trigger.new)
    {
        if(!setAccountName.contains(objAccount.Name))
        {
            setAccountName.add(objAccount.Name);
        }
        else
        {
            objAccount.Name.addError('Account with same name Exists');
        }
    }
}

 Hope it helps :)

This was selected as the best answer
Kanagaraj ArjunanKanagaraj Arjunan

hi i am solve this problem using trigger.  my master object is StudentName__c and my child object is CollegeName__C.  When the user select the one student name morethan once  from CllogeName__C Object  it will throws an error.

 

Trigger Deduplicate on CollegeName__c (before insert,before update)
{
list<CollegeName__c>list1=new list<CollegeName__c>();
set<id>set1=new set<id>();
for(CollegeName__c c:trigger.new)
{
list1.add(c);
set1.add(c.Student_Name__c);
}


List<CollegeName__c> cc = [select Student_Name__c from CollegeName__c where Student_Name__c=:set1];
for(CollegeName__c c1:trigger.new)
{
if(cc.size()>0)
{
c1.addError('already exsits');
}
}