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
Sivasankari MuthuSivasankari Muthu 

How to avoid duplicate data entry?

Hi,
I am seraching the students by Student Name. 
Query: select Name,Student_Name__r.External_Student_Id__c, Year__c, School_Name__r.School_Name__c, Student_Name__r.Student_First_Name__c, Student_Name__r.Student_Last_Name__c from Student_Academic_Class__c where Student_Name__r.Student_First_Name__c =: student;
Need Student Name, School name from the above query without duplicate entry.
User-added image
Student Name: Test (Link)
It showing Student data based on Class Name count. In the above example Student link TEST have three class.
How to avoid duplicate entry of Student ?.


Thanks
Sivasankari M
SandhyaSandhya (Salesforce Developers) 
Hi Sivasankari Muthu,

You have to process your list which has duplicate entries in a loop.
You cannot do it in a query as we do in SQL like DISTINCT.
SOQL doesn't have DISTINCT in the query.
Make use of set which does not allow duplicates by default.
Or You can use below code to avoid duplicates.
public static List<String> getnames(List<String> duplicates){

List<String> distinctnames = new List<String>();

for(String name: duplicates){

Boolean present = false;

for(Integer i=0; i< distinctnames.size(); i++){

if(name.equalsIgnoreCase(distinctnames[i])){ //Check for duplicate

present=true;

break;

}

}

if(!present)

distinctnames.add(name);

}

return distinctnames;

}
Hope this helps you.
 
Please accept my solution as Best Answer if my answer was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya