You need to sign in to do that
Don't have an account?

'Variable does not exist' for a custom object
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:
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:
- Is there still something missing/incorrect?
Thank you for your advice!
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!
Check out this solution, you should be able to adapt it. Same idea here, just using email to check dupes
http://www.sfdc99.com/2013/10/19/example-how-to-write-a-deduping-trigger-for-leads-and-contacts/
If you are really new, check out the entire site, it will really help you