+ Start a Discussion
Adaniels117Adaniels117 

Help with Trigger Error: Attempt to de-reference a null object

Hey Everyone,

Beginner here, hoping if somebody could help me with my trigger. I'm attempting to create a new contract when a custom object is updated.

It allows me to save the trigger, but when i update the custom object I'm getting the following error:

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

Here is the code i'm using:

/////////////////////////////////////////////////

trigger AutoContract on Zuora__SubscriptionProductCharge__c (After Update) {
List<Contract> listContracts= new List<Contract>();
for(Zuora__SubscriptionProductCharge__c S: Trigger.new){
Contract C= new Contract();
C.Description=S.Zuora__ProductDescription__c;
C.accountid=S.Zuora__Account__r.Id;
C.Type_of_Sale__c=S.Zuora__ProductName__c;
C.Payment_Plan__c=S.Zuora__BillingPeriod__c;
C.Contract_Total__c=S.Zuora__ExtendedAmount__c;
C.StartDate=S.Zuora__EffectiveStartDate__c;
C.Account.Name=S.Zuora__Account__r.Name;
C.User__c='00550000002TLtx';
listContracts.add(c);
}

   if(listContracts.isEmpty()== false)
{Database.insert(listContracts);

}


  }

//////////////////////////////////////////////////

It seems that the line that is causing the error is the line where i'm trying to map the Contract account name. Can anybody help explain why i'm getting the error and what I need to add to fix?

Thank you!
Best Answer chosen by Adaniels117
sfdc_ninjasfdc_ninja
Yo dont need to set the account name.  If you just set the AccountId field on the contract, this sets the relationship, and that is all you need.  Assuming from your code, you have a lookup on the Zuora__SubscriptionProductCharge__c object to the account which is named Zuora__Account__c, then below should fix your issue

Instead of this

C.Account.Name=S.Zuora__Account__r.Name;

Try this

C.AccountId = S.Zuora__Account__c


All Answers

sfdc_ninjasfdc_ninja
Yo dont need to set the account name.  If you just set the AccountId field on the contract, this sets the relationship, and that is all you need.  Assuming from your code, you have a lookup on the Zuora__SubscriptionProductCharge__c object to the account which is named Zuora__Account__c, then below should fix your issue

Instead of this

C.Account.Name=S.Zuora__Account__r.Name;

Try this

C.AccountId = S.Zuora__Account__c


This was selected as the best answer
chris_centrachris_centra
right, s.zuora__account__r is null, so you're getting the exception.  when you're in a trigger, the context (trigger.new) contains all fields on the object that was updated, but it doesn't contain any of fields from records that are lookups.  so in this case, you can see s.zuroa__account__c (the account id), but you can't see any fields on that account.  so get those fields, you need to query them separately.  (to make it batch safe, iterate over all incoming records and build a set of account ids...then query them into a map...and then interate again over incoming records with the code you have above - but use the map to grab the Account name).
thanks
chris
sfdc_ninjasfdc_ninja
What you are saying is corect Chris, but for the code he has shown, there is no need to set the Account name.  Once he sets the AccountId, the name will come over with the record as it is now related.
chris_centrachris_centra
sfdc_ninja - that depends, right?  yes, if he just wants the reference, then he should do what you describe.  but maybe he wants to have the name in that field for some purpose downstream?  i'm not sure what he's doing with the data, i'm just explaining why he's getting the nullptrex.  but what you say is absolutely true.  

thanks
chris
sfdc_ninjasfdc_ninja
Yes, you are right about not having relationships in trigger.new context.  My point is just that he is trying to set the Related Account name, which is unecesary.  If he wanted it for some reason, to use later, then yes.  I'm just saying that setting is unecesary and not how you relate objects.
Adaniels117Adaniels117
sfdc_ninja....your update worked like a charm. Thank you!

I've checked off "best answer" on your response. Thanks again everyone!