2016-09-30 53 views
0

獲取行「taskObj.OwnerId = ConMap.get(ben.contact__c).contact__r.OwnerId;」中的錯誤因爲ownerid領域正在接觸。如何解決System.NullPointerException:嘗試使用Map <parentId,child>來取消引用apex類中的空對象?

聯繫人是好處的父母,在這裏我獲得了啓動方法的所有好處。我想添加contactid只有一次,如果它有超過一個孩子,我用SET。我想使用地圖,因爲我需要從聯繫對象中獲取聯繫人的OwnerId字段,我在start方法的查詢中獲取該聯繫人對象。如何使用地圖訪問contact.ownerId字段?下面是代碼。

global Database.QueryLocator start(Database.BatchableContext bc) { 
    Query='select contact__r.ownerId, contact__c, Task_creation_date__c, Status__c, task_created__c, type__c from Benefit__c Where Task_creation_date__c= TODAY AND Status__c IN (\'Active\',\'Pending\') AND Task_created__c =FALSE AND Type__c!=\'Short Term Medical\''; 
    return Database.getQueryLocator(query); 

} 

global void execute(Database.BatchableContext bc, List<Benefit__c> scope){ 
    // process each batch of records 

    List<Contact> contacts = new List<Contact>(); 
    List<Task> TaskList = new List<Task>(); 
    set<id> conset = New set<id>(); 
    Map<id,benefit__c> ConMap = new map<id,Benefit__c>(); 
    for (Benefit__c b : scope) { 
      conset.add(b.contact__c); 
      ConMap.put(b.contact__c, b); 
      b.task_created__c = TRUE; 
    } 
     system.debug('contact and its benefit------'+ConMap); 
    recordsProcessed = conset.size(); 
    //List<Contact> tempList = new List<Contact>(); 
    // tempList = [Select Id,OwnerId, firstname from Contact where Id IN:(conset)]; 
    if(!ConMap.isEmpty()) 
     { 
     for(Benefit__c ben : ConMap.values()) 
     { 
     Task taskObj = new Task(); 
         taskObj.OwnerId = ConMap.get(ben.contact__c).contact__r.OwnerId; 

我想OWNERID填充接觸OWNERID的任務,但如何從地圖訪問,並保持獨特的接觸式ID的地圖嗎?

回答

0

我重新寫的代碼解決這個nd優化代碼如下。

public with sharing class MyBatch implements Database.Batchable<AggregateResult>{ 
public Iterable<AggregateResult> start(Database.BatchableContext context) 
{ 
    return [ 
     SELECT Contact__c, Contact__r.OwnerId owner FROM Benefit__c 
     WHERE Contact__c != null AND ... 
     GROUP BY Contact__c, Contact__r.OwnerId 
    ]; 
} 
public void execute(Database.BatchableContext context, List<AggregateResult> scope) 
{ 
    Set<Id> contactIds = new Set<Id>(); 
    List<Task> tasks = new List<Task>(); 
    for (AggregateResult aggregate : scope) 
    { 
     Id ownerId = (Id)aggregate.get('owner'); 
     Id contactId = (Id)aggregate.get('Contact__c'); 

     contactIds.add(contactId); 
     tasks.add(new Task(OwnerId=ownerId, /*other fields*/)); 
    } 
    insert tasks; 

    List<Benefit__c> benefits = [ 
     SELECT Id FROM Benefit__c WHERE Contact__c IN :contactIds 
    ]; 
    for (Benefit__c benefit : benefits) 
     benefit.Task_Created__c = true; 
    update benefits; 
} 
public void finish(Database.BatchableContext context) { /*additional logic*/ }} 
0

我看到批量查詢沒有過濾條件'Contact__c!= null'。因此,有可能某個福利記錄在'Contact__c'字段中缺失值,並且您不會在地圖中找到它。您可以通過兩種方式解決此問題:

  1. 如果您不關心這些記錄,請在選擇器查詢中添加'Contact__c!= null'。

(OR)

  • 檢查 '空' 在for循環如下值:

    if(!ConMap.isEmpty()) 
    { 
        for(Benefit__c ben : ConMap.values()) 
        { 
         if(String.isBlank(ben.Contact__c)){ 
          /* continue; 
           or 
           throw exception() 
          */ 
         } 
    
         Task taskObj = new Task(); 
         taskObj.OwnerId = ConMap.get(ben.contact__c).contact__r.OwnerId; 
    
  • +0

    謝謝維諾德,我能夠解決它使用其他方法,我也包括contact__c!= null。 – apple123

    相關問題