You need to sign in to do that
Don't have an account?
Donna 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 */
=======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 */
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. Hope this helps.
All Answers
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. Hope this helps.
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?
Apologies,
There is a typo error.
Please change the following line to
Please do the following changes and it will work fine.
Please Change below code to
This should work fine.
Thanks,
Dushyant