function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RavitejaRaviteja 

Help on Apex code that copy parent address to chlild adddress

HI,


Iam writing Apex code to create new contact record,when i select the parent account the address of selected account should copy to contact address fields  in edit page itself.

 

I wrote some code :

vf:

-------------------------------

<apex:page sidebar="false" Controller="cont">
    <apex:form >  
    <apex:inputfield value="{!con.accountid}">
    <apex:actionSupport event="onchange" action="{!getrec}"/>
    </apex:inputField>    
    </apex:form>
</apex:page>


class:

-------------------------------------------------------

public class cont {
 
  public contact con {get; set; }
    
     public PageReference getrec() {
      if(con!=null){    
    contact cc=[select id,name from contact where id=:con.accountid ];
        }   
        return null;
    }      
}

 

Getting following Error:
------------------------------------------------

Visualforce Error

System.NullPointerException: Attempt to de-reference a null object

Error is in expression '{!getrec}' in component <apex:page> in page parentaddr

 

logontokartiklogontokartik

Please look at the similar post. 

 

Similar Post

 

This should solve your issue. You are not initializing the contact record. Please use Constructor to initialize the records.