0

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

  • Wait a minute.... Account is parent of Contact. If you are inserting a fresh account, then how come the contact would exists? Please elaborate your question. – Shailesh Patil Jan 14 '15 at 9:18
  • All contacts do not have accounts. It is possible that the lead was created as a contact and later a need was felt to create an account. – abhi Nov 12 '15 at 20:31
1

There are a lot of improvements that need to be done on your code.
1.

  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
  }

Here you need not create a map if you are using the map.keySet() method in the SOQL. Instead create a Set<Id> and use it in the SOQL.
2.

  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
  }

Here as you are fetching the listacc from SOQl, you can directly create a Map<Id, Account> from it. Use:

Map<Id, Account> mapIdAccount = new Map<Id, Account>([YOUR SOQL QUERY]);

When you pass a list of sObjects as a parameter to the map constructor, it creates a map of Id and the sObject. Refer Salesforce Map Documentation
3.

  for(Contact con :trigger.new){   //new contacts 
     con.Description = mp1.get(con.AccountId).Description;  //  discription field 
  }

Here before you set con.Description, check whether mp1.get is null or not. If the map doesn't contain that key, the get() method simply returns null. Hence it produces a NullPointerException.
Use

if(mp1.containsKey(con.AccountId))
   con.Description = mp1.get(con.AccountId).Description;
  • 3
    not to mention that naming one's maps mp and mp1 causes endless confusion. – cropredy Jan 19 '15 at 23:18
  • Of course they do – Saket Joshi Jan 20 '15 at 2:12
0

I don't think you have to use Trigger in this case, and adding a custom formula field instead of Contact.Description field which is lookup to Account.Description is a more simple solution.

If you really want to use Trigger,you will need both of Contact Trigger and Account Triger to keep the same values between Contact and Account.

And if you only want to fix the CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY error,how about the following code.

trigger updatecontactusingmap on Contact (before insert, before update) {
  Map<Id,Contact> mp = new Map<Id,Contact>();  // new map 

  for(Contact con :trigger.new){  
    if(String.isEmpty(con.AccountId)){
        continue;
    }
     mp.put(con.AccountId,con);  // enter contact id in map
    }
  }
  Set<Id> idSet = mp.KeySet();
  if(!idSet.isEmpty()){
    Map<Id,Account> mp1 = new Map<Id,Account>([SELECT Id,Description FROM Account WHERE Id IN:idSet]);  //  fetch the account details
      for(Contact con :trigger.new){   //new contacts 
        if(String.isEmpty(con.AccountId)){
            continue;
        }
        con.Description = mp1.get(con.AccountId).Description;  //  discription field 
      }
  }

}
0

Try this one it will work

trigger updateContactDescription on Contact (before insert,before update) {

   Map<Id,contact> map1 = new Map<Id,contact>();
   for(contact con : trigger.new)
     map1.put(con.AccountId,con);

   //if(map1.size() > 0)
   Map<Id,Account> map2 = new Map<Id,Account>([SELECT Id ,Description FROM Account WHERE Id IN :map1.keySet()]);

   for(Contact con : Trigger.new){
      if(map2.get(con.AccountId).Description != NULL)
         con.Description = map2.get(con.AccountId).Description;
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.