+ Start a Discussion
Mamadou Diallo 16Mamadou Diallo 16 

pass record Id to inner class (Compile Error: Variable does not exist)

Hello,
I'm trying to pass the account Id to the new Contact role but I'm getting this error: Compile Error: Variable does not exist: currentAccoun​t.Id
public class AccountWithContactRolesExtension{	
	Account currentAccount {get; set;}
	
	public AccountWithContactRolesExtension(ApexPages.StandardController stdCtrl){
		std=stdCtrl;
		lstInner = new List<innerClass>();
		addMore();
		selectedRowIndex = '0';
		currentAccount = (Account) std.getRecord();
		System.Debug('#######current Account Id:' + currentAccount.Id); 
	}

    public void Add()
    {   
        count = count+1;
       // AccountId = getAccount().id;
        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*/
        
    public class innerClass {     

        public String recCount
        {get;set;}
		
        /*Inner Class Constructor*/
        public innerClass(Integer intCount) {
            recCount = String.valueOf(intCount);        
            
            /*create a new AccountContactRole*/
            if(intCount>0 && contactRole == null && currentRecord != null){
               AccountContactRole   contactRole = new AccountContactRole(AccountId=currentAccount.Id);
            }    
        }/*End Inner class Constructor*/    
    }/*End inner Class*/
}
Thank you for your help.
 
Best Answer chosen by Mamadou Diallo 16
Amit Chaudhary 8Amit Chaudhary 8
there was one extra }
try below code
public class AcctWithContactsAndRolesExtension 
{
	public Account currentAccount {get; set;}
    public ApexPages.StandardController std;
    
	// the associated contacts
	public List<Contact> contacts;
    // the chosen contact id - used when deleting a contact
    public Id chosenContactId {get; set;}
    public String newContactFirstName {get; set;}
    public String newContactLastName {get; set;}
    public AcctWithContactsAndRolesExtension()
	{
	
    }
     
		public AcctWithContactsAndRolesExtension(ApexPages.StandardController stdCtrl){
			std=stdCtrl;
			lstInner = new List<innerClass>();
			addMore();
			selectedRowIndex = '0';
			currentAccount = (Account) std.getRecord();
			System.Debug('#######current Account Id:' + currentAccount.Id); 
			addMore();
		}
		 
		public Account getAccount(){
		 return (Account) std.getRecord();
		}
 
		public SObject getSobject(){
			return std.getRecord();
		}
      
		private boolean updateContacts()
		{
			boolean result=true;
			if (null!=contacts)
			   {
				 List<Contact> updConts=new List<Contact>();
				  try
				  {
				   update contacts;
				  }
				  catch (Exception e)
				  {
					 String msg=e.getMessage();
					 integer pos;
					  
					 // if its field validation, this will be added to the messages by default
					 if (-1==(pos=msg.indexOf('MailState_2digit_code, ')))
					 {
						ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
					 }
					  
					 result=false;
				  }
			   }
				
			   return result;
		}
     
		public PageReference saveAndExit()
		{
			 boolean result=true;
			 boolean result1=true;
			 result=updateContacts();
			  
			 if (result){
				// call standard controller save
				return std.save();
			 }
			 else
			 {
			  return null;
			 }
		}
    
		public void newContact()
		{
		   if (updateContacts())
		   {
			  Contact cont=new Contact(FirstName=newContactFirstName, LastName=newContactLastName, AccountId=getAccount().id);
			  insert cont;
			 
			  newContactFirstName=null;
			  newContactLastName=null;
			  contacts=null;
		   }
		}
    
    
     
		public void deleteContact()
		{
		   if (updateContacts())
		   {
			  if (null!=chosenContactId)
			  {
				 Contact cont=new Contact(Id=chosenContactId);
				  delete cont;
			
				  contacts=null;
				  chosenContactId=null;
			  }
		   }
		}
     
		public List<Contact> getContacts()
		{
		   if ( (null!=getAccount().id) && (contacts == null) )
		   {
				contacts=[SELECT Id, FirstName, LastName, Name, Title, Email, Active__c, AccountId, Phone, CreatedDate,
							MailingStreet, MailingCity, MailingState
							FROM Contact
							WHERE AccountId = : getAccount().ID
							ORDER BY CreatedDate]; 
		   }
							   
		   return contacts;
		}
    
	//Account Contact Roles
 
		public List<AccountContactRole> contactRoles;
		public List<AccountContactRole> getContactRoles(){
			if((getAccount().id != null) && (contactRoles == null)){
				contactRoles = [SELECT Id, Role, IsPrimary, AccountId, Contact.Name 
								FROM AccountContactRole
								WHERE AccountId = : getAccount().ID
								ORDER BY CreatedDate];         
			}
			return contactRoles;
		}
		//will hold the AccountContactRole records to be saved
		public List<AccountContactRole> lstRole  = new List<AccountContactRole>();
    
		//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 lstRole,return to the same page
		public Boolean UpdateRole(){
			Boolean newRole = True;
		  //  PageReference pr=ApexPages.currentPage();
			
			for(Integer j = 0;j<lstInner.size();j++)
			{
				lstRole.add(lstInner[j].contactRole);
			} 
			insert lstRole;
		
			return newRole = True;
		}
    
		public PageReference save()
		{
			Boolean result=true;
			Boolean newRole=true;
			Boolean result1=true;
			PageReference pr=ApexPages.currentPage();

			if (null!=getAccount().id)
			{
			result=updateContacts();
			newRole=updateRole();

			}
			else
			{
			pr.setRedirect(true);
			}

			if (result)
			{
			// call standard controller save, but don't capture the return value which will redirect to view page
			std.save();
			   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
			}
			ApexPages.currentPage().getParameters().put('id', getAccount().id);

			return pr;
			//  system.debug('pr:----->' +pr);

		}
        
		//add one more row
		public void Add()
		{   
			count = count+1;
		   // AccountId = getAccount().id;
			addMore();      
		}
    
		/*Begin addMore*/
		public void addMore()
		{
			//call to the iner class constructor
			innerClass objInnerClass = new innerClass(count, currentAccount);
			
			//add the record to the inner class list
			lstInner.add(objInnerClass);    
			system.debug('lstInner---->'+lstInner);            
		}/* end addMore*/
    
		public void Del()
		{
			system.debug('selected row index---->'+selectedRowIndex);
			lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
			count = count - 1;
			
		}
        
        public String recCount  {get;set;}
        
        
        public AccountContactRole contactRole   {get;set;}
       
        
        public  innerClass 
		{    
			public String recCount{get;set;}
			public innerClass(Integer intCount , Account acc) 
			{
				   recCount = String.valueOf(intCount);       
				   System.Debug('#######current Account Id:' + acc.Id);
				   AccountContactRole   contactRole = new AccountContactRole(AccountId=acc.Id);
			}
		}/*End inner Class*/
}

now test and let us know the result

All Answers

Raj VakatiRaj Vakati
Make it as a public  like below .. its access specifier issue .. 

public Account currentAccount {get; set;}

complete code is here
 
public class AccountWithContactRolesExtension{	
	public Account currentAccount {get; set;}
	
	public AccountWithContactRolesExtension(ApexPages.StandardController stdCtrl){
		std=stdCtrl;
		lstInner = new List<innerClass>();
		addMore();
		selectedRowIndex = '0';
		currentAccount = (Account) std.getRecord();
		System.Debug('#######current Account Id:' + currentAccount.Id); 
	}

    public void Add()
    {   
        count = count+1;
       // AccountId = getAccount().id;
        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*/
        
    public class innerClass {     

        public String recCount
        {get;set;}
		
        /*Inner Class Constructor*/
        public innerClass(Integer intCount) {
            recCount = String.valueOf(intCount);        
            
            /*create a new AccountContactRole*/
            if(intCount>0 && contactRole == null && currentRecord != null){
               AccountContactRole   contactRole = new AccountContactRole(AccountId=currentAccount.Id);
            }    
        }/*End Inner class Constructor*/    
    }/*End inner Class*/
}

 
Mamadou Diallo 16Mamadou Diallo 16
Thank you Raj. I'm trying it and let you know.
Mamadou Diallo 16Mamadou Diallo 16
Thank you Raj for your help but unfortunately, it did not work. Compile Error: Variable does not exist: currentAccount
Raj VakatiRaj Vakati
try this
 
public class AccountWithContactRolesExtension{	
	public static  Account currentAccount {get; set;}
	
	public AccountWithContactRolesExtension(ApexPages.StandardController stdCtrl){
		std=stdCtrl;
		lstInner = new List<innerClass>();
		addMore();
		selectedRowIndex = '0';
		currentAccount = (Account) std.getRecord();
		System.Debug('#######current Account Id:' + currentAccount.Id); 
	}

    public void Add()
    {   
        count = count+1;
       // AccountId = getAccount().id;
        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*/
        
    public class innerClass {     

        public String recCount
        {get;set;}
		
        /*Inner Class Constructor*/
        public innerClass(Integer intCount) {
            recCount = String.valueOf(intCount);        
            
            /*create a new AccountContactRole*/
            if(intCount>0 && contactRole == null && currentRecord != null){
               AccountContactRole   contactRole = new AccountContactRole(AccountId=currentAccount.Id);
            }    
        }/*End Inner class Constructor*/    
    }/*End inner Class*/
}

 
Mamadou Diallo 16Mamadou Diallo 16
I was able to save without error but in the visualforce page, the account Id dis not get to the new contact role form.
Mamadou Diallo 16Mamadou Diallo 16
Here is the part of the visualforce form that calls the AccountId.
<apex:panelGrid >
                    <apex:facet name="header">Account Name</apex:facet>
                    <apex:inputfield value="{!e1.ContactRole.AccountId}"/>
</apex:panelGrid>

 
Raj VakatiRaj Vakati
try this and give me your page if it dnt work
 
public class AccountWithContactRolesExtension{	
    public  Account currentAccount {get; set;}
    /*
    public AccountWithContactRolesExtension(ApexPages.StandardController stdCtrl){
        std=stdCtrl;
        lstInner = new List<innerClass>();
        addMore();
        selectedRowIndex = '0';
        currentAccount = (Account) std.getRecord();
        System.Debug('#######current Account Id:' + currentAccount.Id); 
    }
    
    public void Add()
    {   
        count = count+1;
        // AccountId = getAccount().id;
        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*/
   
    public class innerClass {     
        public String recCount{get;set;}
        
        /*Inner Class Constructor*/
        public innerClass(Integer intCount) {
            recCount = String.valueOf(intCount);        
            /*create a new AccountContactRole*/
          //  if(intCount>0 && contactRole == null && currentRecord != null){
                AccountContactRole   contactRole = new AccountContactRole(AccountId=new AccountWithContactRolesExtension().currentAccount.Id);
          //  }    
        }/*End Inner class Constructor*/    
    }/*End inner Class*/
}

 
Amit Chaudhary 8Amit Chaudhary 8
Look like issue is coming because you are accessing the "currentAccount " var in Inner Class.

Try to update your code like below

public class AccountWithContactRolesExtension{    
    public Account currentAccount {get; set;}
    public AccountWithContactRolesExtension(ApexPages.StandardController stdCtrl){
        std=stdCtrl;
        lstInner = new List<innerClass>();
        addMore();
        selectedRowIndex = '0';
        currentAccount = (Account) std.getRecord();
        System.Debug('#######current Account Id:' + currentAccount.Id);
    }

    public void Add()
    {   
        count = count+1;
        addMore();      
    }
    public void addMore()
    {
        innerClass objInnerClass = new innerClass(count ,currentAccount);
        lstInner.add(objInnerClass);    
    }
        
    public class innerClass {     
        public String recCount{get;set;}
        public innerClass(Integer intCount , Account acc) {
            recCount = String.valueOf(intCount);        
            if(intCount>0 && contactRole == null && currentRecord != null){
               AccountContactRole   contactRole = new AccountContactRole(AccountId=acc.Id);
            }
        }
    }
}
 
public class AccountWithContactRolesExtension{	
	public Account currentAccount {get; set;}
	public AccountWithContactRolesExtension(ApexPages.StandardController stdCtrl){
		std=stdCtrl;
		lstInner = new List<innerClass>();
		addMore();
		selectedRowIndex = '0';
		currentAccount = (Account) std.getRecord();
		System.Debug('#######current Account Id:' + currentAccount.Id); 
	}

    public void Add()
    {   
        count = count+1;
        addMore();      
    }
    public void addMore()
    {
        innerClass objInnerClass = new innerClass(count ,currentAccount);
        lstInner.add(objInnerClass);    
    }
        
    public class innerClass {     
        public String recCount{get;set;}
        public innerClass(Integer intCount , Account acc) {
            recCount = String.valueOf(intCount);        
            if(intCount>0 && contactRole == null && currentRecord != null){
               AccountContactRole   contactRole = new AccountContactRole(AccountId=acc.Id);
            }
        }
    }
}

Let us know if this will help you
 
Mamadou Diallo 16Mamadou Diallo 16
Sorry Raj it did not work. Account ID is still blank.
User-added image
Mamadou Diallo 16Mamadou Diallo 16
Thank you Amit. I updated my code as per your suggestion but it did not work. No error but the account Id has been not passed to the form.
Amit Chaudhary 8Amit Chaudhary 8
Hi Mamadou Diallo 16 ,

I am glad at least your variable issue is fixed. Can you please provide your VF page code

Thanks
Amit
Raj VakatiRaj Vakati
try this
 
public class AccountWithContactRolesExtension{	
	public Account currentAccount {get; set;}
	
	public  List<innerClass> lstInner{get;set;}
	public AccountWithContactRolesExtension(ApexPages.StandardController stdCtrl){
		std=stdCtrl;
		lstInner = new List<innerClass>();
	currentAccount = (Account) std.getRecord();
		System.Debug('#######current Account Id:' + currentAccount.Id); 
	
	
	addMore();
		selectedRowIndex = '0';
	}

    public void Add()
    {   
        count = count+1;
        addMore();      
    }
    public void addMore()
    {
        innerClass objInnerClass = new innerClass(count ,currentAccount);
        lstInner.add(objInnerClass);    
    }
        
    public class innerClass {     
        public String recCount{get;set;}
        public innerClass(Integer intCount , Account acc) {
            recCount = String.valueOf(intCount);        
            if(intCount>0 && contactRole == null && currentRecord != null){
               AccountContactRole   contactRole = new AccountContactRole(AccountId=acc.Id);
            }
        }
    }
}

 
Mamadou Diallo 16Mamadou Diallo 16
Amit, I set the system.debug in the inner class but the account Id is not there.
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your VF page code ? and Page URL
How you are calling Add method in VF page ?

NOTE:- I hope you are passing the URL while testing page ?
Mamadou Diallo 16Mamadou Diallo 16
Here is the visualfroce form:
<apex:pageblock id="pb1">
            
        <apex:repeat value="{!lstInner}" var="e1" id="therepeat" rendered="true" >
                <apex:panelGrid columns="5">
                
                <apex:panelGrid headerClass="Name">
                    <apex:facet name="header">Del</apex:facet>
                    <apex:commandButton value="X" action="{!Del}" rerender="pb1" immediate="true">
                        <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
                    </apex:commandButton>
                </apex:panelGrid>   
                
                <apex:panelGrid >
                    <apex:facet name="header">Account Name</apex:facet>
                    <apex:inputfield value="{!e1.ContactRole.AccountId}"/>
                </apex:panelGrid>
                
                <apex:panelGrid >
                    <apex:facet name="header">Contact Name</apex:facet>
                    <apex:inputfield value="{!e1.ContactRole.ContactId}"/>
                </apex:panelGrid>
               
                <apex:panelGrid >
                    <apex:facet name="header">Role</apex:facet>
                    <apex:inputfield value="{!e1.ContactRole.Role}"/>
                </apex:panelGrid>
                
                <apex:panelGrid >
                    <apex:facet name="header">Primary</apex:facet>
                    <apex:inputfield value="{!e1.ContactRole.IsPrimary}"/>
                </apex:panelGrid>
                <apex:panelGrid >
                    <apex:commandbutton value="Add" action="{!Add}" rerender="pb1"/>
                </apex:panelGrid>
            </apex:panelgrid>
        </apex:repeat>
    </apex:pageBlock>

 
Mamadou Diallo 16Mamadou Diallo 16
The idea is to have this part default to the Account of the current record:
<apex:panelGrid >
                    <apex:facet name="header">Account Name</apex:facet>
                    <apex:inputfield value="{!e1.ContactRole.AccountId}"/>
 </apex:panelGrid>

 
Amit Chaudhary 8Amit Chaudhary 8
Page URL Please ? I hope you are passing Account ID in URL ?
are you getting Account in below line ?
System.Debug('#######current Account Id:' + currentAccount.Id);
 
Mamadou Diallo 16Mamadou Diallo 16
are you getting Account in below line ?
System.Debug('#######current Account Id:' + currentAccount.Id);

Yes but not here:
System.Debug('#######current Account Id:' + acc.Id);
Amit Chaudhary 8Amit Chaudhary 8
 public class innerClass {    
        public String recCount{get;set;}
        public innerClass(Integer intCount , Account acc) {
               recCount = String.valueOf(intCount);       
               System.Debug('#######current Account Id:' + acc.Id);
               AccountContactRole   contactRole = new AccountContactRole(AccountId=acc.Id);
        }
    }

Try to update your inner class like above and check account ID. I hope that should work
Mamadou Diallo 16Mamadou Diallo 16
Amit, I updated the code but I'm getting this new error:
Error: Compile Error: Inner types are not allowed to have inner types 
Amit Chaudhary 8Amit Chaudhary 8
Can you please share your code which you are trying. Please share full code.
Mamadou Diallo 16Mamadou Diallo 16
Thank you Amit. Here is the full code:
public class AcctWithContactsAndRolesExtension {
    
	public Account currentAccount {get; set;}
    
    public ApexPages.StandardController std;
     
    // the associated contacts
   public List<Contact> contacts;
   
      
    // the chosen contact id - used when deleting a contact
    public Id chosenContactId {get; set;}
     
    public String newContactFirstName {get; set;}
    public String newContactLastName {get; set;}
     
    public AcctWithContactsAndRolesExtension(){
    }
     
    public AcctWithContactsAndRolesExtension(ApexPages.StandardController stdCtrl){
        std=stdCtrl;
        lstInner = new List<innerClass>();
        addMore();
        selectedRowIndex = '0';
        currentAccount = (Account) std.getRecord();
        System.Debug('#######current Account Id:' + currentAccount.Id); 
        addMore();
    }
     
    public Account getAccount(){
     return (Account) std.getRecord();
    }
 
     public SObject getSobject(){
      return std.getRecord();
     }
      
    private boolean updateContacts()
    {
        boolean result=true;
        if (null!=contacts)
           {
             List<Contact> updConts=new List<Contact>();
              try
              {
               update contacts;
              }
              catch (Exception e)
              {
                 String msg=e.getMessage();
                 integer pos;
                  
                 // if its field validation, this will be added to the messages by default
                 if (-1==(pos=msg.indexOf('MailState_2digit_code, ')))
                 {
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
                 }
                  
                 result=false;
              }
           }
            
           return result;
    }
     
    public PageReference saveAndExit()
    {
     boolean result=true;
     boolean result1=true;
     result=updateContacts();
      
     if (result){
        // call standard controller save
        return std.save();
     }
     else
     {
      return null;
     }
    }
    
    public void newContact()
    {
       if (updateContacts())
       {
          Contact cont=new Contact(FirstName=newContactFirstName, LastName=newContactLastName, AccountId=getAccount().id);
          insert cont;
         
          newContactFirstName=null;
          newContactLastName=null;
          contacts=null;
       }
    }
    
    
     
    public void deleteContact()
    {
       if (updateContacts())
       {
          if (null!=chosenContactId)
          {
             Contact cont=new Contact(Id=chosenContactId);
              delete cont;
        
              contacts=null;
              chosenContactId=null;
          }
       }
    }
     
   public List<Contact> getContacts()
    {
       if ( (null!=getAccount().id) && (contacts == null) )
       {
            contacts=[SELECT Id, FirstName, LastName, Name, Title, Email, Active__c, AccountId, Phone, CreatedDate,
                        MailingStreet, MailingCity, MailingState
                        FROM Contact
                        WHERE AccountId = : getAccount().ID
                        ORDER BY CreatedDate]; 
       }
                           
       return contacts;
    }
    
 //Account Contact Roles
 
     public List<AccountContactRole> contactRoles;
     public List<AccountContactRole> getContactRoles(){
        if((getAccount().id != null) && (contactRoles == null)){
            contactRoles = [SELECT Id, Role, IsPrimary, AccountId, Contact.Name 
                            FROM AccountContactRole
                            WHERE AccountId = : getAccount().ID
                            ORDER BY CreatedDate];         
        }
        
        return contactRoles;
    }
    //will hold the AccountContactRole records to be saved
    public List<AccountContactRole> lstRole  = new List<AccountContactRole>();
    
    //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 lstRole,return to the same page
    public Boolean UpdateRole(){
        Boolean newRole = True;
      //  PageReference pr=ApexPages.currentPage();
        
        for(Integer j = 0;j<lstInner.size();j++)
        {
            lstRole.add(lstInner[j].contactRole);
        } 
        insert lstRole;
    
        return newRole = True;
    }
    
     public PageReference save()
    {
     Boolean result=true;
     Boolean newRole=true;
     Boolean result1=true;
      PageReference pr=ApexPages.currentPage();
      
     if (null!=getAccount().id)
     {
      result=updateContacts();
      newRole=updateRole();
      
     }
     else
     {
      pr.setRedirect(true);
     }
      
     if (result)
     {
        // call standard controller save, but don't capture the return value which will redirect to view page
        std.save();
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
     }
        ApexPages.currentPage().getParameters().put('id', getAccount().id);
      
     return pr;
   //  system.debug('pr:----->' +pr);
    
    }
        
    //add one more row
    public void Add()
    {   
        count = count+1;
       // AccountId = getAccount().id;
        addMore();      
    }
    
    /*Begin addMore*/
    public void addMore()
    {
        //call to the iner class constructor
        innerClass objInnerClass = new innerClass(count, currentAccount);
        
        //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*/
    
    
        
        public String recCount
        {get;set;}
        
        
        public AccountContactRole contactRole 
        {get;set;}
       
        
        public  innerClass {    
        public String recCount{get;set;}
        public innerClass(Integer intCount , Account acc) {
               recCount = String.valueOf(intCount);       
               System.Debug('#######current Account Id:' + acc.Id);
               AccountContactRole   contactRole = new AccountContactRole(AccountId=acc.Id);
        }
    } 
    }/*End inner Class*/
}/*End Class*/

 
Amit Chaudhary 8Amit Chaudhary 8
there was one extra }
try below code
public class AcctWithContactsAndRolesExtension 
{
	public Account currentAccount {get; set;}
    public ApexPages.StandardController std;
    
	// the associated contacts
	public List<Contact> contacts;
    // the chosen contact id - used when deleting a contact
    public Id chosenContactId {get; set;}
    public String newContactFirstName {get; set;}
    public String newContactLastName {get; set;}
    public AcctWithContactsAndRolesExtension()
	{
	
    }
     
		public AcctWithContactsAndRolesExtension(ApexPages.StandardController stdCtrl){
			std=stdCtrl;
			lstInner = new List<innerClass>();
			addMore();
			selectedRowIndex = '0';
			currentAccount = (Account) std.getRecord();
			System.Debug('#######current Account Id:' + currentAccount.Id); 
			addMore();
		}
		 
		public Account getAccount(){
		 return (Account) std.getRecord();
		}
 
		public SObject getSobject(){
			return std.getRecord();
		}
      
		private boolean updateContacts()
		{
			boolean result=true;
			if (null!=contacts)
			   {
				 List<Contact> updConts=new List<Contact>();
				  try
				  {
				   update contacts;
				  }
				  catch (Exception e)
				  {
					 String msg=e.getMessage();
					 integer pos;
					  
					 // if its field validation, this will be added to the messages by default
					 if (-1==(pos=msg.indexOf('MailState_2digit_code, ')))
					 {
						ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
					 }
					  
					 result=false;
				  }
			   }
				
			   return result;
		}
     
		public PageReference saveAndExit()
		{
			 boolean result=true;
			 boolean result1=true;
			 result=updateContacts();
			  
			 if (result){
				// call standard controller save
				return std.save();
			 }
			 else
			 {
			  return null;
			 }
		}
    
		public void newContact()
		{
		   if (updateContacts())
		   {
			  Contact cont=new Contact(FirstName=newContactFirstName, LastName=newContactLastName, AccountId=getAccount().id);
			  insert cont;
			 
			  newContactFirstName=null;
			  newContactLastName=null;
			  contacts=null;
		   }
		}
    
    
     
		public void deleteContact()
		{
		   if (updateContacts())
		   {
			  if (null!=chosenContactId)
			  {
				 Contact cont=new Contact(Id=chosenContactId);
				  delete cont;
			
				  contacts=null;
				  chosenContactId=null;
			  }
		   }
		}
     
		public List<Contact> getContacts()
		{
		   if ( (null!=getAccount().id) && (contacts == null) )
		   {
				contacts=[SELECT Id, FirstName, LastName, Name, Title, Email, Active__c, AccountId, Phone, CreatedDate,
							MailingStreet, MailingCity, MailingState
							FROM Contact
							WHERE AccountId = : getAccount().ID
							ORDER BY CreatedDate]; 
		   }
							   
		   return contacts;
		}
    
	//Account Contact Roles
 
		public List<AccountContactRole> contactRoles;
		public List<AccountContactRole> getContactRoles(){
			if((getAccount().id != null) && (contactRoles == null)){
				contactRoles = [SELECT Id, Role, IsPrimary, AccountId, Contact.Name 
								FROM AccountContactRole
								WHERE AccountId = : getAccount().ID
								ORDER BY CreatedDate];         
			}
			return contactRoles;
		}
		//will hold the AccountContactRole records to be saved
		public List<AccountContactRole> lstRole  = new List<AccountContactRole>();
    
		//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 lstRole,return to the same page
		public Boolean UpdateRole(){
			Boolean newRole = True;
		  //  PageReference pr=ApexPages.currentPage();
			
			for(Integer j = 0;j<lstInner.size();j++)
			{
				lstRole.add(lstInner[j].contactRole);
			} 
			insert lstRole;
		
			return newRole = True;
		}
    
		public PageReference save()
		{
			Boolean result=true;
			Boolean newRole=true;
			Boolean result1=true;
			PageReference pr=ApexPages.currentPage();

			if (null!=getAccount().id)
			{
			result=updateContacts();
			newRole=updateRole();

			}
			else
			{
			pr.setRedirect(true);
			}

			if (result)
			{
			// call standard controller save, but don't capture the return value which will redirect to view page
			std.save();
			   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
			}
			ApexPages.currentPage().getParameters().put('id', getAccount().id);

			return pr;
			//  system.debug('pr:----->' +pr);

		}
        
		//add one more row
		public void Add()
		{   
			count = count+1;
		   // AccountId = getAccount().id;
			addMore();      
		}
    
		/*Begin addMore*/
		public void addMore()
		{
			//call to the iner class constructor
			innerClass objInnerClass = new innerClass(count, currentAccount);
			
			//add the record to the inner class list
			lstInner.add(objInnerClass);    
			system.debug('lstInner---->'+lstInner);            
		}/* end addMore*/
    
		public void Del()
		{
			system.debug('selected row index---->'+selectedRowIndex);
			lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
			count = count - 1;
			
		}
        
        public String recCount  {get;set;}
        
        
        public AccountContactRole contactRole   {get;set;}
       
        
        public  innerClass 
		{    
			public String recCount{get;set;}
			public innerClass(Integer intCount , Account acc) 
			{
				   recCount = String.valueOf(intCount);       
				   System.Debug('#######current Account Id:' + acc.Id);
				   AccountContactRole   contactRole = new AccountContactRole(AccountId=acc.Id);
			}
		}/*End inner Class*/
}

now test and let us know the result
This was selected as the best answer
Mamadou Diallo 16Mamadou Diallo 16
Now a new error:
Error: Compile Error: Variable does not exist: contactRole 
Mamadou Diallo 16Mamadou Diallo 16
Error: Compile Error: Variable does not exist: contactRole  at Line 156
Amit Chaudhary 8Amit Chaudhary 8
Try below code
public class AcctWithContactsAndRolesExtension 
{
	public Account currentAccount {get; set;}
    public ApexPages.StandardController std;
    
	// the associated contacts
	public List<Contact> contacts;
    // the chosen contact id - used when deleting a contact
    public Id chosenContactId {get; set;}
    public String newContactFirstName {get; set;}
    public String newContactLastName {get; set;}
    public AcctWithContactsAndRolesExtension()
	{
	
    }
     
		public AcctWithContactsAndRolesExtension(ApexPages.StandardController stdCtrl){
			std=stdCtrl;
			lstInner = new List<innerClass>();
			addMore();
			selectedRowIndex = '0';
			currentAccount = (Account) std.getRecord();
			System.Debug('#######current Account Id:' + currentAccount.Id); 
			addMore();
		}
		 
		public Account getAccount(){
		 return (Account) std.getRecord();
		}
 
		public SObject getSobject(){
			return std.getRecord();
		}
      
		private boolean updateContacts()
		{
			boolean result=true;
			if (null!=contacts)
			   {
				 List<Contact> updConts=new List<Contact>();
				  try
				  {
				   update contacts;
				  }
				  catch (Exception e)
				  {
					 String msg=e.getMessage();
					 integer pos;
					  
					 // if its field validation, this will be added to the messages by default
					 if (-1==(pos=msg.indexOf('MailState_2digit_code, ')))
					 {
						ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
					 }
					  
					 result=false;
				  }
			   }
				
			   return result;
		}
     
		public PageReference saveAndExit()
		{
			 boolean result=true;
			 boolean result1=true;
			 result=updateContacts();
			  
			 if (result){
				// call standard controller save
				return std.save();
			 }
			 else
			 {
			  return null;
			 }
		}
    
		public void newContact()
		{
		   if (updateContacts())
		   {
			  Contact cont=new Contact(FirstName=newContactFirstName, LastName=newContactLastName, AccountId=getAccount().id);
			  insert cont;
			 
			  newContactFirstName=null;
			  newContactLastName=null;
			  contacts=null;
		   }
		}
    
    
     
		public void deleteContact()
		{
		   if (updateContacts())
		   {
			  if (null!=chosenContactId)
			  {
				 Contact cont=new Contact(Id=chosenContactId);
				  delete cont;
			
				  contacts=null;
				  chosenContactId=null;
			  }
		   }
		}
     
		public List<Contact> getContacts()
		{
		   if ( (null!=getAccount().id) && (contacts == null) )
		   {
				contacts=[SELECT Id, FirstName, LastName, Name, Title, Email, Active__c, AccountId, Phone, CreatedDate,
							MailingStreet, MailingCity, MailingState
							FROM Contact
							WHERE AccountId = : getAccount().ID
							ORDER BY CreatedDate]; 
		   }
							   
		   return contacts;
		}
    
	//Account Contact Roles
 
		public List<AccountContactRole> contactRoles;
		public List<AccountContactRole> getContactRoles(){
			if((getAccount().id != null) && (contactRoles == null)){
				contactRoles = [SELECT Id, Role, IsPrimary, AccountId, Contact.Name 
								FROM AccountContactRole
								WHERE AccountId = : getAccount().ID
								ORDER BY CreatedDate];         
			}
			return contactRoles;
		}
		//will hold the AccountContactRole records to be saved
		public List<AccountContactRole> lstRole  = new List<AccountContactRole>();
    
		//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 lstRole,return to the same page
		public Boolean UpdateRole(){
			Boolean newRole = True;
		  //  PageReference pr=ApexPages.currentPage();
			
			for(Integer j = 0;j<lstInner.size();j++)
			{
				lstRole.add(lstInner[j].contactRole);
			} 
			insert lstRole;
		
			return newRole = True;
		}
    
		public PageReference save()
		{
			Boolean result=true;
			Boolean newRole=true;
			Boolean result1=true;
			PageReference pr=ApexPages.currentPage();

			if (null!=getAccount().id)
			{
			result=updateContacts();
			newRole=updateRole();

			}
			else
			{
			pr.setRedirect(true);
			}

			if (result)
			{
			// call standard controller save, but don't capture the return value which will redirect to view page
			std.save();
			   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Changes saved'));
			}
			ApexPages.currentPage().getParameters().put('id', getAccount().id);

			return pr;
			//  system.debug('pr:----->' +pr);

		}
        
		//add one more row
		public void Add()
		{   
			count = count+1;
		   // AccountId = getAccount().id;
			addMore();      
		}
    
		/*Begin addMore*/
		public void addMore()
		{
			//call to the iner class constructor
			innerClass objInnerClass = new innerClass(count, currentAccount);
			
			//add the record to the inner class list
			lstInner.add(objInnerClass);    
			system.debug('lstInner---->'+lstInner);            
		}/* end addMore*/
    
		public void Del()
		{
			system.debug('selected row index---->'+selectedRowIndex);
			lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
			count = count - 1;
			
		}
        
        public String recCount  {get;set;}
        
        
        public AccountContactRole contactRole   {get;set;}
       
        
        public  innerClass 
		{    
			public String recCount{get;set;}
			public AccountContactRole contactRole   {get;set;}
			public innerClass(Integer intCount , Account acc) 
			{
				   recCount = String.valueOf(intCount);       
				   System.Debug('#######current Account Id:' + acc.Id);
				   contactRole = new AccountContactRole(AccountId=acc.Id);
			}
		}/*End inner Class*/
}

 
Mamadou Diallo 16Mamadou Diallo 16
I'm getting attempt to deference a null object. I can work with that.
I will let you know tomorrow.

Thank you so much for your help Amit and Raj.