2

So I am a total newb here and I am sure I'm missing something easy but can anyone tell me why the key isn't being set thus resulting in a null point exception?

trigger Contact_Assignment_ET on Exact_Target__c (before insert) {

Set<String> CIFs = new Set<String>();

for (Exact_Target__c et : Trigger.new)
    CIFs.add(et.Customer_Identifier__c);

Map<String, Contact> contacts = new Map<String, Contact>([SELECT Id FROM Contact WHERE CIF__c in : CIFs]);

for(Exact_Target__c et :trigger.new)
    et.Contact__c = contacts.get(et.Customer_Identifier__c).Id;
}

I am getting a NullPointer Exception:

System.NullPointerException: Attempt to de-reference a null object: Trigger.Contact_Assignment_ET: line 13, column 1

  • Post the error message. – navD87 May 8 '15 at 22:30
2

There is an error in your logic. Your map contacts has the contact record Ids as it's key values but later in you code segment

for(Exact_Target__c et :trigger.new) et.Contact__c = contacts.get(et.Customer_Identifier__c).Id;

you are trying to get a value from the map using the key value et.Customer_Identifier__c which returns a null because that key doesn't exist in your map.

New code :

trigger Contact_Assignment_ET on Exact_Target__c (before insert) {

    Set<String> CIFs = new Set<String>();

    for (Exact_Target__c et : Trigger.new)
        CIFs.add(et.Customer_Identifier__c);

    List<Contact> contacts = [SELECT Id, CIF__c FROM Contact WHERE CIF__c in : CIFs];

    Map<String, Contact> contactsByCustomerIdentifier = new Map<String, Contact>();
    for(Contact c : contacts) {
        contactsByCustomerIdentifier.put(c.CIF__c, c);
    }

    for(Exact_Target__c et :trigger.new) {
        if(contactsByCustomerIdentifier.containsKey(et.Customer_Identifier__c)) {
            et.Contact__c = contactsByCustomerIdentifier.get(et.Customer_Identifier__c).Id;
        }
    }
}
  • You forgot to check if contacts contains that Customer Identifier-- this can still crash. – sfdcfox May 8 '15 at 22:42
  • Thank you very much, I was having a heck of a time getting my head to wrap around this for some reason. – Vlad May 8 '15 at 22:43
  • There's still a bug, though. If the contact doesn't exist, the field won't be wiped out in the process. See my code by way of example. – sfdcfox May 8 '15 at 22:44
1

you have to check if the map contains the key/value for the key you are passing in before reading a property out of that value object.

you can use map.containsKey to check the existence and then proceed with reading the value only if it exists in the map.

    for(Exact_Target__c et :trigger.new){
       if(contacts.containsKey(et.Customer_Identifier__c))
          et.Contact__c = contacts.get(et.Customer_Identifier__c).Id;
    }
  • The query's incorrect (Line 7), they need the map by Customer Identifier, not by ID (presumably). – sfdcfox May 8 '15 at 22:41
  • copy paste of the original code.. jus added the containskey part.. i shd hv jus kept the lines i focused on.. – Vamsi Krishna May 8 '15 at 23:00
0

You can use an ID value in this case to avoid the null pointer exception.

trigger Contact_Assignment_ET on Exact_Target__c (before insert) {
    Set<String> CIFs = new Set<String>();

    for (Exact_Target__c et : Trigger.new)
        CIFs.add(et.Customer_Identifier__c);

    Map<String, Id> contactsByCustomerIdentifier = new Map<String, Id>();
    for(Contact c : [SELECT Id, CIF__c FROM Contact WHERE CIF__c in : CIFs]) {
        contactsByCustomerIdentifier.put(c.CIF__c, c.Id);
    }

    for(Exact_Target__c et :trigger.new)
        et.Contact__c = contactsByCustomerIdentifier.get(et.Customer_Identifier__c);
}

If it's null, this will have the same effect as emptying out the lookup field.

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.