In my trigger,I would try to update contact description field when i insert a new account and add some value in description field,But when i save the account i will face this error.
trigger updatecontactusingmap on Contact (before insert, before update) {
Map<Id,Contact> mp = new Map<Id,Contact>(); // new map
for(Contact con :trigger.new){
mp.put(con.AccountId,con); // enter contact id in map
}
List<Account> listacc = [SELECT Id,Description FROM Account WHERE Id IN:mp.KeySet()]; // fetch the account details
Map<Id,Account> mp1 = new Map<Id,Account>(); // new map
for(Account a : listacc){ // enter the list details in a
mp1.put(a.Id,a); // enter account id in map
}
for(Contact con :trigger.new){ //new contacts
con.Description = mp1.get(con.AccountId).Description; // discription field
}
}
first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updatecontactusingmap: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger