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
Chirag MehtaChirag Mehta 

Duplicate Contact Check - Before Insert Trigger

For checking of contact duplicates before inserting them is fine ,its works fine as i had written a before insert trigger for the same.

But i need something extra in the before insert trigger ........

1. To display the Account Name of already existing Contact Name in  SFDC (Done--Achieved)


2. A message or alert with yes or no button saying tht 'would u like to proceed with creating of this contact ,though its duplicate exists' and if he clicks yes then the contact shld be created..overriding the before insert trigger written by me ... (Help Needed on this)


Message Edited by Chirag Mehta on 05-07-2007 01:46 AM

SaurabhRawaneSaurabhRawane
You want to bypass a trigger for some records, even if you find any duplicates records,
You can achive this, by stating the user a message that if you still want this record to be inserted then select one Custom created Field as true ...then on the Save Button your trigger will not be called based on the status of this ...check box.....


If you find anyother way..then post it...!!
Chirag MehtaChirag Mehta
Good,

But lets try to make something automate so that user needs to perform only one action instead of two
1. select check box
2. save



MuMu
hello chirag,

i like this. it is important to me too. i wonder how do i do this.
what code should i write and where?

thanks.
Chirag MehtaChirag Mehta
But wht do u want ...
MuMu
the triggers thing.
i need to catch duplicates on lead and account entry.
thanks chirag.
Chirag MehtaChirag Mehta
trigger leadEmailCheck on Lead(before insert) {
if (Trigger.new.Email != NULL)
{
Lead[] leads = [select id from lead where email = :Trigger.new.Email];
if (leads.size() > 0) {
Trigger.new.Email.addError('Email already exists');
}

}
}


MuMu

so kind of you Chirag. Let me try it!

thank you very much.

smikesmike

Hi Chirag,

 

Is this trigger valid for Contact checking or only for leads?. I require a trigger to prevent duplicate contacts and account entry. Can you help me.

Thanks

programmer4hireprogrammer4hire
I have tried this, but I keep getting the following error:

Compile Error: Initial term of field expression must be an SObject: ARRAY:SOBJECT:Lead


I just copied and pasted what was posted,  although I had to add 'bulk' after  Lead on the first line.  Can anyone help?  Thanks.
programmer4hireprogrammer4hire
I should have specified that it refers to the Trigger.new.Email in Lead[] leads = [ select id from Lead WHERE email = :Trigger.new.Email ];

Thanks.
MuMu
Hello programmer4hire,
 
I have exactly the same problem.
 
My other probblem is that I need to catch duplicates on lead entery in my Unlimited Edition which has no triggers!
MuMu
Sorry programmer4hire,
 
I did not get you. What should the code be?
 
Thanks
Chirag MehtaChirag Mehta
Didnt got wht exactly is ur first prblm.

Abt Second prblm ,i too cant help u in that ...

MuMu
Hello Chirag,
 
The following code:
 
trigger leadEmailCheck on Lead bulk (before insert) {
    if (Trigger.new.Email != NULL)  {
        Lead[] leads = [ select id from Lead WHERE email = :Trigger.new.Email];
        if (leads.size() > 0) {
            Trigger.new.Email.addError('Email already exists');
        }
    }
}
 
Gives the following error:
 
Error: Compile Error: Initial term of field expression must be an SObject: ARRAY:SOBJECT:Lead at line 3 column 61
 
Did I miss something?
 
Thanks.
 
<SCRIPT type=text/javascript>function initSelectionInEditor() { setSelectionInEditor('Body', 153, 153) }setContentWindow(window);initSelectionInEditor();</SCRIPT>
Chirag MehtaChirag Mehta
1.The code i gave was for Single Row Trigger which doesnt uses Bulk keyword.
But if we dont add bulk keyword ,then we get the error ...

Error:
Compile Error: Single row trigger not allowed, use the bulk keyword at line 1 column 1

I too dont know why salesforce stop support of single row trigger which were working fine few days back



2. If u use bulk keyword , no longer the new is single variable ..
IT becomes a array variable ...though it is single column array ...

Something like new [i] ..so u need to use Trigger.new[0].Email


So now the code changes to
trigger leadEmailCheck on Lead bulk (before insert) {
   if (Trigger.new[0].Email != NULL)  {
       Lead[] leads = [ select id from Lead WHERE email = :Trigger.new[0].Email];
       if (leads.size() > 0) {
           Trigger.new[0].Email.addError('Email already exists');
       }
   }
}
MuMu

Hello Jirag,

It works! Thanks a lot.

I wonder how i can do this in Unlimited Edition. It has no triggers :(

Chirag MehtaChirag Mehta
Triggers are just available for Developer Edition

On Unlimited Edition or Sandbox ...still they arent supported...

Hope to see Saleforce provides the same @ earliest. I think thy re testing the triggers stuff and once it is robust and bug free it will be available on all editions of Salesforce
programmer4hireprogrammer4hire
Ok, it works now, that was the problem (it no longer being a single variable by adding bulk keyword).  Thanks Chirag!
pgreggpgregg
I get the same error as programmer4hire:

Error: Compile Error: Initial term of field expression must be an SObject: ARRAY:SOBJECT:Lead at line 9 column 73

this is using the *exact* cut and paste sample trigger from the ApexWiki at:
http://wiki.apexdevnet.com/index.php/Apex_Code:_The_World%E2%80%99s_First_On-Demand_Programming_Language#Apex_Code_Example

when I try to add it as a New Trigger in the Lead "buttons and links".

Chirag MehtaChirag Mehta
pgreggpgregg
Ah, Sorry about that - this was as a result of a bug (I believe) in this forum software.  When in "page 1" of this thread, in Firefox at least, I do not see a "next page" for the rest of the posts.   Because I searched for the specific error and came directly to the thread I had no idea there were any more posts in the thread.

It is only when I go to the forum index that I can see the 1 2 3 to jump to the later threads.

PG
Chirag MehtaChirag Mehta
I too faced the same probs and came up with own browser tactics..

clcik on show printer friendly and u can read all the post in one page ...
yagnayagna
i think the above code will throw a querylist exception or something like that.

instead of directly using the select statement its better you use a FOR statement

for(Account acc : [select id, name from Account])
{

}
swamy PRNswamy PRN

Wonder full logic sir/mam