+ Start a Discussion
Che SFDCChe SFDC 

Help with duplication trigger

Dear all, I`m trying to create a code which will prevent duplication in my org. THis code will run if lead is being created/updated and contact is already exisiting. If contact is already exisitng, lead will give an error. what I want to do is, if creator of this lead (user) is "Internal Login", trigger should not be applied and lead should be allowed to create. When I add "If" argument**, it is not working. It is either allowing everyone to create or no one to create dupe. I need a condition where only "Internal User" should be allowed to create dupes.. Can someone please help?

 
trigger AvoidDupe on Lead (before insert, before update) {

  for (Lead myLead : Trigger.new) {
              String Fullname;

                *//adding "if " argument below is not helping. it is either allowing everyone to create or no one to create dupe. I need a condition where only "Internal User" should be allowed to create dupes.

                 if (myLead.CreatedById == '00580000003a935') {
 
if (myLead.Email != null) {
List<Contact> dupes = [SELECT Id, FirstName, LastName,Name FROM Contact WHERE Email = :myLead.Email];
if (dupes.size() > 0) { 
FullName = dupes[0].Name;
mylead.addError('ERROR: There is already an identical record with same email. <a href=\'https://cs1.salesforce.com/' + dupes[0].Id + '\'> Contact - '+ FullName  +  '</a>', false);

} }
  }
}
}

 
Best Answer chosen by Che SFDC
Vivek_PatelVivek_Patel
Hi Chetan

If I understand correctly, you need to allow duplicates for Salesforce Platform licence. navigate to following link to see what each value means in the UserType field.

https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_user.htm#profilelicensetype

Regards,
Vivek Patel.

All Answers

Vivek_PatelVivek_Patel

Hi Chetan,

You can use "UserType" field on Users and put the condition in your code for "Standard" users which are internal.

Hope this helps!

Regards,

Vivek Patel.

Please like or mark this as best answer if this answers your question to help others find better answer and improve the quality of developer forum.

Che SFDCChe SFDC
Hi Vivek,
"Internal User" is one of our standard users with Salesforce license. Only "Internal User" should be allowed to create duplcicates. Rest all users, shouldnt be allowed. Does that make sense?
Vivek_PatelVivek_Patel
Hi Chetan

If I understand correctly, you need to allow duplicates for Salesforce Platform licence. navigate to following link to see what each value means in the UserType field.

https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_user.htm#profilelicensetype

Regards,
Vivek Patel.
This was selected as the best answer
Che SFDCChe SFDC
Hi Vivek, thanks for your suggestions. I found out what the issue is. This is a before trigger so CreatedById will be null, hence wont work. I used "
UserInfo.getUserId()" to get user id. :)

Thanks for all your help! Cheers. :)