0

How to get the Set of Object Fields where the Contact Field Value is unique?

Here is my Apex Controller Code

for(Members__c mgml : MotherGroupMemberList)
    {
    Student__c ss = new Student__c();
        ss.Contact__c = mgml.Contact__c;
        ss.Session__c = XXXXXXXX;
        ss.Fee__c = 1000;
    studentsInMother.add(ss);
   }

In the above code, i am getting all the Objects which are having the Same Contacts. But I don't want to get the Contacts having duplicate. What should I do now to prevent those Contacts duplicates and Store them in a Set?

1

In order to prevent duplication, you can use a map where the key is the contact and the value is the student. In this case, you'll get only one student per each contact. Please see code below:

//Map to be used for saving unique students.
Map <string, Student__c> map_uniqueStudents = new Map <string, Student__c>();

for(Members__c mgml : MotherGroupMemberList)
    {
    Student__c ss = new Student__c();
        ss.Contact__c = mgml.Contact__c;
        ss.Session__c = XXXXXXXX;
        ss.Fee__c = 1000;

    //We don't need this line anymore.
    //studentsInMother.add(ss);

    //Add the student to the map.
    map_uniqueStudents.put(ss.Contact__c, ss);
   }

You can use "map_uniqueStudents.values()" to get the list of unique students.


In regards to your comment, I'm not sure i understand your exact problem.
If "studentsInMother" from your code is a set and the value for the fields "Session__c" and "Fee__c" are the same for every student, you will not get duplicate students in that set.

If "studentsInMother" is not a set or if the fields "Session__c" and "Fee__c" are not the same for every student, you can try the following:

//set for saving unique contacts.
set <string> set_uniqueContacts = new set <string>();

for(Members__c mgml : MotherGroupMemberList)
    {
     //Check if you don't have this contact.
     if(!set_uniqueContacts.contains(mgml.Contact__c))
     {
        Student__c ss = new Student__c();
        ss.Contact__c = mgml.Contact__c;
        ss.Session__c = XXXXXXXX;
        ss.Fee__c = 1000;

        studentsInMother.add(ss);

        //Add the contact to the set.
        set_uniqueContacts.add(mgml.Contact__c);
     }
   }
  • Thanks for your reply, It might be helpful to get the contacts seperately as i appreciate it. But here my problem is different that i am getting all those MotherGroupMemberList from the Visualforce Page. Whenever i add a row, the particular Member--> Contacts will show in another section of vf page. So, If i add the same group again, it must not show the same students again. – Subhash Apr 7 '16 at 9:21
  • I'm not sure i understand your exact problem. I edited my answer and added an additional solution option. If it doesn't solve your problem, please try to elaborate, maybe with additional code. – Dagish Apr 7 '16 at 11:40
  • yeah, "Session__c" and "Fee__c" are the same for every student. But I don't want the duplicate students in that set – Subhash Apr 7 '16 at 12:01
  • 1. If you add student to the set that is identical to existing student in the set, the new student will run over the existing student and you won't have duplicates in the set. 2. By using the second option in the answer i wrote above (the one with the "set_uniqueContacts"), you won't create duplicate students at all because the if at the beginning of the for loop will prevent such cases. – Dagish Apr 7 '16 at 13:39
0

You can create a map with key as of type Contact__c and value as Student__c.

I assume Contact__c is a reference to Contact object.So the type of Contact__c is an Id.


Map<Id,Student__c> contactStudentMap = new Map<Id,Student__c>();
for(Members__c mgml : MotherGroupMemberList)
{
  if(!contactStudentMap.containsKey(mgml.Contact__c)) // If map does not contain key then enter.
   {
      Student__c ss = new Student__c();
      ss.Contact__c = mgml.Contact__c;
      ss.Session__c = XXXXXXXX;
      ss.Fee__c = 1000;
      contactStudentMap.put(mgml.Contact__c,Student__c );
   }
}
List<Student__c> stdList = contactStudentMap.values(); // In case you need a list as result
Set<Student__C>  stdSet = new Set<Student__C>(contactStudentMap.values()); // If you need a set as result

But this would work only for one field. If you want a unique object, then try creating a wrapper class and implement Hashcode and equals.

-1

Just to confirm.

The list of Members__c could have duplicated Contacts and you want to insert only one Student__c per Contact__c.

If this is your problem and assuming Contact__c field is Text Type, you can try this:

Set<String> contactDedup = new Set<String>();
for(Members__c mgml : MotherGroupMemberList){
    if (!contactDedup.contains(mgml.Contact__c)){
        Student__c ss = new Student__c();
        ss.Contact__c = mgml.Contact__c;
        ss.Session__c = XXXXXXXX;
        ss.Fee__c = 1000;
        studentsInMother.add(ss);
        contactDedup.add(mgml.Contact__c); 
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.