+ Start a Discussion
lawlaw 

Code ignores if statement when Batch Update

I have a trigger that fires on Insert/Update.  Works fine when testing this by entering contacts or updating contacts manually one at a time.
However, when there is a batch update it seems to ignore this one  IF condition in my code.

Anyone ever experience this?
Balaji BondarBalaji Bondar
Hi Law.

paste the trigger code.There might be some issue with bulkfying the code.
 
MTBRiderMTBRider
Can you post the code?   
lawlaw
The highlighted line towards the bottom is the one that does not evaluate when a Bulk Update occurrs

public with sharing class updateSubscriptions {

String AdvisorRecordType;
String RTName;
     
      public void doAutoSubscribe(Map<Id, Contact> contactMap) {
     
        List<Subscription__c> existingSubs;       
        Set<ID> advisorIDs = contactMap.keySet();
//////// BEGIN Added  by lreed 09/23/2014  to Only Auto Subscribe for Advisors with ID RecordType                
        for (contact con : contactMap.values()){ 
            System.debug('con.RecordTypeID  ----->'+ con.RecordTypeID);            
            AdvisorRecordType =  con.RecordTypeID;
        }
        List<RecordType> RecordTypeName;
         RecordTypeName = [Select Id, Name from RecordType where ID = :AdvisorRecordType];
         
         for(RecordType r : RecordTypeName){
         RTName = r.name;    
         System.debug('THIS IS THE RECORDTYPE NAME ----->'+RTName);
         }
///////// END 

    
        Map<Id,Set<Id>> alreadySubscribed = new Map<Id,Set<Id>>();
        List<Subscription__c> newSubs = new List<Subscription__c>();
        
        existingSubs =  [SELECT Advisor__c, Publication__c, Id FROM Subscription__c where Advisor__c in :advisorIDs and Publication__r.Subscribe_On_Advisor_Create__c = true];

    for(Id id: advisorIds) {
      alreadySubscribed.put(id, new Set<id>());
    }
    
    for(Subscription__c sub : existingSubs) {
      Set<id> currentSubcriptions = new Set<id>();
      if (alreadySubscribed.containsKey(sub.Advisor__c)) {
        currentSubcriptions = alreadySubscribed.get(sub.Advisor__c);        
      }
      currentSubcriptions.add(sub.Publication__c);
      alreadySubscribed.put(sub.Advisor__c, currentSubcriptions);          
    }
   
    List<Publication__c> pubs;
    pubs = [ Select id from Publication__c where Subscribe_On_Advisor_Create__c = true ] ;
          
       for(Publication__c p : pubs) {
         for(Id id : advisorIds) {
           Set<id> currentSubscriptions = alreadySubscribed.get(id);
           System.debug('THIS IS THE RECORDTYPE NAME 2----->'+RTName);
           //if (!currentSubscriptions.contains(p.id) ) {  
           if (!currentSubscriptions.contains(p.id) && RTName == 'ID' ) {   // Add check for ID RecordType  Lreed 09/23/2014
             System.debug('Advisor Record TypeName 3----->'+ RTName);
             Subscription__c sub = new Subscription__c();
             sub.Advisor__c = id;
             sub.Publication__c = p.id;
             sub.Subscribed__c = true;
             newSubs.add(sub);
           }
         }
       }
       System.debug('NEW SUBS --------- >' + newSubs.size());
       System.debug('NEW SUBS EMPTY --------- >' + newSubs.isEmpty());
       if (!newSubs.isEmpty())  {
            System.debug('INSERT NEW SUBS --------- >');   
         insert newSubs; 
       }
       
     }