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
PannarPannar 

unique oppotunity name check thru apex?

Hi,

If an opportunity is created for Account 123, opportunity name is Opportunity–123.  If another opportunity is created for Account 123, the system will return an error if the name of the opportunity is the same as one already in SFDC.

Is it possible?

RickyGRickyG
I think it would be possible with a VLOOKUP function in a validation rule.  It should definitley be possible with a trigger.
PannarPannar

Dear Ricky,

Thank you so much for responding all of queries.

It would be great, if You could share us some sample syntax code for either trigger or validation rule.. :-)

RickyGRickyG
Pannar -

The online help has good information on VLOOKUP, and the doc and the Developer Guide can walk you through simple triggers.
PannarPannar
Code:
IF(VLOOKUP(Name,$ObjectType.—.Fields.Name, Name)= Name,True,False)


 
Ricky.,

ok. I wanted to compare the Opportunity Name and check whether that is already exists in the opportunity history so that we can set the unique opportunity name right? but i couldn't find opportunity history by using Insert Field button on the validation rule section. Just need to check the opportunity name in the opportunity history and throws the error if the entered opportunity already present in the history?

 

Ron WildRon Wild
You might accomplish what you talking about with a simple trigger :


Code:
trigger CheckOppname on Opportunity (before insert, before update) {

 for (Opportunity opp :Trigger.new ) {

  Integer oppCnt = [Select Count() from Opportunity where Name = :opp.Name];
  if (oppCnt > 0) opp.addError('Duplicate opportunity name found.');
 }

}

Please see the Salesforce wiki and documentation for examples on how to get started with Apex and triggers.
 

PannarPannar

Wow! thats nice to see that trigger.

I've also written like this not sure its correct. But i am very much afraid with the error message"Too many SOQL queries, if i tried to insert more than 5000 records thru data loader! :-(

Code:
trigger tocOpportunityUniqueName on Opportunity (before insert) {
    if (Trigger.isBefore) {
        if (Trigger.isInsert)  {
         String name = Trigger.new[0].Name;
             for (Opportunity o : [select Name from Opportunity where Name=:name]) {
                    if(o != null)
                     Trigger.new[0].Name.addError('Duplicate Opportunity name found.');    
                    }
              }
    }
}


 are you sure either my code or your code doesn't throw too many SOQL queries error,if we inert very huge records thru bulk load?


 

Ron WildRon Wild
By the way ... the example code does a query for each object that invokes the trigger, which won't scale well.   You will need to adapt the code if you need to handle opportunity records in large numbers.
PannarPannar

Yes, i expected this answer from You. :-)

To adapt this code, we need to use List, Map variable to store the bulk of opportunity names in the array and then search the loopkup opportunity name from there? I am novice to apex trigger :-(

Thanks for your help.

 

 

SuperfellSuperfell
well, you don't care about the number of duplicates, just if there's zero, or more than zero, so you can add a limit 1 to your query to address the SOQL governer limits.

But like many triggers posted here yours has a problem with concurrent inserts, where concurrent inserts of the same opportunity name won't be spotted (because the other transaction hasn't committed yet, so isn't visible to your query), and in this particular case, you're not going to spot duplicates within the same batch.
Ron WildRon Wild
There is ample of material on this forum and in the wiki to get you started.  Just do a quick search on "Apex Governor Limits" and I'm sure you'll find plenty of answers and lots of good reading for the afternoon.

Best of luck!

 
Ron WildRon Wild
Just another thought ... you might be able to make use of a unique value field and let Salesforce do the duplicate checking for you.


PannarPannar

Yes Simon Sir, You are right. adding the statement LIMIT 1 which obviously increase the performance. :-)

If i do the batch process and transaction wouldn't able to spot the duplicate names, then what can be the solution for this? Is it limitation from Salesforce or any other approach which i need to follow.

Ricky was given me some tips on VLOOKUP function which can be used in validation rule to lookup the value against the custom object's value. but here i couldn't find any opportunity history to lookup.
 
PannarPannar

Ron,

I've seen the Apex Governor limits" from apex api document. It says that how many maximum SOQL queries can be run in the for loop. If it exceed the limit, then we would suffer from trigger exception which would make the business user to be very unhappy.

by the way, i am working on this in the midnight india time ;-)

PannarPannar


Ron Wild wrote:
Just another thought ... you might be able to make use of a unique value field and let Salesforce do the duplicate checking for you.





Yes, we can, but Opportunity Name is the standard field! how can i make it as a unique field? any idea?
 
Eager-2-LearnEager-2-Learn

I hope this post is not too old to pick up from!!!

 

With all this great information; would there be a way to interact with the user so that they can deside if they want the duplicate record to be saved or canceled.  Our business rules do allow dups for certain situations.

Himanshu ParasharHimanshu Parashar

 

You can make trigger like this which will avoid Too Many SOQL queries error.

 

trigger noduplicate on opportunity(before insert) {

   set<String> oppset=new set<String>();
   for(opportunity opp : [select name from opportunity])
   {
       oppset.add(opp.name.toUpperCase());
   }

   for(opportunity newopp : Trigger.new)
   {
   if(oppset.contains(newart.name.toUpperCase()))

  {
      newopp .addError('Opportunity Already exist.');
  }


   }

}