1

I'm brand new at SF development but here's what I'm trying to do.

I have a property custom object and it has some basic fields (Address, City, State, Zip). I have a Leasing Invoice custom object as well and when creating that I have a lookup for an associated property. Upon selecting from the lookup, I'd like it to autopopulate the property's address in the Property Address field so that someone can see not only the name of the Property but where it is. This is a picture of my Leasing Invoice form:

enter image description here

I've written the following trigger:

trigger populatePropertyAddress on Leasing_Invoice__c (before insert, before update) {
 if(trigger.isbefore && trigger.isinsert){

Set<ID> PropIds = new Set<ID>();

for(Leasing_Invoice__c li : trigger.new)
{
    PropIds.add(li.Property__c);
}

list<Property__c> proplist = [SELECT Address__c, id FROM Property__c WHERE id IN:PropIds];

MAP<ID, Property__c> mapProp = new MAP<ID, Property__c>();

for(Property__c p : proplist)
{

   mapProp.put(p.id,p);
}

for(Leasing_Invoice__c li : trigger.new)
{
   if(trigger.oldmap.get(li.Id).Property__c != li.Property__c){
      if(li.Property__c!=null)
      {
         if(mapProp.containskey(li.Property__c))
         {
            Property__c p = mapProp.get(li.Property__c);
            li.Property_Address__c = p.Address__c;
         }
   }

   else
   {
      li.Property_Address__c = null;
   }
}

} } }

Whenever I try to create a new Leasing Invoice now, whether I populate the Property field or not, I get the error:

"Apex trigger populatePropertyAddress caused an unexpected exception, contact your administrator: populatePropertyAddress: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.populatePropertyAddress: line 23, column 1"

I realize my code is not numbered but for clarity that's this line:

if(trigger.oldmap.get(li.Id).Property__c != li.Property__c){

Can anyone explain what I'm doing wrong?

Thanks so much!

Nick

1

1 Answer 1

1

Trigger.oldMap is null on before insert/after insert trigger events. You'll want to do something like this:

if(Trigger.isInsert || trigger.oldmap.get(li.Id).Property__c != li.Property__c) {

This will prevent the null-pointer exception.

You'll want to read this topic for details about when the various Trigger context variables are available/not null.

Here's the elements available and notes regarding them:

isExecuting     Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsert        Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdate        Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDelete        Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBefore        Returns true if this trigger was fired before any record was saved.
isAfter         Returns true if this trigger was fired after all records were saved.
isUndelete      Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
new             Returns a list of the new versions of the sObject records.
                Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
newMap          A map of IDs to the new versions of the sObject records.
                Note that this map is only available in before update, after insert, and after update triggers.
old             Returns a list of the old versions of the sObject records.
                Note that this sObject list is only available in update and delete triggers.
oldMap          A map of IDs to the old versions of the sObject records.
                Note that this map is only available in update and delete triggers.
size            The total number of records in a trigger invocation, both old and new.
1
  • @NickOliver I had a typo... I was trying to imply that oldMap is null for insert events, not before events. I've updated this answer.
    – sfdcfox
    Nov 20, 2015 at 20:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .