【问题标题】:Attempt to dereference a Null object Salesforce尝试取消引用 Null 对象 Salesforce
【发布时间】:2015-03-25 03:57:16
【问题描述】:

我已触发更新个人帐户中的联系人 ID 及其对个人帐户的工作,它正在填充个人帐户上的联系人 ID 字段。

问题是它在业务和雇主帐户记录类型上出现错误,我还插入了记录类型检查。但它仍然给出一个错误

关于错误:无效数据。

查看以下所有错误消息以更正您的数据。

Apex 触发器 UpdateContactID 导致意外异常,请联系您的管理员:UpdateContactID:执行 AfterInsert 原因:System.NullPointerException:尝试取消引用空对象:Trigger.UpdateContactID:第 41 行,第 1 列

触发器如下:

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

 //Identify applicable record type
    RecordType PersonAccount = [SELECT Id FROM RecordType WHERE SobjectType='Account' AND Name = 'Career Champion Account' limit 1];

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

if(trigger.isInsert) 
{



 List<String> newAccountIDList = new List<String>();


 // Taking all account IDs in collection
 for(Account acct: Trigger.new)
 {  
  newAccountIDList.add(acct.ID); 
 }

 // Fetching contacts against the account IDs
 List<Contact> contactList = [SELECT Id, Account.Id FROM Contact WHERE Account.ID in :newAccountIDList];      


 // Adding contacts in a map with relation to Account ID
 Map<String, Contact> mapContact = new Map<String, Contact>();
 for(Contact cont : contactList)
 {
  mapContact.put(cont.Account.Id, cont) ;
 }


 // Updating Contact_ID__c from Map to new Account list to update
 List<Account> newAccounts = [select Id, Contact_ID__c from Account where Id in :newAccountIDList];

 for(Account acct: newAccounts)
 {  
 **LINE 41**  
toUpdate.add(new Account(
  id = acct.Id,
  Contact_ID__c = mapContact.get(acct.Id).Id
 ));
}

update toUpdate;

} // if
else if(trigger.isUpdate && trigger.new[0].Contact_ID__c == null && trigger.old[0].Contact_ID__c == null) 
{



 List<String> newAccountIDList = new List<String>();


 // Taking all account IDs in collection
 for(Account acct: Trigger.new)
 {  
  newAccountIDList.add(acct.ID); 
 }

 // Fetching contacts against the account IDs
 List<Contact> contactList = [SELECT Id, Account.Id FROM Contact WHERE Account.ID in :newAccountIDList];      


 // Adding contacts in a map with relation to Account ID
 Map<String, Contact> mapContact = new Map<String, Contact>();
 for(Contact cont : contactList)
 {
  mapContact.put(cont.Account.Id, cont) ;
  }


 // Updating Contact_ID__c from Map to new Account list to update
 List<Account> newAccounts = [select Id, Contact_ID__c from Account where Id in :newAccountIDList];
// List<Account> toUpdate = new List<Account>();
 for(Account acct: newAccounts)
 {  

  toUpdate.add(new Account(
   id = acct.Id,
   Contact_ID__c = mapContact.get(acct.Id).Id
  ));
 }

 update toUpdate;

 } // else if

} // trigger

【问题讨论】:

    标签: triggers salesforce account


    【解决方案1】:

    很可能mapContact 不包含键acct.Id 的值。 mapContact.get(acct.Id) 调用将返回 null 并且下面的 .Id 将给出 null 引用异常。

    您应该在该行之前添加一个保护语句。例如

    if(mapContact.containsKey(acct.Id)) {
        toUpdate.add(new Account(
          id = acct.Id,
          Contact_ID__c = mapContact.get(acct.Id).Id
         ));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2013-12-23
      相关资源
      最近更新 更多