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
krishna chaitanya 35krishna chaitanya 35 

junction object trigger

HI All,
I have a junction object called locationcontact which is related to location (masterdetail) object and contact(masterdetail).Below the location a related list is placed to enter a location contact .I need to restrict if the user tries to assign a same contact to location (means avoid duplication of same contact to location).I am writing a trigger to solve this ,but unable to fetch rows.

trigger duplocationcontact on location_Contact__c (before insert) {
    
    for (location_Contact__c ci : trigger.new) {
            
        List<location_Contact__c> resultList = [SELECT id FROM location_Contact__c WHERE  location__c = :ci.location__c    AND Contact__c = :ci.Contact__c.name];
                 
        if (!resultList.isEmpty()) {
              
        ci.addError('Duplicate record, a location contact already exists');
        }
        }
}
Best Answer chosen by krishna chaitanya 35
venkat-Dvenkat-D
The easier way for this to do is 
1) Create a text field which should be unique
2) Create a workflow or process builder and populate ContactId_LocationId

If a duplicate record is entered in system this field will throw error. This wasy you can avoid trigger and test class as well. 

All Answers

venkat-Dvenkat-D
The easier way for this to do is 
1) Create a text field which should be unique
2) Create a workflow or process builder and populate ContactId_LocationId

If a duplicate record is entered in system this field will throw error. This wasy you can avoid trigger and test class as well. 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Hi krishna chaitanya 35,

You dnt need to create trigger for same. You can do the same with above step as venky409 mention you.
1) Create a text field which should be unique
2) Create a workflow or process builder with field update and populate Contact__c+location__c on it.

But if you want to do same by trigger only then your trigger should be like below
trigger duplocationcontact on location_Contact__c (before insert) 
{
	Set<Id> setLocationId = new set<Id>();
	Set<Id> setContactId = new set<Id>();
	
    for (location_Contact__c ci : trigger.new) {
		
		if(ci.Contact__c != null)
		{
			setContactId.add(ci.Contact__c);
		}
		if(ci.location__c != null)
		{
			setLocationId.add(ci.location__c);
		}
	}
	
	List<location_Contact__c> lstLC =  [ SELECT id,location__c,Contact__c FROM location_Contact__c WHERE  location__c = :setLocationId  AND Contact__c = :setContactId ]
	Map<String,location_Contact__c> mapLC = new Map<String,location_Contact__c>();
	for(location_Contact__c LC : lstLC)
	{
		String key = LC.location__c+'_'+LC.Contact__c;
		mapLC.put(key,LC)
	}
	
	
    for (location_Contact__c ci : trigger.new) {
		String key = ci.location__c+'_'+ci.Contact__c;
        if ( mapLC.containsKey(key) ){
			ci.addError('Duplicate record, a location contact already exists');
        }
	}
}

Let us know if this will help you

 
krishna chaitanya 35krishna chaitanya 35
Hi Amit and @venky409,
Thanks to both of you for your suggestions.I implemented this one with workflow suggestion .In future if constraints change i will use this trigger.
Thank you,
Krishna.