You need to sign in to do that
Don't have an account?
Kathryn Bullock
Apex Code Trigger
I've been trying to create a trigger for a while and it is not going well, but I was tackling it wrong. I have now created a field called Trigger_Help__c on the Opportunity Object and I need it to be unique. I have found the following code and I think it needs to be something similar. Is it possible someone could help me change it to work how I need?
Trigger trigger leadDuplicatePreventer on Lead (before insert, before update) { Map<String, Lead> leadMap = new Map<String, Lead>(); for (Lead lead : System.Trigger.new) { // Make sure we don't treat an email address that // isn't changing during an update as a duplicate. if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))) { // Make sure another new lead isn't also a duplicate if (leadMap.containsKey(lead.Email)) { lead.Email.addError('Another new lead has the ' + 'same email address.'); } else { leadMap.put(lead.Email, lead); } } } // Using a single database query, find all the leads in // the database that have the same email address as any // of the leads being inserted or updated. for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]) { Lead newLead = leadMap.get(lead.Email); newLead.Email.addError('A lead with this email ' + 'address already exists.'); } }
All Answers
Can you elaborate your requirement..? Do you want opportunity name to be unique or the field Trigger_Help__c to be unique. If so mention the conditions and values for the fields to be made as unique.
Thank You,
Rajesh.
Please check once following snippet code:
I have not tested above code may you will get some syntactical errors.
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
It's not clear what you want to be unique, the sObject or the field. Could you clarify your requirement?
Thanks!