如何修复System.NullPointerException:尝试使用Map <parentid,child =“”>取消引用apex类中的空对象?

时间:2016-09-30 15:39:43

标签: salesforce apex apex-trigger

在行中获取错误“taskObj.OwnerId = ConMap.get(ben.contact__c).contact__r.OwnerId;”因为所有者字段正在联系。

联系是受益的父母,在这里我获得了start方法的所有好处。我想只添加一次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?

2 个答案:

答案 0 :(得分:1)

我发现批量查询没有过滤条件&#39; Contact__c!= null&#39;。因此,有可能其中一个福利记录在“联系人”中缺失了价值。字段,你不会在地图中找到它。您可以通过两种方式解决此问题:

  1. 添加&#39; Contact__c!= null&#39;如果您不关心这些记录,请选择选择器查询。
  2. (OR)

    1. 检查&#39; null&#39; 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;
      

答案 1 :(得分:0)

我重新编写了代码以解决此问题并优化代码如下所示。

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*/ }}