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
Kathryn BullockKathryn 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.');
    }
}

 
Best Answer chosen by Kathryn Bullock
v varaprasadv varaprasad
trigger leadDuplicatePreventer on opportunity(before insert, before update) {
   set<string> settgs = new set<string>();
   list<opportunity> opps = [select id,Trigger_Help__c  from opportunity WHERE CreatedDate = LAST_N_DAYS:90];
   for(opportunity opp : opps){
     if(opp.Trigger_Help__c != ){
	 settgs.add(opp.Trigger_Help__c);
	 }
   }
   
   for(opportunity op : trigger.new){
      if(settgs.contains(op.Trigger_Help__c)){
	     op.adderror('Trigger_Help__c already exists');
	  }
   
   }


   
}

 

All Answers

Kathryn BullockKathryn Bullock
I forgot to mention, I also need to add a query of only Opps created in the last 90 days and an exclusion of the system admin in this trigger.
Rajesh3699Rajesh3699
Hello Kathryn Bullock,

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.
v varaprasadv varaprasad
Hi Kathryn Bullock,

Please check once following snippet code:
 
trigger leadDuplicatePreventer on opportunity(before insert, before update) {
   set<string> settgs = new set<string>();
   list<opportunity> opps = [select id,Trigger_Help__c  from opportunity];
   for(opportunity opp : opps){
     if(opp.Trigger_Help__c != ){
	 settgs.add(opp.Trigger_Help__c);
	 }
   }
   
   for(opportunity op : trigger.new){
      if(settgs.contains(op.Trigger_Help__c)){
	     op.adderror('Trigger_Help__c already exists');
	  }
   
   }


   
}

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

 
v varaprasadv varaprasad
trigger leadDuplicatePreventer on opportunity(before insert, before update) {
   set<string> settgs = new set<string>();
   list<opportunity> opps = [select id,Trigger_Help__c  from opportunity WHERE CreatedDate = LAST_N_DAYS:90];
   for(opportunity opp : opps){
     if(opp.Trigger_Help__c != ){
	 settgs.add(opp.Trigger_Help__c);
	 }
   }
   
   for(opportunity op : trigger.new){
      if(settgs.contains(op.Trigger_Help__c)){
	     op.adderror('Trigger_Help__c already exists');
	  }
   
   }


   
}

 
This was selected as the best answer
Gustavo BertolinoGustavo Bertolino

It's not clear what you want to be unique, the sObject or the field. Could you clarify your requirement?

Thanks!

Kathryn BullockKathryn Bullock
I would like the Trigger_Help__c to be unique on this trigger.