我已触发更新个人帐户中的联系人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
答案 0 :(得分:0)
最有可能mapContact
不包含密钥acct.Id
的值。 mapContact.get(acct.Id)
调用将返回null,并且以下.Id
将提供空引用异常。
您应该在该行之前添加一个保护声明。 E.g。
if(mapContact.containsKey(acct.Id)) {
toUpdate.add(new Account(
id = acct.Id,
Contact_ID__c = mapContact.get(acct.Id).Id
));
}