function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raghu Cheruvu 1Raghu Cheruvu 1 

Owner Id cannot be blank error

Hi, 

i am getting OwnerId cannot be blank while populating OwnerId in a trigger, the trigger is working when i load the data using a batch size of 1, but when i use the batch size of 200, i am getting the error owner id cannot be blank ( i have highlighted the line at which error is occuring in bold)

trigger Account_Trigger on Account (after insert, after update) {

Map<String,User> UserDetailsMap=new Map<String,User>();
 Map<String,String> UserIdMap=new Map<String,String>();
 Map<String,String> UserMap=new Map<String,String>();
 Map<String,String> ZipTerrMap=new Map<String,String>();
List<Account> newAccountList=new List<Account>();
Map<String,String> Zip_TerrMap=new Map<String,String>();
Set<String> AccountZipSet=new Set<String>();
 Set<String> newAccountZipSet=new Set<String>();
Map<String,String> UserTerrMap=new Map<String,String>();
List<Zip_to_Terr_vod__c> ZipTerrList=new List<Zip_To_Terr_vod__c>();
 List<Account> UpdateAccountList=new List<Account>();
  List<Alert_vod__c> AlertList=new List<Alert_vod__c>();List<Alert_vod__c> ExistingAlertList=new List<Alert_vod__c>();
  Map<Id,Alert_vod__c> AlertMap=new Map<Id,Alert_vod__c>();Set<Id> AlertSet=new Set<Id>();
   List<Alert_vod__c> InsertAlertList=new List<Alert_vod__c>();
   Map<Id,Account> AccountMap=new Map<Id,Account>();
   Set<Id> AlertAccountIds=new Set<Id>();
   
 List<Account> newAccList=new List<Account>([Select Id,Primary_Zip__c,Assigned_Rep__c,Rep_Territory__c,MSL_Territory__c,
New_RX_Alert_Expiration_Date__c,New_RX_Date__c,Rep_Email__c ,New_Prescriber__c,New_Precription__c,Specialty_1_vod__c
 from Account where Id IN:Trigger.newMap.keySet()
 ]);
 
 Profile_Custom_Settings__c p= Profile_Custom_Settings__c.getValues('SampleInstance');
 
string profileid=p.ProfileId__c;

if(UserInfo.getProfileId()==profileid){

List<User> UserList=new List<User>([Select Id,Primary_Territory_Vod__c,Name,Email from user where IsActive=true]);
               
        for(User u:UserList){
        
        UserDetailsMap.put(u.Id,u);
        
        UserMap.put(u.Primary_Territory_vod__c,u.Name);
        UserTerrMap.put(u.Primary_Territory_vod__c,u.id);
        UserIdMap.put(u.Primary_Territory_vod__c,u.Email);
        }

for(Account acc:newAccList){
    
   AccountZipSet.add(acc.Primary_Zip__c);
    
}

ZipTerrList=[Select Name,Territory_Vod__c from Zip_To_Terr_vod__c  where Name IN: AccountZipSet];
            
for(Zip_To_Terr_vod__C z: ZipTerrList){

ZipTerrMap.put(z.Name,z.Territory_vod__c);


}
              
 if(checkRecursive.runOnce()) {
            for(Account acc: newAccList){
     
     IF(ZipTerrMap.get(acc.Primary_Zip__c)!=null){
     acc.Rep_Territory__c=ZipTerrMap.get(acc.Primary_Zip__c).substring(0,8);
     acc.Assigned_Rep__c=UserMap.get(acc.Rep_Territory__c);
     acc.Rep_Email__c=UserIdMap.get(ZipTerrMap.get(acc.Primary_Zip__c).substring(0,8));
     
     if(ZipTerrMap.get(acc.Primary_Zip__c).length()>=15){
                acc.MSL_Territory__c=ZipTerrMap.get(acc.Primary_Zip__c).substring(7,15);
                acc.Assigned_MSL__c=UserMap.get(acc.MSL_Territory__c);
                                                                }
     updateAccountList.add(acc);
                                        }
     }

     update updateAccountList;
}

List<Account> updateAccList=new List<Account>();

/*for(Account acc: newAccList){

acc.Assigned_Rep__c=UserMap.get(acc.Rep_Territory__c);
acc.Assigned_MSL__c=UserMap.get(acc.MSL_Territory__c);
acc.Rep_Email__c=UserIdMap.get(ZipTerrMap.get(acc.Primary_Zip__c).substring(0,8));
updateAccList.add(acc);

}
update updateAccList; */

}
  
///To Insert Alerts...
  
List<Alert_vod__c> AlertExistingList=new List<Alert_vod__c>([Select Id,Account__c from Alert_Vod__c]);

for(Alert_vod__c a: AlertExistingList){

AlertAccountIds.add(a.Account__c);

}

for(Account acc:newAccList ){
    
    if(acc.New_RX_Date__c!=null){
AccountMap.put(acc.Id,acc);

    }
}

ExistingAlertList=[Select Id,Account__c from Alert_vod__c];

for(Alert_vod__c a: ExistingAlertList){

AlertMap.put(a.Account__c,a);

}
AlertList=[Select Id,Activation_Date_vod__c,Expiration_Date_vod__c,Link_Reference_vod__c,
           Account__c,New_Prescriber__c,
          Priority_vod__c,Public_vod__c,Dismissible_vod__c,Alert_Text_vod__c,Name,User__c,
          Rep_Email__c,New_Prescription__c
          from Alert_vod__c where 
          Account__c IN: AccountMap.keyset()];
                   
  //Insert updated alerts
  
  List<Alert_vod__c> InsertUpdAlerts=new List<Alert_vod__C>(); 
  

if(Trigger.IsUpdate){

for(Account acc: newAccList){

    if(Trigger.oldMap.get(acc.Id).New_RX_Date__c!=Trigger.newMap.get(acc.Id).New_RX_Date__c){


//for(Alert_vod__c a: AlertList){

//if(AccountMap.get(a.Account__c).New_Prescriber__c=='Yes'){

if(acc.New_Rx_Date__c!=null){

Alert_vod__c a=new Alert_vod__c();
//a.Activation_Date_vod__c=AccountMap.get(a.Account__c).New_Rx_Date__c;
a.Account__c=acc.Id;
a.Activation_Date_vod__c=acc.New_Rx_Date__c;
//a.Expiration_Date_vod__c=AccountMap.get(a.Account__c).New_RX_Alert_Expiration_Date__c;
a.Expiration_Date_vod__c=acc.New_RX_Alert_Expiration_Date__c;

a.public_vod__c=false;
a.alert_text_vod__c='Specialty:' + ' ' +   acc.Specialty_1_vod__c +
                 '\r\n' +
                 + 'Click HCPs link to view additional information';
                 
                 if(acc.New_Prescriber__c=='Yes'){
a.Name='New Prescriber';
a.New_Prescriber__c='Yes';
a.New_Prescription__c='No';

                 }
                 
           if(acc.New_Precription__c=='Yes'){
a.Name='New Prescription';
a.New_Prescription__c='Yes';
a.New_Prescriber__c='No';

                 }
a.Link_Reference_vod__c=acc.Id+','+'Account';
a.Priority_vod__c='Important';
a.Dismissible_vod__c=true;
a.Rep_Email__C=acc.Rep_Email__c;
a.OwnerId=UserTerrMap.get(ZipTerrMap.get(acc.Primary_Zip__c).substring(0,8));

InsertUpdAlerts.add(a);
//}
}
}


}
}    
insert InsertUpdAlerts;

if(Trigger.IsInsert){
   for(Account a: Trigger.new){
   
        if(a.New_RX_Date__c!=null){
   
       if(!AlertAccountIds.contains(a.id)){
       
       Alert_vod__c al=new Alert_vod__c();
       
       al.Account__c=a.id;
       
       al.Activation_Date_vod__c=a.New_Rx_Date__c;
       
       al.Expiration_Date_vod__c=a.New_RX_Alert_Expiration_Date__c;
       
       al.public_vod__c=false;
       
       al.alert_text_vod__c='Specialty:' + ' ' +   a.Specialty_1_vod__c +
                 '\r\n'+
                 + 'Click HCPs link to view additional information';
                 
                 if(a.New_Prescriber__c=='Yes'){
       al.name='New Prescriber';
       al.New_Prescriber__c='Yes';
       al.New_Prescription__c='No';
       
                 }
                 
                 
              if(a.New_Precription__c=='Yes'){
       al.name='New Prescription';
       al.New_Prescription__c='Yes';
       al.New_Prescriber__c='No';
       
                 }   
       
       al.Link_Reference_vod__c=a.Id+',' + 'Account';
       
       al.Priority_vod__c='Important';
     
       
       al.Rep_Email__C=a.Rep_Email__c;
       
       al.Dismissible_vod__c=true;
       
        
       
       al.OwnerId=UserTerrMap.get(ZipTerrMap.get(a.Primary_Zip__c).substring(0,8));
       
       InsertAlertList.add(al);
      }
    }
     }
     insert InsertAlertList;
     }
   }
Santanu HalderSantanu Halder
After a quick look, only thing I can imagine, UserTerrMap is not holding the User record the expression ZipTerrMap.get(acc.Primary_Zip__c).substring(0,8) is returning. One of the reason could be you are querying only for active users, so try removing the isActive = true clause from the query on User record.

Incase it doesnt work, try puutting some Debug statements and track the flow.