+ Start a Discussion
nagasnagas 

batch apex issue

I have an issue with an batch apex code I wrote a batch apex to go a ahead check on territory object where it checks its related object and if it didnot find matching records keeps all the records in a string. That  string is passed as an attachment to an custom object once the batch process is completed. I wrote the logic but finally when i run it the string is returning null. saying argument cannot be null.

 

 

global class UserTerritoryCheck implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallOuts {

     String myString ;
     
     
        
    /* Start Method*/
     global Database.QueryLocator start(Database.BatchableContext BC)
     {
       
        String query ='select t.id,t.name from territory t where (t.id IN (select ut.territoryid from userterritory ut))'; 
       system.debug('query ----->' +query);
        return(Database.getQueryLocator(query));
        
     }
     
     /*Execute Method*/
     global void execute(Database.BatchableContext BC, list<Sobject> scope)
     { 
        list<String> UsT = new list<String>();
        for(Sobject cc : scope)
           {
            UsT.add(cc.Id);
           }
          map<id,territory> StdTerritory = new map<id,territory>();
    
    for(territory ter:[select id,name from territory  where id IN :ust])
    {
    
    if(!stdterritory.containskey(ter.id))
     {
       stdterritory.put(ter.id,ter);
     }
    
    }
  
  map<id,userterritory> ustr = new Map<id,userterritory>();
  
  for(userterritory uter:[select id,userid,territoryid,IsActive from userterritory where territoryid  IN :stdterritory.keyset()and IsActive=true ])
    {
      if(!ustr.containskey(uter.territoryid))
      {
         ustr.put(uter.territoryid,uter);
      }
    
    }
  
   Map<id,user_territory_shadow__c> ushadow = new Map<id,user_territory_shadow__c>([Select u.User__c, u.Territory_ID__c From User_Territory_Shadow__c u Where( u.User__c IN  (Select ut.UserId From UserTerritory ut))  AND u.Territory_ID__c IN :stdterritory.keyset()]);
   
   
       String myString='TerritoryName,UserID';

        for(Territory terr :stdTerritory.values())
        {
            if(ustr.containskey(terr.id))
            {
                if(!ushadow.containskey(terr.id))
                {
                    
                    myString+='\n'  + terr.name + ',' + ustr.get(terr.id).Userid;
                      
                }
            }
            
        }
        
        
          
       
     }   
   
     /*Finish Method*/
     global void finish(Database.BatchableContext BC)
    { 
          errorlog__c myobj = new errorlog__c();
          insert myobj; 
         
          Attachment att=new Attachment();
          att.Body=Blob.valueOf(mystring);
          att.name = 'errordetails.csv';
          att.ParentId = myobj.id;
          insert att; 
    }
    

 the error showing at the redcoded line. i tried keeping all the inserting custom object and attaching the file in the execute method then i am not getting the errors the object is creating with attachements. But in my case i have 3000 territories the batch size if i give 1000 there 3 batches running with 3 custom objects created. I want only one object to be created for the whole  process. So i moved that code to finish method which is giving these errors. Can anyone help me in solving this error.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
nagasnagas

solved the issue i declared the string variable globally as null so its taking the null as default value,Niow i gave the fields to display for the string and its working now the updated code is below.

global class UserTerritoryCheck implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallOuts {

      String myString='TerritoryName,UserID';
     
        
    /* Start Method*/
     global Database.QueryLocator start(Database.BatchableContext BC)
     {
       
        String query ='select t.id,t.name from territory t where (t.id IN (select ut.territoryid from userterritory ut))'; 
       system.debug('query ----->' +query);
        return(Database.getQueryLocator(query));
        
     }
     
     /*Execute Method*/
     global void execute(Database.BatchableContext BC, list<territory> scope)
     { 
        list<String> UsT = new list<String>();
        for(Territory cc : scope)
           {
            UsT.add(cc.Id);
            }
        
       
         map<id,territory> StdTerritory = new map<id,territory>();
    
    for(territory ter:[select id,name from territory  where id IN :ust])
    {
    
    if(!stdterritory.containskey(ter.id))
     {
       stdterritory.put(ter.id,ter);
     }
    
    }
   
   map<id,userterritory> ustr = new Map<id,userterritory>();
  
  for(userterritory uter:[select id,userid,territoryid,IsActive from userterritory where territoryid  IN :stdterritory.keyset()and IsActive=true ])
    {
      if(!ustr.containskey(uter.territoryid))
      {
         ustr.put(uter.territoryid,uter);
      }
    
    }
  
   Map<id,user_territory_shadow__c> ushadow = new Map<id,user_territory_shadow__c>([Select u.User__c, u.Territory_ID__c From User_Territory_Shadow__c u Where( u.User__c IN  (Select ut.UserId From UserTerritory ut))  AND u.Territory_ID__c IN :stdterritory.keyset()]);

   
      for(Territory terr : stdterritory.values())
        {
            if(ustr.containskey(terr.id))
            {
                if(!ushadow.containskey(terr.id))
                {
                    
                    myString+='\n'  + terr.name + ',' + ustr.get(terr.id).Userid;
                      
                }
            }
           }
      
     }   
   
     /*Finish Method*/
     global void finish(Database.BatchableContext BC)
    { 
      
         errorlog__c myobj = new errorlog__c();
          insert myobj; 
         
          Attachment att=new Attachment();
          att.Body=Blob.valueOf(mystring);
          att.name = 'errordetails.csv';
          att.ParentId = myobj.id;
          insert att; 
        
    }
  
}