+ Start a Discussion
nagasnagas 

trigger issue

hi,

 

I have a scenario here where i need to create a trigger.

 

scenario:  I have 3 objects objectA, object B, and object C. object B is an mirror image of object A. object B and object C have one-many relationship. object C lies in related list of object B.

 

I want to write a trigger on object A when a filed called "status is updated to "open"" a trigger should fire and check for object B for any matching records matching the Id of object A and then check for the object C whether the Id of object B exists in it and check a filed named "territory role" and if its value is "quota rep" the trigger should shoot an error message saying "you cannot update the status field".

 

Note:  there is no direct relationship between object A and object C.

 

trigger opencheck on Territory (before update) {
    
    Map<Id,Territory_Shaddow__c> tshadow = new Map<Id,Territory_Shaddow__c>();
    
    Map<String,Territory_Member_Shaddow__c> mshadow = new Map<String,Territory_Member_Shaddow__c>();
    
    for(Territory_Shaddow__c ts :[select territory_Id__c,name from territory_shaddow__c where Territory_Id__c  =:Trigger.new[0].id])
    {
        if(!tshadow.containsKey(ts.Territory_Id__c))
        {
            tshadow.put(ts.Territory_Id__c,ts);
        }
    }
    
    for(Territory_Member_Shaddow__c ms : [select territory_shaddow__c,territory_role__c from territory_member_shaddow__c where territory_role__c = 'primary quota rep' and territory_shaddow__c IN :tshadow.keySet()])
    {
        
        if(!mshadow.containskey(ms.territory_shaddow__c))
        {
            mshadow.put(ms.territory_shaddow__c,ms);
        }
    }
    for(territory t :Trigger.New)

{
   if(t.status__c == 'open')
   {
   
        if(tshadow.containskey(t.Id))
        {
          if(!mshadow.Isempty())
          {
            Trigger.new[0].status__c.addError('You cannot select the status as open since it has primary quota rep assigned to it');
           } 
        }
     }

 

 

I wrote a trigger but its not working .

 

Best Answer chosen by Admin (Salesforce Developers) 
nagasnagas

the problem coming up because there is no direct reference form territory object to territory member shaddow object so i created a formula field which gets the territory Id from the territory shaddow object and store in it then i used that reference to check for the validation of records in territory member shaddow object . Finally the problem solved.