4

I have a Junction object called "LOCATION JUNCTION", and it has two master-detail relationships to Opportunity and Location

When I create new junction object records like

  1. name1 opp1 loca1 
  2. name2 opp2 loca1 

The system should throw an error like

"please select different location"

(FOR one account if  opp1 is having location1 then opp2 should not take location1) (Opportunities under same account should not have same location) How should I write a trigger on this junction object. Please suggest. enter image description here

Thanks

  • Should this be part of trigger or validation rules ? – karthikselva Feb 18 '16 at 7:00
  • Part of Trigger karthik – May the Force be with you Feb 18 '16 at 9:02
  • I have updated my ans please try that – Peeyush Feb 18 '16 at 11:15
2

I am assuming junction object name is LocationJunction__c and location field name is Location__c.

Following code will only process one record properly. Click to Bulkify your trigger

trigger LocationJunctionTrigger on LocationJunction__c (after insert,after Update)
{
    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
    {
        List<LocationJunction__c> currentList = [SELECT Id,Location__c,Opportunity__r.AccountId from LocationJunction__c WHERE Location__c=:trigger.new[0].Location__c AND Id IN : trigger.new];
        List<LocationJunction__c> existingList = [SELECT Id,Location__c,Opportunity__r.AccountId from LocationJunction__c WHERE Location__c=:trigger.new[0].Location__c AND Id Not IN : trigger.new];

        for(LocationJunction__c currentLocation : currentList)
        {
            for(LocationJunction__c existingLocation : existingList)
                if(existingLocation.Opportunity__r.AccountId == currentLocation.Opportunity__r.AccountId)
                    trigger.new[0].adderror('Please select different location');
        }
    }
}
  • Thanks Peeyush, it should check for opportunity1 and opportunity2 which are related to same Account. – May the Force be with you Feb 18 '16 at 7:43
  • Ok i will update my answer – Peeyush Feb 18 '16 at 7:46
  • Please try this code – Peeyush Feb 18 '16 at 11:06
  • Thank you very much Peeyush, its working for this requirement. For the same requirement if we have opp2 startdate is BETWEEN opp1 startdate and closedate then the location should not be same as opp1 for opp2 (location should be different). if opp2 startdate is NOT BETWEEN opp1 startdate and closedate then the location can be anything. can you please advice. – May the Force be with you Feb 18 '16 at 14:27
  • In both query you should add Opportunity__r.StartDate and end date. Now you put one more condition in inner for loop -> If((existing condition)&&(currentLocation.Opportunity__r.StartDate>=existingLocation.Opportunity__r.StartDate&&currentLocation.Opportunity__r.StartDate<=existingLocation.Opportunity__r.endDate)) – Peeyush Feb 19 '16 at 6:07
0

You can try below code

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.

  • Martin, I guess this will make any Opportunity with same location throws error. I need somethiing like "Opportunities under same account should not have same location".. – May the Force be with you Feb 18 '16 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.