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
diydiy 

how i pass Account value in contact in multiple tables

two pageblock table


1 is Account


2nd is contact


one save button


Add row and remove row button


1st pageblock  I added 3 Account and 2nd page block 6 contact ... 1st pageblock Account  1st fied called radio button .. if i select radio button and add contact in 2nd pageblcok table.. foreign key for Account is selected radio button..and click save button


3 diff Account  and 5 diff contact save


do u have any idea? how i pass Account value in contact in this scenario?

GunishGunish
HI Diya,

Can you share your code here on the board, we might be able to help.

On a general guidance note, this is how you would do this,

From the first save button, when you call the save method in the controller, store the account id, in a variable in the class, and then when you call the second save button, retrieve the previously saved value to populate the account id.

Let me know if this helps!
-Gunish
diydiy

thanks gunish.. but I want to use single save

GunishGunish
Hi Diya,

I can be of more help if you share your code here. You can PM me if you prefer otherwise.

-Gunish
diydiy

 

 

      I want to save themultiple  Account and multiple contact in one vf page.There are two pageblock table one is account and another one is contact. Add and remove button in 2 pageblock table. first field of acoount page block table is radio button ...

 

 

 

scenario is

 

1) add  2 Account item - A1 ,A2

2) select a A1 radio button

3) add 2 contact C1,C2

4) select a A2 radio button

5) add 3 contact C3,C4,C5

6) click save botton

 

 

   please copy and paste a apex class and vf page as below

 

 

class:

 

public class ClsaccountConfigs
{
    public List<Account> AccountList {get;set;}
    public List<Account> AccountAddList {get;set;}  
     public List<contact> contactList {get;set;}
    public List<contact> contactAddList {get;set;}
    public Boolean Removebool {get;set;}
    public Boolean Removeboolcont {get;set;}
    public String AccountName {get;set;}   
    public String contactName {get;set;}
    public integer counter=1;
    private ApexPages.StandardController controller;  
    public ClsaccountConfigs ()
    {
         this.controller = controller;
        String sql = 'SELECT name,Phone,AccountNumber FROM Account';
        AccountList = Database.Query(sql);
       AccountAddList = new List<Account>();
        AccountList.add(new Account());
        
         this.controller = controller;
        String sql1 = 'SELECT name,lastname,Phone,email FROM contact';
        contactList = Database.Query(sql1);
       contactAddList = new List<contact>();
        contactList.add(new contact());
       
       // Removebool = True;
    }
    
    public void AddRow()
    {
        AccountAddList.add(new Account());
        Removebool = true;
       
    }  
    
    
    public void RemoveRow()
    {
        Integer i = AccountAddList.size();
        AccountAddList.remove(i-1);
        
        if(AccountAddList.size()==1)
        {
        Removebool = false;  
        }
        
    }  
    
      public void AddRowcontact()
    {
        contactAddList.add(new contact());
        Removeboolcont = true;
       
    }  
    
    
    public void RemoveRowcontact()
    {
        Integer i = contactAddList.size();
       contactAddList.remove(i-1);
        
        if(contactAddList.size()==1)
        {
        Removeboolcont = false;  
        }
        
    }
   
    
   public PageReference save()
   {      
     insert AccountAddList;   
     insert contactAddList;        
     Pagereference savepage = new Pagereference ('https://ap1.salesforce.com/001/o');
     return savepage;
     }
     
   
}

 

 

page :

 

 

<apex:page sidebar="false" Controller="ClsaccountConfigs " >
 
<apex:form >
     <apex:commandButton value="Save"  action="{!save}"  />
    <apex:pageBlock id="ComAdd" title="accounts"  >    
    <apex:pageMessages ></apex:pageMessages>              
        <apex:pageblockSection >
            <apex:outputPanel style="width:900px;float:right;" >
            <apex:pageBlockTable value="{! AccountAddList}"  var="com"    >
                <apex:column headervalue="select">                    
                        
                        <input type="radio" />                                                                                             
                    </apex:column>
                <apex:column headerValue="name">
                    <apex:inputField value="{!com.name}"/>
                </apex:column>
                <apex:column headerValue="phone">
                    <apex:inputField value="{!com.phone}"/>
                </apex:column>
                <apex:column headerValue="AccountNumber">
                    <apex:inputField value="{!com.AccountNumber}"/>
                </apex:column>
                                                                                                                                        
               </apex:pageBlockTable>
              </apex:outputPanel>
                 </apex:pageblockSection>               
               <apex:pageblockSection columns="0" >
               <apex:pageBlockSectionItem >
            
                </apex:pageBlockSectionItem>  
                                 
        </apex:pageblockSection>        
       <!-- <apex:pageblockSection columns="1" >
            <apex:pageblockSectionItem > -->
                <apex:pageBlockButtons location="bottom"  >           
                <apex:commandButton value="Add Row" action="{!addRow}" reRender="ComAdd"   />               
                <apex:commandButton value="Remove Row" action="{!RemoveRow}" rendered="{!Removebool}" reRender="ComAdd"/>
               <!-- <apex:commandButton value="Save"  action="{!save}"  /> -->
               
                </apex:pageBlockButtons>
           <!-- </apex:pageblockSectionItem>         
        </apex:pageblockSection> -->
    </apex:pageBlock>
    
     <apex:pageBlock id="ComAdd1" title="contact"  >    
    <apex:pageMessages ></apex:pageMessages>              
        <apex:pageblockSection >
            <apex:outputPanel style="width:900px;float:right;" >
            <apex:pageBlockTable value="{! contactAddList}"  var="com1"    >
               
                <apex:column headerValue="name">
                    <apex:inputField value="{!com1.lastname}"/>
                </apex:column>
                <apex:column headerValue="phone">
                    <apex:inputField value="{!com1.phone}"/>
                </apex:column>
                <apex:column headerValue="email">
                    <apex:inputField value="{!com1.email}"/>
                </apex:column>
                                                                                                                                        
               </apex:pageBlockTable>
              </apex:outputPanel>
                 </apex:pageblockSection>               
               <apex:pageblockSection columns="0" >
               <apex:pageBlockSectionItem >
            
                </apex:pageBlockSectionItem>  
                                 
        </apex:pageblockSection>        
       
                <apex:pageBlockButtons location="bottom"  >           
                <apex:commandButton value="Add Row" action="{!addRowcontact}" reRender="ComAdd1"   />               
                <apex:commandButton value="Remove Row" action="{!RemoveRowcontact}" rendered="{!Removeboolcont}" reRender="ComAdd1"/>
              <!--  <apex:commandButton value="Save"  action="{!save}"  /> -->
               
                </apex:pageBlockButtons>
           <!-- </apex:pageblockSectionItem>         
        </apex:pageblockSection> -->
    </apex:pageBlock>
</apex:form>
</apex:page>

GunishGunish

Okay, 

 

The first problem is with the save function, 

You have to break it into two parts, 

1, when you insert an account, 2, when you insert a contact. 

 

The reason for that is, to link an Contact to an Account, you need an accountID, which is a salesforce generated id and is only generated after the account is inserted into the system. 

 

Once the account is inserted, then you can pickup the id which was geneted by salesforce and update the contact record with the account id, and insert it afterwards. 

 

The other alternative is you build a system for generating you own ID, and store it with the Account when inserting it. 

 

If you do break up your solution into two parts, 

The way to send back your selected account id, into the controller is to add javascript remoting or ActionFuction. 

The PseudoCode would look something like this.

 

Page :

<RadioButton id="A" value="B" onClick="setSelectedAccount(this)" />

<script>
function setSelectedAccount(e) {
   //Call the Javscript Remoting method from the apex class, and set the account id
   JavaScriptRemotingMethod(e.value);
}
<script>


Controller

Class ControllerClass {
   
   // Private 
string selectedAccountId{get;set;}
// Add Javascript remoting attributes public pageReference setSelectedAccount(value) { // Add more code
selectedAccountId = value; }
// while adding contacts, use the selectedAccountID to populate the accountid on the contact row.
}

  Hope this helps!

 

-Gunish

diydiy

Hi Gunish,

 

Thanks for quick reply.. Can you elaborate this one?

 

thanks

diya