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
SF7SF7 

Issue with Trigger checking dupes

Hi i have trigger on custom object relationship__c which is having a lookup to contacts.

 

What this trigger does is it checks if an user is having a relationship with the contcat already and if it then it throws an error , it is doing it by checking the Owner field which is automatically populated by default with the created by user.

 

problem with that is no one else can create a relationship for others because dupes are checked by owner field so instead of the owner field i need to pull the id of the person in the name filed and check it . how can i do that  any ideas?

 

trigger CheckDupContactRelationShip on Relationship__c (before insert, before update) {

List<Id> contactIds = new List<Id>();
List<Id> ownerIds = new List<Id>();
List<Relationship__c> duplicateContactRels = null;
Map<String,Integer> mapOfDuplicateValues = new Map<String,Integer>();
Integer k = 0;

/*Looping through batch of new records and adding the contactIds and
OwnerIds to individual lists */

for(Relationship__c cr : Trigger.New)
{
contactIds.add(cr.Contact__c);
ownerIds.add(cr.OwnerId);
}

/*Retrieving all the records into a list where the id matches the id
of records that are being inserted or updated*/

duplicateContactRels = [Select id,Contact__c,OwnerId from Relationship__c
where Contact__c in: contactIds and OwnerId in:ownerIds ];

/*If any duplicate records exist adding those to a map to display the error message
In Trigger Insert/Update */
if(duplicateContactRels != null)
{
for(Relationship__c dcr : duplicateContactRels )
{
mapOfDuplicateValues.put(dcr.Contact__c+ ' ' +dcr.OwnerId, k);
k++;
}
}

if(Trigger.isInsert)
{
for (Integer i = 0; i < Trigger.New.size(); i++)
DisplayErrorForDup(i);
}


else if(Trigger.isUpdate)
{
for (Integer i = 0; i < Trigger.New.size(); i++)
{
if(Trigger.Old[i].Contact__c != Trigger.New[i].Contact__c || Trigger.Old[i].OwnerId != Trigger.New[i].OwnerId )
{
DisplayErrorForDup(i);
}
}
}



public void DisplayErrorForDup(Integer j)
{
if(mapOfDuplicateValues.get(Trigger.New[j].Contact__c+ ' ' +Trigger.New[j].OwnerId)!= null)
Trigger.New[j].addError(Label.ConRel_ErrMsgAlreadyExists);
}
}

 

Thanks 

Akhil

Best Answer chosen by Admin (Salesforce Developers) 
Noam.dganiNoam.dgani

Hi Akhil

 

allow me to offer a different approach.

 

1. If you want to allow users t create relationships I with contacts for someone other than themselves - you can create a new custom lookup field in relationship object, that points to a user record. This way you don't rely on the owner field what has pre built logic that doesn't suit your needs.

 

2. In any case, instead of a trigger, you can create a text field that isn't displayed in the relationship layout. Create a workflow rule on create and every update of a relationship record that checks if the contact or the user field (owner or custom)  changed Or if the record is new.

if so, the workflow will perform a field update and concatenate the contato and user ids.

final step, make that hidden text field unique.

 

now, if the combination of that contact and that user exists, you will break uniqueness and get a duplicate error, referring you to the duplicate record.

 

 

Hope this helps - if it does, kindly mark your post as resolved.

All Answers

Noam.dganiNoam.dgani

Hi Akhil

 

allow me to offer a different approach.

 

1. If you want to allow users t create relationships I with contacts for someone other than themselves - you can create a new custom lookup field in relationship object, that points to a user record. This way you don't rely on the owner field what has pre built logic that doesn't suit your needs.

 

2. In any case, instead of a trigger, you can create a text field that isn't displayed in the relationship layout. Create a workflow rule on create and every update of a relationship record that checks if the contact or the user field (owner or custom)  changed Or if the record is new.

if so, the workflow will perform a field update and concatenate the contato and user ids.

final step, make that hidden text field unique.

 

now, if the combination of that contact and that user exists, you will break uniqueness and get a duplicate error, referring you to the duplicate record.

 

 

Hope this helps - if it does, kindly mark your post as resolved.

This was selected as the best answer
SF7SF7

Hey Thanks Noam ,

 

I just tried the first solution and it worked perfectly and i did not understand the second idea .

 

Thanks

Akhil.

Noam.dganiNoam.dgani
Glad it helped.

Actually they are not two different solutions, but more complimentary solutions.
The second indicates that you don't really need a trigger to do this.
I'll try to elaborate tomorrow when in front of a computer.