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
Donna Whitig 34Donna Whitig 34 

Comparing new item being added to existing item in SObject

I'm new to coding and trying to write a test trigger in my dev org.  The trigger needs to fire before insert and compare the name of the course being inserted inot my custom object against the existing courses.  If there is a duplicate course being inserted, throw an error.  Here is what I have so far that is not working.  It gives an error that the comparison if statement of "Variable does not exist: Course_Name__c".  Can someone point me in a direction?

=======TRIGGER CODE+++++

trigger testtr1 on Course__c (before insert) {

Public static void CheckDuplicates(List<Course__c> newCourse){             

      //Create a LIST of all existing courses
    List<Coures__c> ExistingCourses= [SELECT Id, Course_Name__c
                            FROM Course__c];

    
    // Create a SET of all incoming  course names to evaluate
  Set<String> Courses = new Set<String>();
    for (Courses__c s : Trigger.new) {
      Courses.add(s.Course_Name__c);
    } /*end for loop */
    
    //Compare SET values to LIST

      for(Courses__c s : Trigger.new)
            if(
                ExistingCourses.Course_Name__c = s.Course_Name__c)
                s.addError('DUPLICATE COURSE NAME');
  

}  /* end ChecDuplicates method */
}   /*end triggger begin */

 
Best Answer chosen by Donna Whitig 34
Dushyant SonwarDushyant Sonwar
Hi Donna,

Below trigger will check for duplicate course Name and give an error for duplicate courses.
I have added comments to understand how the below trigger is working.
/*
Name : CourseTrigger
Description : Check duplicates for the Course Name
Date :  08-04-2018
*/
trigger CourseTrigger on Course__c (before insert) {
	
	if(trigger.isInsert && trigger.isBefore){
		Set<String> setOfCourseNames = new Set<String>();
		for(Course__c courseObj : Trigger.new){
			if(courseObj.Course_Name__c != null){ // Check if course name is not blank
				setOfCourseNames.add(courseObj.Course_Name__c); // Adding Course name in Set
			}
			if(setOfCourseNames.contains(courseObj.Course_Name__c)){ //Check if user is trying to insert duplicate list of course names using csv.
				courseObj.addError('Duplicate Course name'); // Giving Error that will break the execution if found duplicates
			}
		}
		if(setOfCourseNames.size() > 0){
			Map<String , Course__c> mapOfCourse = new Map<String , Course__c>();
			for(Course__c courseObj : [Select id, Course_name__c from Course__c where Course_name__c in :setOfCourseNames]){
				mapOfCourse.put(courseObj.Course_name__c , courseObj); // filling existing course names which are in the database
			}
			for(Course__c courseObj : Trigger.new){
				if(mapOfCourse.contains(courseObj.Course_Name__c)){ //checking name with database courses with courses that user is trying to insert
					courseObj.addError('Duplicate Course name'); // Giving Error that will break the execution if found duplicates
				}
			}
		}
	}
}
Hope this helps.
 

All Answers

Dushyant SonwarDushyant Sonwar
Hi Donna,

Below trigger will check for duplicate course Name and give an error for duplicate courses.
I have added comments to understand how the below trigger is working.
/*
Name : CourseTrigger
Description : Check duplicates for the Course Name
Date :  08-04-2018
*/
trigger CourseTrigger on Course__c (before insert) {
	
	if(trigger.isInsert && trigger.isBefore){
		Set<String> setOfCourseNames = new Set<String>();
		for(Course__c courseObj : Trigger.new){
			if(courseObj.Course_Name__c != null){ // Check if course name is not blank
				setOfCourseNames.add(courseObj.Course_Name__c); // Adding Course name in Set
			}
			if(setOfCourseNames.contains(courseObj.Course_Name__c)){ //Check if user is trying to insert duplicate list of course names using csv.
				courseObj.addError('Duplicate Course name'); // Giving Error that will break the execution if found duplicates
			}
		}
		if(setOfCourseNames.size() > 0){
			Map<String , Course__c> mapOfCourse = new Map<String , Course__c>();
			for(Course__c courseObj : [Select id, Course_name__c from Course__c where Course_name__c in :setOfCourseNames]){
				mapOfCourse.put(courseObj.Course_name__c , courseObj); // filling existing course names which are in the database
			}
			for(Course__c courseObj : Trigger.new){
				if(mapOfCourse.contains(courseObj.Course_Name__c)){ //checking name with database courses with courses that user is trying to insert
					courseObj.addError('Duplicate Course name'); // Giving Error that will break the execution if found duplicates
				}
			}
		}
	}
}
Hope this helps.
 
This was selected as the best answer
Donna Whitig 34Donna Whitig 34
Thanks Dushyant!  This is very helpful.  

Let me see if I am following...  Lines 9-15 are looking for duplicates within the same list that is being inserted and lines 18 through 25 are looking for duplicates between the list being inserted and the existing names.  Is that true?  
Dushyant SonwarDushyant Sonwar
Yes Donna , line 9-15 checking in the same list and line 18-25 is checking in the database for the same courses.
Donna Whitig 34Donna Whitig 34
Thanks!
Donna Whitig 34Donna Whitig 34
When I try to compile my code I get an error on the line below: if(mapOfCourse.contains(courseObj.Course_Name__c) The error is: Method does not exist or incorrect signature: void contains(String) from the type Map Any thoughts?
Dushyant SonwarDushyant Sonwar
Donna, 

Apologies,
There is a typo error.
Please change the following line
if(mapOfCourse.contains(courseObj.Course_Name__c)){
to
if(mapOfCourse.containsKey(courseObj.Course_Name__c)){

 
Donna Whitig 34Donna Whitig 34
No worries! Thanks for all the help.
Donna Whitig 34Donna Whitig 34
Got code working without compile error however, it thinks that any item inserted is s duplicate item. Could it be that the item is first being added to the set and then compared to the same set thus throwing the error?
Dushyant SonwarDushyant Sonwar
Yes you are right. We need to first check for error in set and if it did not found , then we need to add in the set.
Please do the following changes  and it will work fine.

Please Change below code
if(courseObj.Course_Name__c != null){ // Check if course name is not blank
				setOfCourseNames.add(courseObj.Course_Name__c); // Adding Course name in Set
			}
			if(setOfCourseNames.contains(courseObj.Course_Name__c)){ //Check if user is trying to insert duplicate list of course names using csv.
				courseObj.addError('Duplicate Course name'); // Giving Error that will break the execution if found duplicates
			}
to
if(setOfCourseNames.contains(courseObj.Course_Name__c)){ //Check if user is trying to insert duplicate list of course names using csv.
				courseObj.addError('Duplicate Course name'); // Giving Error that will break the execution if found duplicates
			}
if(courseObj.Course_Name__c != null){ // Check if course name is not blank
				setOfCourseNames.add(courseObj.Course_Name__c); // Adding Course name in Set
			}


This should work fine.

Thanks,
Dushyant