• Hannes Rettig
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hallo,
I'm absolutely new with APEX Code, so this is in my opinion just a simple question:
I want to validate a date field over a few objects. So I wrote a trigger and wanted to test
it, here's the code:
 
trigger TimeValidation on Time__c (before insert) {
    
Set<String> TNames = new Set<String>();

for (Time__c tt : Trigger.New){

Time__c.add(tt.Date__c);
}
    
List<Time__c> lstTT = [select id from Time__c where Date__c = tt.Date__c];
    
if (lstTT.size() > 0){
Trigger.New[0].Name.addError('Duplicate Error!!');
}
}
What I want to do is: When sombedy makes a new entry on Time__c I want to validate if there is still an entry with the same date es currently entered. When there's an entry found, I want have an error and reject the entry.

So as it is one of my first triggers I have the questions:

- In the line 'for (Time__c tt : Trigger.New){' I got the error 'unknown variable'. Does the IDE not know, that Time__c is a custom object or do I have to declare it (in another way)?
- What does this code:
Set<String> TNames = new Set<String>();
... and: Do I need it here?
- Is there still something missing/incorrect?

Thank you for your advice!