You need to sign in to do that
Don't have an account?
Kathleen Lehnigk
get values from account billing address into contact mailing address in new contact
Hi everyone,
I've created a VF page to replace the "New" button. Unfortunately it didn't pull the account address data plus phone number into the contact as the standard page would do.
After some amending my VF page and writing a controller extension, I'm at least able to get the values from the account shown in the contact, but when saving the record I get the following error message:
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!Save}' in component <apex:commandButton> in page test
Class.RelatedController.save: line 23, column 1
The code for my VF page is:
Your help in this is really appreciated.
Thank you
Kathleen
I've created a VF page to replace the "New" button. Unfortunately it didn't pull the account address data plus phone number into the contact as the standard page would do.
After some amending my VF page and writing a controller extension, I'm at least able to get the values from the account shown in the contact, but when saving the record I get the following error message:
System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!Save}' in component <apex:commandButton> in page test
Class.RelatedController.save: line 23, column 1
The code for my VF page is:
<apex:page standardController="Contact" extensions="RelatedController" action="{!AccountPopulated}" title="Contact Edit" showHeader="true" sidebar="true"> <apex:sectionHeader title="Contact Edit" subtitle="New Contact"/> <apex:form id="theform"> <apex:pageMessages escape="false"/> <apex:pageBlock title="Main Detail" mode="edit"> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!Save}" immediate="false"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Contact Information" columns="2"> <apex:inputField value="{!Contact.Salutation}"/> <apex:inputField value="{!Contact.Account.Phone}" required="TRUE"/> <apex:inputField value="{!Contact.FirstName}"/> <apex:inputField value="{!Contact.Email}" required="TRUE"/> <apex:inputField value="{!Contact.LastName}"/> </apex:pageBlockSection> <apex:pageBlockSection title="Address Information" columns="2"> <apex:inputField value="{!contact.AccountId}" required="true"/> <apex:pageBlockSectionItem > </apex:pageBlockSectionItem> <apex:inputField value="{!Contact.Account.BillingStreet}" label="Mailing Street" id="MailingStreet"/> <apex:inputField value="{!Contact.OtherStreet}" id="OtherStreet"/> <apex:inputField value="{!Contact.Account.BillingCity}" label="Mailing City" id="MailingCity" required="true"/> <apex:inputField value="{!Contact.OtherCity}" id="OtherCity"/> <apex:inputField value="{!Contact.Account.BillingPostalCode}" label="Mailing Zip/Postal Code" id="MailingPostalCode"/> <apex:inputField value="{!Contact.OtherPostalCode}" id="OtherPostalCode"/> <apex:inputField value="{!Contact.Account.BillingState}" label="Mailing State/Province" id="MailingState"/> <apex:inputField value="{!Contact.OtherState}" id="OtherState"/> <apex:inputField value="{!Contact.Account.BillingCountry}" label="Mailing Country" id="MailingCountry" required="true"/> <apex:inputField value="{!contact.OtherCountry}" id="OtherCountry"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>my Controller extension is:
public with sharing class RelatedController { public Contact cont{get;set;} public ApexPages.StandardController controller; public RelatedController(ApexPages.StandardController controller) { this.controller = controller; } public void AccountPopulated() { Contact con=(Contact) controller.getRecord(); if(con != null) con.Account=[select Account.BillingStreet, Account.BillingCity, Account.BillingPostalCode, Account.BillingState, Account.BillingCountry, Account.Phone from Account where id=:con.AccountId limit 1]; else this.cont = new Contact (); } public PageReference save() { insert this.cont; return null; }}
Your help in this is really appreciated.
Thank you
Kathleen
Controller extension:
Apex code:
and test class:
All Answers
Please refer to links below that will help you to resolve the error:
1.https://help.salesforce.com/apex/HTViewSolution?urlname=NullPointerException-de-reference-a-null-object-Apex-Code-trigger&language=en_US
2.http://salesforce.stackexchange.com/questions/23715/system-nullpointerexception-attempt-to-de-reference-a-null-object
3.http://salesforce.stackexchange.com/questions/16652/getting-system-nullpointerexception-attempt-to-de-reference-a-null-object
4.http://salesforce.stackexchange.com/questions/24190/system-nullpointerexception-attempt-to-de-reference-a-null-object
5.https://developer.salesforce.com/forums/ForumsMain?id=906F00000008mmmIAA
I hope this helps.
The code for the extension is
But now I need help with the test class. I only got 45% coverage. Please help me to get the minimum of 75%.
Sorry, but this is my first controller (extension) and test class. Thank you.
Best wishes
Kathleen
Controller extension and test class:
the apex page remained the same and is above.
Controller extension:
Apex code:
and test class: