+ Start a Discussion
dkccraigdkccraig 

Null Values

Hi

 

I am trying to do something very simple, but it seems simple things are difficult with APEX.  Or I am completely not understanding.  Probably the latter.

 

I have a Custom Object named Junction Object with an Account as the detail in a master detail relationship.

 

On a trigger ALL i am trying to do is set the Account's website name to www.google.com.

 

I wrote the following code to do so:

 

trigger InsertionTrigger on Junction_Object__c (before insert)
{
  for(Junction_Object__c jo: Trigger.new)
  {
      jo.Account__r.Website = 'www.google.com';
  }
}

 

Ok, this code seems straightforward.  So I go and create a Junction Object tab and then using the tab I try to create a new Junction Object.  So I insert the required fields for the new Junction Object and hit "save".  Well it tells you that you cannot de-reference a null value.  How can Account__r be null if I just inserted data for it? 

 

I don't get it.  Time to continue banging my head against the desk.

SuperfellSuperfell

related objects are not populated in triggers, only their foreign key values (so account__c contains the account Id), and because the object your trying to change is not the triggered record itself, you'll need to call update yourself, something like

 

trigger myTrigger on Junction_Object__c (before insert)

{

Set<Account> accs = new Set<Account>(); 

for (Junction_Object__c o : Trigger.new) {

  Account a = new Account(Id=o.account__c, website='www.google.com'); 

  accs.put(a);

}

update accs;

dkccraigdkccraig

Thanks for your help Simon, I was able to change your code to the following to get it to work.

 

trigger InsertionTrigger on Junction_Object__c (before insert)

  Account a;
  
  for(Junction_Object__c jo: Trigger.new)
  {
    a = new Account(ID=jo.Account__c, website='www.google.com');
  }

  update a;
}

 

Ok thats great.  Now lets take it a step further.

 

I have a Contact which is also in a master-detail relationship with the Junction Object.  I want to copy the phone number from the Account to the Contact.  You would think the following code would work, because if the previous code works fine, it really only makes sense that this code works fine too.

 

trigger InsertionTrigger on Junction_Object__c (before insert)

  Account a;
  Contact c;


  for(Junction_Object__c jo: Trigger.new)
  {
    a = new Account(ID=jo.Account__c);

    c = new Contact(ID=jo.Contact__c, Phone = a.Phone)
  }

  update c;
}

 

...and of course that does not work.  It doesn't copy anything.  How come the API doesn't reference constructors and how they work?  I am trying to learn APEX and the Force for a prospective job, but I am at wits end.  How can a.Phone be blank, but if you go and look at that Account record there is clearly a phone number?  I can program Java and SQL fine, but APEX just gives me headaches, what dont I understand?

SuperfellSuperfell

Have you read the tutorials and other docs ?

 

a.phone is blank because a is just an in memory account object, if you want to read data from the db, you'll need to run a query to go fetch it.  (you also need to make your trigger bulk safe, yours currently only does something for the last object in the trigger)

 

trigger myTrigger on Junction_Object__c(before insert) {

Set<id> accIds = new Set<Id>();

  for (Junction_Object__c o : Trigger.new) {

           accIds.put(o.account__c);

        }

        map<id, Account> accs = [select id,phone from account where id in :accIds];

        List<Contact> contacts = new List<Contact>(); 

        for (Junction_Object__c o : Trigger.new) {

              Contact c = new Contact(id=o.contact__c, Phone = accs.get(o.account__c).phone);

              contacts.add(c);

        }

  update contacts;