You can try below code
trigger LocationJunctionCRUD on LocationJunction__c (before insert) {
List lstOpportunity= new List();
Map<Id,Id> mapOppLoc= new Map<Id,Id>(); for(LocationJunction__c locationJunction: Trigger.new){ lstOpportunity.add(locationJunction.Opportunity__c); mapOppLoc.put(locationJunction.Opportunity__c,locationJunction.Location__c ); }
if(lstOpportunity != null && lstOpportunity.size()>0){
List<LocationJunction__c> lstlocationJunction= [Select id,Opportunity__c,Location__c from LocationJunction__c where Opportunity__c in :lstOpportunity];
if(lstlocationJunction!= null && lstlocationJunction.size()>0){
for(LocationJunction__c locationJunction:lstlocationJunction){
if(locationJunction.Location__c == mapOppCamp.get(locationJunction.Opportunity__c)) { trigger.new[0].adderror('This Location already exist in other record');
}
}
}
}
}
trigger LocationJunctionCRUD on LocationJunction__c (before insert)
{
List<Id> lstOpportunity= new List<Id>();
Map<Id,Id> mapOppLoc= new Map<Id,Id>();
for(LocationJunction__c locationJunction: Trigger.new){
lstOpportunity.add(locationJunction.Opportunity__c);
mapOppLoc.put(locationJunction.Opportunity__c,locationJunction.Location__c );
}
if(lstOpportunity != null && lstOpportunity.size()>0){
List<LocationJunction__c> lstlocationJunction= [Select id,Opportunity__c,Location__c from LocationJunction__c
where Opportunity__c in :lstOpportunity];
if(lstlocationJunction!= null && lstlocationJunction.size()>0){
for(LocationJunction__c locationJunction:lstlocationJunction){
if(locationJunction.Location__c == mapOppCamp.get(locationJunction.Opportunity__c))
{
trigger.new[0].adderror('This Location already exist in other record');
}
}
}
}
}
Hope this helps.