+ Start a Discussion
Paras JainParas Jain 

Trigger Not Working on Mass update of property Object

I have populates core field value from Property to its related Cntact 's Account ...on Account the field name is Core
trigger updateAccCorefrmProperty2 on Property__c (after insert, after update, after delete) {


      Set<Id> accid = new Set<Id> ();
      Set<Id> conids= new Set<Id> ();
        Set<Id> conids2= new Set<Id> ();
      Map<id,Id> mpConAccIds= new Map<id,Id>();        
      Map<id,List<Contact>> mpConAccIds2= new Map<id,List<Contact>>();        
      Map<id,List<Property__c>> mpConProp = new  Map<id,List<Property__c>>();     
      Set<String>  PropCores= new Set<String>();
      Map<Id,Set<String>> mpCores = new Map<id,Set<String>> ();
      List<Account> acco = new List<Account>();
      set<id>  propids= new set<id> ();
           
    if(Trigger.IsInsert || Trigger.IsUpdate ) {
       for(Property__c prp: [select id,core__c ,Contact__c,Contact__r.Accountid,Contact__r.Account.core__c from Property__c where id in : Trigger.new] )
       {
        if(prp.Contact__c!=null && prp.core__c !=null)
        accid.add(prp.Contact__r.Accountid);
        
        }
       }
 
    if(Trigger.IsDelete ){
     
         for(Property__c prp :Trigger.old){
             if(prp.Contact__c!=null && prp.core__c !=null)
             propids.add(prp.Contact__c);        
         }
         
         for(Contact con: [select id, AccountId from Contact where id in : propids] )
         {  
              
            accid.add(con.Accountid);
         }    
     }    
    
       for(Account acc: [select id,(select id,AccountId from Contacts) from Account where id in : accid]){
       
          mpConAccIds2.put(acc.id,acc.Contacts);
       
              for(Contact con : acc.Contacts){
                  conids.add(con.id);
               }      
       }
       
       
       for(Contact cont: [select id,(select id,Core__c from Properties__r where Core__c !=null) from Contact where id in : conids2]){
       
       if(cont.Properties__r !=null)
           mpConProp.put(cont.id,cont.Properties__r);
           
           }

          
          
           for(id acc: mpConAccIds2.keyset()){
               PropCores.clear();
               for(Contact cons : mpConAccIds2.get(acc) ){
                   for(Property__c prop: mpConProp.get(cons.id) ){
                       if (prop.core__c != null){
                           PropCores.add(prop.core__c);                   
                       }
                    }
                   
                   }    
               
                     mpCores.put(acc,PropCores);           
               
               }
               
      String new_propCore;
       
       for(Account acc : [select id,core__c from Account where id in :mpCores.keyset()]){
           for(String str: mpCores.get(acc.id)){
               if(new_propCore!=null)
                   new_propCore+= ','+str;
               else{
               new_propCore=str;
               }    
           }
               acc.core__c =new_propCore ;
               acco.add(acc);
       }
       
 
       if(acco.size()>0)
       update acco;
        
        

}
Amit Chaudhary 8Amit Chaudhary 8
Please let us know wht issue you are getting ? Your trigger look good to me
Paras JainParas Jain
I have done more modification and now null pointer at line 65


trigger updateAccCorefrmProperty_final on Property__c (after insert, after update, after delete) {


      Set<Id> accid = new Set<Id> ();
      Set<Id> conids= new Set<Id> ();
        Set<Id> conids2= new Set<Id> ();
      Map<id,Id> mpConAccIds= new Map<id,Id>();        
      Map<id,List<Contact>> mpConAccIds2= new Map<id,List<Contact>>();        
      Map<id,List<Property__c>> mpConProp = new  Map<id,List<Property__c>>();     
      Set<String>  PropCores= new Set<String>();
      Map<Id,Set<String>> mpCores = new Map<id,Set<String>> ();
      List<Account> acco = new List<Account>();
      set<id>  propids= new set<id> ();
           
    if(Trigger.IsInsert || Trigger.IsUpdate ) {
       for(Property__c prp: [select id,core__c ,Contact__c,Contact__r.Accountid,Contact__r.Account.core__c from Property__c where id in : Trigger.new] )
       {
        if(prp.Contact__c!=null && prp.core__c !=null)
        accid.add(prp.Contact__r.Accountid);
        conids2.add(prp.Contact__c);
        
        }
       }
 
    if(Trigger.IsDelete ){
     
         for(Property__c prp :Trigger.old){
             if(prp.Contact__c!=null && prp.core__c !=null)
             propids.add(prp.Contact__c);        
         }
         
         for(Contact con: [select id, AccountId from Contact where id in : propids] )
         {  
              
            accid.add(con.Accountid);
         }    
     }    
    
       for(Account acc: [select id,(select id,AccountId from Contacts) from Account where id in : accid]){
       
          mpConAccIds2.put(acc.id,acc.Contacts);
       
              for(Contact con : acc.Contacts){
                  conids.add(con.id);
               }      
       }
       
       
       for(Contact cont: [select id,(select id,Core__c from Properties__r where Core__c !=null) from Contact where id in : conids2]){
       
       if(cont.Properties__r !=null)
           mpConProp.put(cont.id,cont.Properties__r);
           
           }

          
          
           for(id acc: mpConAccIds2.keyset()){
               PropCores.clear();
               List<Contact>  lts =mpConAccIds2.get(acc);
               if(lts.size()>0){
               for(Contact cons : mpConAccIds2.get(acc) ){
                   
                   List<Property__c >  ltspp =mpConProp.get(cons.id);
                       if(ltspp.size()>0){
               
                   for(Property__c prop: mpConProp.get(cons.id) ){
                       if (prop.core__c != null){
                           PropCores.add(prop.core__c);                   
                       }
                    }}
                   
                   }    
               }
                     mpCores.put(acc,PropCores);           
               
               }
               
      String new_propCore;
       
       for(Account acc : [select id,core__c from Account where id in :mpCores.keyset()]){
           for(String str: mpCores.get(acc.id)){
               if(new_propCore!=null)
                   new_propCore+= ','+str;
               else{
               new_propCore=str;
               }    
           }
               acc.core__c =new_propCore ;
               acco.add(acc);
       }
       
 
       if(acco.size()>0)
       update acco;
        
        

}
Nidhi GuptaNidhi Gupta
Hi Paras,

The issue is with code  if(ltspp.size()>0){
try replacing this code with  if(ltspp != null && (lts.size()>0)){

The scope of contacts in conids2 is not same as all the all contacts for all account in Accid.
You may have properties for contacts in conids2 but not necessarily for all the contacts associated with Accounts of contact in conids.
Hope this helps

Nidhi
 
Nidhi GuptaNidhi Gupta
And to add, the above change is a technical fix, you might have ot look at solution logic.

Thanks
Nidhi