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
sailersailer 

Not able to build the relation for Account to conact

Hello 

 

I need to query all the contact from the account and its working and when i update the records its get updated and new contact records is created ,but that contact is not associated to that account.

Below is the log and error 

 

Apex Class

 

 

public class MultiAdd
{

public List<contact> lstcontact = new List<contact>();
public List<Contact> contacts;
public List<Contact> accts {get; set;}
public ApexPages.StandardController std;



//list of the inner class
public List<innerClass> lstInner
{ get;set; }

//will indicate the row to be deleted
public String selectedRowIndex
{get;set;}

//no. of rows added/records in the inner class list
public Integer count = 1;
//{get;set;}


////save the records by adding the elements in the inner class list to lstAcct,return to the same page
public PageReference Save()
{
PageReference pr = new PageReference('/apex/jqueryDoctorProfile');

for(Integer j = 0;j<lstInner.size();j++)
{

system.debug('123456789@@@@@@@@@@@@@@@@'+lstInner.size());

lstcontact .add(lstInner[j].acct);
}
system.debug('lstcontactupsert------------------------>'+lstcontact);
upsert lstcontact;
pr.setRedirect(True);
return pr;
}

//add one more row
public void Add()
{
count = count+1;
addMore();
}
public void addMoreconst()
{
//call to the iner class constructor
innerClass objInnerClass = new innerClass(count);
List<Contact> lstCon =new List<Contact>();
// List<Contact> lstCon = [SELECT testvantage__TotalNumber__c , testvantage__DoctorDescription__c, AccountId, LastName, FirstName from Contact limit 1];
lstCon=getContacts();

system.debug('totalconatctlsit'+lstCon.Size());
for(Contact c: lstCon){
objInnerClass = new innerClass(count);
objInnerClass.acct = c;
//objInnerClass.acct = c.AccountId;


lstInner.add(objInnerClass);
}
//add the record to the inner class list

system.debug('lstInner---->'+lstInner);
}/* end addMore*/

/*Begin addMore*/
public void addMore()
{
//call to the iner class constructor
innerClass objInnerClass = new innerClass(count);

//add the record to the inner class list
lstInner.add(objInnerClass);
system.debug('lstInner---->'+lstInner);
}/* end addMore*/

/* begin delete */
public void Del()
{
system.debug('selected row index---->'+selectedRowIndex);
lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
count = count - 1;

}/*End del*/



/*Constructor*/
public MultiAdd(ApexPages.StandardController ctlr)
{

std=ctlr;
lstInner = new List<innerClass>();
addMoreconst();
selectedRowIndex = '0';


}/*End Constructor*/
public Account getAccount()
{
return (Account) std.getRecord();
}


/*Inner Class*/
public class innerClass
{
/*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
public String recCount
{get;set;}


public contact acct
{get;set;}

/*Inner Class Constructor*/
public innerClass(Integer intCount)
{
recCount = String.valueOf(intCount);

/*create a new account*/
acct = new contact();


}/*End Inner class Constructor*/
}/*End inner Class*/
public List<Contact> getContacts()
{ if ( (null!=getAccount().id) && (contacts == null) )
{
contacts=[SELECT Id, Name, testvantage__TotalNumber__c , testvantage__DoctorDescription__c, AccountId, LastName, FirstName FROM Contact WHERE AccountId =:ApexPages.currentPage().getParameters().get('id') ORDER BY CreatedDate];
} return contacts;
}
}/*End Class*/

 

VFPAGE

 

 

<apex:page StandardController="Account" extensions="MultiAdd" id="thePage">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$j = jQuery.noConflict();
$j(document).ready(function()
{
$("input[name^=TotalNumber]").keyup(function() {
var sum = 0;
$("input[name^=TotalNumber]").not("input[name=totalcost]").each(function() {
var number = parseInt(this.value) || 0;
sum += parseInt(number);
});
$("input[name=Description]").val(sum);
});

 

}
</script>
</head>
<apex:form >
<apex:pageblock id="pb" >
<apex:pageBlockButtons >
<apex:commandbutton value="Add" action="{!Add}" rerender="pb1"/>
<apex:commandbutton value="Save" action="{!Save}"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Account Details" collapsible="true" id="mainRecord" columns="2" >
<apex:inputField value="{!Account.Name}"/>
<apex:inputField value="{!Account.Type}"/>
<apex:inputField value="{!Account.BillingStreet}"/>
<apex:inputField value="{!Account.Industry}"/>
<apex:inputField value="{!Account.Phone}"/>

</apex:pageBlockSection>


<apex:pageblock id="pb1">

<apex:repeat value="{!lstInner}" var="e1" id="therepeat">
<apex:panelGrid columns="6">

<apex:panelGrid headerClass="Name">
<apex:facet name="header">Del</apex:facet>
<apex:commandButton value="X" action="{!Del}" rerender="pb1">
<apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
</apex:commandButton>
</apex:panelGrid>

<apex:panelGrid title="SPD" >
<apex:facet name="header">LastName</apex:facet>
<apex:inputfield value="{!e1.acct.LastName}"/>
</apex:panelGrid>

<apex:panelGrid >
<apex:facet name="header">FirstName</apex:facet>
<apex:inputfield value="{!e1.acct.FirstName}"/>
</apex:panelGrid>

<apex:panelGrid >
<apex:facet name="header">TotalNumber</apex:facet>
<apex:inputfield value="{!e1.acct.TotalNumber__c}"/>
</apex:panelGrid>


<apex:panelGrid >
<apex:facet name="header">DoctorDescription</apex:facet>
<apex:inputfield value="{!e1.acct.DoctorDescription__c}"/>
</apex:panelGrid>


</apex:panelgrid>



</apex:repeat>

<apex:panelgrid >


</apex:panelgrid>



</apex:pageBlock>


</apex:pageblock>
</apex:form>
</apex:page>

 

When is save the record i get the following error.

 

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

 

Class.testvantage.MultiAdd.addMoreconst: line 56, column 1
Class.testvantage.MultiAdd.<init>: line 98, column 1

 

 

The Debug log is below and i am able to see the accountid but is not getting asscoiated to that account

 

Contact:{Name=Ashley TestVf22, testvantage__DoctorDescription__c=Test123, AccountId=0019000000OmGsCAAV, FirstName=Ashley, Id=0039000000P7oI9AAJ, testvantage__TotalNumber__c=122, LastName=TestVf22}

 

 

 

hitesh90hitesh90

Hi,

 

Try to use following class for your requirement.

 

Apex Class:

public class MultiAdd
{
    
    public List<contact> lstcontact  = new List<contact>();
    public List<Contact> contacts;
    private ApexPages.StandardController std;
    
    //list of the inner class
    public List<innerClass> lstInner
    {   get;set;    }
    
    //will indicate the row to be deleted
    public String selectedRowIndex
    {get;set;}  
    
    //no. of rows added/records in the inner class list
    public Integer count = 1;
    //{get;set;}
    
    
    ////save the records by adding the elements in the inner class list to lstAcct,return to the same page
    public PageReference Save()
    {
        PageReference pr = new PageReference('/apex/jqueryDoctorProfile');
        
        for(Integer j = 0;j<lstInner.size();j++)
        {
            lstcontact.add(lstInner[j].acct);
        }
        upsert lstcontact;
        pr.setRedirect(True);
        return pr;
        return null;
    }
        
    //add one more row
    public void Add()
    {   
        count = count+1;
        addMore();      
    }
     public void addMoreconst()
    {
        //call to the iner class constructor
        innerClass objInnerClass = new innerClass(count);
        List<Contact> lstCon = getContacts();
        for(Contact c: lstCon){
            objInnerClass = new innerClass(count);
            objInnerClass.acct = c;
            lstInner.add(objInnerClass);
        }
        //add the record to the inner class list
            
        system.debug('lstInner---->'+lstInner);            
    }/* end addMore*/
    
    /*Begin addMore*/
    public void addMore()
    {
        //call to the iner class constructor
        innerClass objInnerClass = new innerClass(count);
         objInnerClass.acct.Accountid = getAccount().id;
        //add the record to the inner class list
            lstInner.add(objInnerClass);
        system.debug('lstInner---->'+lstInner);            
    }/* end addMore*/
    
    /* begin delete */
    public void Del()
    {
        system.debug('selected row index---->'+selectedRowIndex);
        lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
        count = count - 1;
        
    }/*End del*/
    
    
    
    /*Constructor*/
    public MultiAdd(ApexPages.StandardController ctlr)
    {
    
        std=ctlr;
        lstInner = new List<innerClass>();
        addMoreconst();
        selectedRowIndex = '0';
        
        
    }/*End Constructor*/
     public Account getAccount()  
      {      
        return (Account) std.getRecord();   
      }
        


    /*Inner Class*/
    public class innerClass
    {       
        /*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
        public String recCount
        {get;set;}
        
        
        public contact acct
        {get;set;}
        
        /*Inner Class Constructor*/
        public innerClass(Integer intCount)
        {
            recCount = String.valueOf(intCount);        
            
            /*create a new account*/
            acct = new contact();
            
            
        }/*End Inner class Constructor*/    
    }/*End inner Class*/
     public List<Contact> getContacts()    
  {        if ( (null!=getAccount().id) && (contacts == null) )  
          {            
            contacts=[SELECT Id, Name, Email, Phone, AccountId, Title,  Salutation, OtherStreet, OtherState, OtherPostalCode,   OtherPhone, OtherCountry, OtherCity, MobilePhone, MailingStreet, MailingState,     MailingPostalCode, MailingCountry, MailingCity, LeadSource, LastName, HomePhone, FirstName, Fax, Description, Department   FROM Contact  WHERE AccountId = : getAccount().ID   ORDER BY CreatedDate];  
                  }                                    return contacts;  
                     }
}/*End Class*/

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

hitesh90hitesh90

I have added below line in your code for relationship.

 

 objInnerClass.acct.Accountid = getAccount().id;

 

tggagnetggagne
That's a lot of code to accomplish something relatively simple. Check out Andrew Facett's Unit of Work article or my own on dependency management.

http://doingpoorly.blogspot.com/2013/12/simple-dependency-management-for.html